refactor: Escape HTML tags before markdown rendering

This commit is contained in:
Christian Kußowski 2025-11-04 14:57:30 +01:00
parent 9e26e5087a
commit c9f8ece8d4
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652
2 changed files with 11 additions and 1 deletions

View File

@ -212,7 +212,13 @@ String markdown(
bool convertLinebreaks = true, bool convertLinebreaks = true,
}) { }) {
var ret = markdownToHtml( var ret = markdownToHtml(
text.replaceNewlines(), text
.replaceAllMapped(
// Replace HTML tags
RegExp(r'<([^>]*)>'),
(match) => '&lt;${match.group(1)}&gt;',
)
.replaceNewlines(),
extensionSet: ExtensionSet.gitHubFlavored, extensionSet: ExtensionSet.gitHubFlavored,
blockSyntaxes: [ blockSyntaxes: [
BlockLatexSyntax(), BlockLatexSyntax(),

View File

@ -220,6 +220,10 @@ void main() {
), ),
'<p>The first<br/>codeblock</p><pre><code class="language-dart">void main(){\nprint(something);\n}\n</code></pre><p>And the second code block</p><pre><code class="language-js">meow\nmeow\n</code></pre>', '<p>The first<br/>codeblock</p><pre><code class="language-dart">void main(){\nprint(something);\n}\n</code></pre><p>And the second code block</p><pre><code class="language-js">meow\nmeow\n</code></pre>',
); );
expect(
markdown('Test <m> *unescaped*'),
'Test &lt;m&gt; <em>unescaped</em>',
);
}); });
test('Checkboxes', () { test('Checkboxes', () {
expect( expect(