Skip to content

Commit 9d35b6f

Browse files
authored
Merge pull request #373 from wise-king-sullyman/fix-marked-code-fences
fix(markdown): fix plain code fences not being transformed
2 parents 8511ce3 + 5a2bf37 commit 9d35b6f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,20 @@ export const markdownConvert = async (markdown: string, extensions?: ShowdownExt
7171
}
7272
});
7373

74-
// Replace code fences with non markdown formatting relates tokens so that marked doesn't try to parse them as code spans
75-
const markdownWithSubstitutedCodeFences = markdown.replace(/```/g, '@@@');
74+
const reverseString = (str: string) => str.split('').reverse().join('');
75+
76+
// replace code fences that end in a double curly brace (which are used by our custom md extensions) with non
77+
// markdown formatting related tokens so that marked doesn't try to parse them as code spans
78+
//
79+
// we want to reverse the string before we do the substitution so that we only match the opening code fence which
80+
// corresponds to the closing code fence with the double curly brace
81+
const reversedMarkdown = reverseString(markdown);
82+
const reverseMarkdownWithSubstitutedCodeFences = reversedMarkdown.replace(
83+
/{{```((.|\n)*?)```/g,
84+
'{{@@@$1@@@',
85+
);
86+
const markdownWithSubstitutedCodeFences = reverseString(reverseMarkdownWithSubstitutedCodeFences);
87+
7688
const parsedMarkdown = await marked.parse(markdownWithSubstitutedCodeFences);
7789
// Swap the temporary tokens back to code fences before we run the extensions
7890
let md = parsedMarkdown.replace(/@@@/g, '```');

0 commit comments

Comments
 (0)