Protect code spans when stripping HTML tags in mobile preview

Ensure HTML-like content within code spans is not erroneously removed
by the tag stripping logic. We now protect markdown code blocks and
restore them after stripping tags and decoding HTML entities.
This commit is contained in:
Jinjing
2026-06-26 23:57:10 -07:00
parent 0e89c794f4
commit d92bbd75b2
2 changed files with 8 additions and 2 deletions
@@ -80,6 +80,9 @@ describe('parseMobileMarkdown', () => {
expect(normalizeMobileMarkdownPreviewHtml('<p>Use <code>Array&lt;string&gt;</code></p>')).toBe(
'Use `Array<string>`'
)
expect(
normalizeMobileMarkdownPreviewHtml('<p>Use <code>&lt;div&gt;x&lt;/div&gt;</code></p>')
).toBe('Use `<div>x</div>`')
})
it('does not replace literal code placeholder text in markdown prose', () => {
@@ -50,8 +50,9 @@ function decodeHtmlEntities(value: string, preserveEscapedEntities = false): str
}
function stripTags(value: string): string {
return decodeHtmlEntities(
value
const { protectedText, codeSpans, placeholderPrefix } = protectMarkdownCode(value)
const stripped = decodeHtmlEntities(
protectedText
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/<\/?([A-Za-z][A-Za-z0-9:-]*)(?:\s[^<>]*)?\/?>/g, (tag, name: string) =>
strippableHtmlTagNames.has(name.toLowerCase()) ? '' : tag
@@ -61,6 +62,8 @@ function stripTags(value: string): string {
.replace(/[ \t]+\n/g, '\n')
.replace(/\n{3,}/g, '\n\n')
.trim()
return restoreMarkdownCode(stripped, codeSpans, placeholderPrefix)
}
function attrValue(tag: string, name: string): string {