Skip to content

Commit

Permalink
feat(backend): contentのMFM適用対象外部分をエスケープする
Browse files Browse the repository at this point in the history
  • Loading branch information
noellabo committed Jan 11, 2025
1 parent 8076f78 commit e9ac22d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/backend/src/@types/twemoji.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

declare module '@twemoji/parser/dist/lib/regex.js' {
const regex: RegExp;
export default { default: regex };
}
7 changes: 6 additions & 1 deletion packages/backend/src/core/MfmService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { URL } from 'node:url';
import { Inject, Injectable } from '@nestjs/common';
import * as parse5 from 'parse5';
import { Window, XMLSerializer } from 'happy-dom';
import twemojiRegex from '@twemoji/parser/dist/lib/regex.js';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import { intersperse } from '@/misc/prelude/array.js';
Expand All @@ -23,6 +24,10 @@ type ChildNode = DefaultTreeAdapterMap['childNode'];
const urlRegex = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+/;
const urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/;

const mfmEscaperSource = `(?:(?<through>${twemojiRegex.default.source}|(?<![a-z0-9]):[a-z0-9_+-]+:(?![a-z0-9]))|(?<plain>[<>[]|\\*{1,3}|\\(|\`{1,3}|_{1,2}|~~|検索|search))`;
const mfmEscaperRegex = new RegExp(mfmEscaperSource, 'ig');
const mfmEscaper = (...args: any[]) => (args.at(-1).through ?? `<plain>${args.at(-1).plain}</plain>`);

@Injectable()
export class MfmService {
constructor(
Expand Down Expand Up @@ -70,7 +75,7 @@ export class MfmService {

function analyze(node: Node) {
if (treeAdapter.isTextNode(node)) {
text += node.value;
text += node.value.replace(mfmEscaperRegex, mfmEscaper);
return;
}

Expand Down

0 comments on commit e9ac22d

Please sign in to comment.