Skip to content

Commit a6908ee

Browse files
fix(markdown): fix plain code fences not being transformed (#375)
* fix(markdown): fix plain code fences not being transformed * lint
1 parent b846844 commit a6908ee

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

packages/module/src/ConsoleInternal/components/markdown-view.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,24 @@ export const markdownConvert = async (markdown: string, extensions?: ShowdownExt
5151
}
5252
});
5353

54-
// Replace code fences with non markdown formatting relates tokens so that marked doesn't try to parse them as code spans
55-
const markdownWithSubstitutedCodeFences = markdown.replace(/```/g, '@@@');
54+
const reverseString = (str: string) =>
55+
str
56+
.split('')
57+
.reverse()
58+
.join('');
59+
60+
// replace code fences that end in a double curly brace (which are used by our custom md extensions) with non
61+
// markdown formatting related tokens so that marked doesn't try to parse them as code spans
62+
//
63+
// we want to reverse the string before we do the substitution so that we only match the opening code fence which
64+
// corresponds to the closing code fence with the double curly brace
65+
const reversedMarkdown = reverseString(markdown);
66+
const reverseMarkdownWithSubstitutedCodeFences = reversedMarkdown.replace(
67+
/{{```((.|\n)*?)```/g,
68+
'{{@@@$1@@@',
69+
);
70+
const markdownWithSubstitutedCodeFences = reverseString(reverseMarkdownWithSubstitutedCodeFences);
71+
5672
const parsedMarkdown = await marked.parse(markdownWithSubstitutedCodeFences);
5773
// Swap the temporary tokens back to code fences before we run the extensions
5874
let md = parsedMarkdown.replace(/@@@/g, '```');

0 commit comments

Comments
 (0)