From f5c281fd99001e533c37d54b240b3d5ed0bb6df5 Mon Sep 17 00:00:00 2001 From: Mayuran Visakan Date: Tue, 20 Feb 2024 01:31:38 +0000 Subject: [PATCH] Fix REGEX --- .todo | 1 + src/Parsing/InlineCodeParsing.ts | 7 +++---- tests/InlineRegex.regex | 8 ++++++++ 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 tests/InlineRegex.regex diff --git a/.todo b/.todo index 3c3125b..63db41b 100644 --- a/.todo +++ b/.todo @@ -55,6 +55,7 @@ Features: Inline Code: ☐ Add copy button via settings and parameter #88 + ☐ Add optional pandoc style marking General: Improvements: diff --git a/src/Parsing/InlineCodeParsing.ts b/src/Parsing/InlineCodeParsing.ts index 31623c2..a80ff29 100644 --- a/src/Parsing/InlineCodeParsing.ts +++ b/src/Parsing/InlineCodeParsing.ts @@ -6,14 +6,14 @@ export interface InlineCodeParameters { } export function parseInlineCode(codeText: string): {parameters: InlineCodeParameters | null, text: string} { - const match = /^{( *(?:\w+(?: +(?:(?:[^\s"']+|"[^"]*"|'[^']*')+))*)? *)} *?([^ ].*?)$/.exec(codeText); + const match = /^{((?:[^"'{}\\]|\\.|"([^"\\]|\\.)*"|'([^'\\]|\\.)*')*)} *?([^ ].*?)$/.exec(codeText); if (typeof match?.[1] !== "undefined" && typeof match?.[2] !== "undefined") { if (match[1] === "") return {parameters: null, text: match[2]}; else return {parameters: parseInlineCodeParameters(match[1]), text: match[2]}; - } else + } else return {parameters: null, text: codeText}; } function parseInlineCodeParameters(parameterLine: string): InlineCodeParameters { @@ -36,8 +36,7 @@ function parseInlineCodeParameterString(parameterString: string, inlineCodeParam if (parameterString.startsWith("title:")) { const titleMatch = /(["']?)([^\1]+)\1/.exec(parameterString.slice("title:".length)); if (titleMatch) - inlineCodeParameters.title = titleMatch[2].trim(); + inlineCodeParameters.title = titleMatch[2].trim().replace(/\\{/g, "{"); } else if (parameterString === "icon" || (parameterString.startsWith("icon:") && parameterString.toLowerCase() === "icon:true")) inlineCodeParameters.icon = true; } - diff --git a/tests/InlineRegex.regex b/tests/InlineRegex.regex new file mode 100644 index 0000000..cd0f468 --- /dev/null +++ b/tests/InlineRegex.regex @@ -0,0 +1,8 @@ +{python} 'result if true'.method() if 1 else result_if_false.property +{}{text here not styled} +{python icon} 'result if true'.method() if 1 else result_if_false.property +{python title:'Inline If'} 'result if true'.method() if 1 else result_if_false.property +{python title:'Inline If' icon} 'result if true'.method() if 1 else result_if_false.property +{js icon title:foo} javascript:(function(){alert('你好')})() +{python} import pypy; print("code" + 'g' + aaa[23]); def bfs(): return None; False #test +{ import pypy; print("code" + 'g' + aaa[23]); def bfs(): return None; False #test