From 19eb61de4d19a5f4b9237f936250f6057cc66560 Mon Sep 17 00:00:00 2001 From: Ajay Bura <32841439+ajbura@users.noreply.github.com> Date: Fri, 14 Feb 2025 10:51:30 +0530 Subject: [PATCH] escape inline markdown character --- src/app/plugins/markdown.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/app/plugins/markdown.ts b/src/app/plugins/markdown.ts index 9b3b82f781..d46fe5fa7d 100644 --- a/src/app/plugins/markdown.ts +++ b/src/app/plugins/markdown.ts @@ -45,9 +45,10 @@ export type InlineRulesRunner = ( const MIN_ANY = '(.+?)'; const URL_NEG_LB = '(? text.match(CODE_REG_1), html: (parse, match) => { @@ -129,7 +132,7 @@ const CodeRule: InlineMDRule = { }; const SPOILER_MD_1 = '||'; -const SPOILER_PREFIX_1 = '\\|{2}'; +const SPOILER_PREFIX_1 = `${ESC_NEG_LB}\\|{2}`; const SPOILER_NEG_LA_1 = '(?!\\|)'; const SPOILER_REG_1 = new RegExp( `${URL_NEG_LB}${SPOILER_PREFIX_1}${MIN_ANY}${SPOILER_PREFIX_1}${SPOILER_NEG_LA_1}` @@ -153,6 +156,16 @@ const LinkRule: InlineMDRule = { }, }; +const ESC_SEQ_1 = '\\\\([*_~`|])'; +const ESC_REG_1 = new RegExp(`${URL_NEG_LB}${ESC_SEQ_1}`); +const EscapeRule: InlineMDRule = { + match: (text) => text.match(ESC_REG_1), + html: (parse, match) => { + const [, , g2] = match; + return g2; + }, +}; + const runInlineRule: InlineRuleRunner = (parse, text, rule) => { const matchResult = rule.match(text); if (matchResult) { @@ -200,6 +213,7 @@ const LeveledRules = [ StrikeRule, SpoilerRule, LinkRule, + EscapeRule, ]; export const parseInlineMD: InlineMDParser = (text) => {