perf: skip unclosed suffixes when stripping review markdown tags (#20329)

This commit is contained in:
Neil
2026-09-12 18:19:16 -07:00
committed by GitHub
parent b1c6d53e90
commit fbab61ec09
2 changed files with 55 additions and 1 deletions
@@ -44,7 +44,14 @@ const SUMMARY = /<summary\b[^>]*>([\s\S]*?)<\/summary>/i
// show literally. Conservative: only matches `<tag ...>` / `</tag>` shapes, so a bare
// "a < b" in prose is left alone.
export function stripHtmlTags(text: string): string {
return text.replace(/<\/?[a-zA-Z][a-zA-Z0-9-]*(?:\s[^>]*)?\/?>/g, '')
const end = text.lastIndexOf('>') + 1
if (end === 0) {
return text
}
// No tag can close in this suffix; keep it literal without retrying every opener.
return (
text.slice(0, end).replace(/<\/?[a-zA-Z][a-zA-Z0-9-]*(?:\s[^>]*)?\/?>/g, '') + text.slice(end)
)
}
export function parseMarkdownBlocks(content: string): MarkdownBlock[] {
@@ -0,0 +1,47 @@
import { runInNewContext } from 'node:vm'
import { describe, expect, it } from 'vitest'
import { parseInline, stripHtmlTags } from './markdown-blocks'
const ORIGINAL_TAGS = /<\/?[a-zA-Z][a-zA-Z0-9-]*(?:\s[^>]*)?\/?>/g
describe('review Markdown unclosed tags', () => {
it.each(['', '<b>before</b> '])('preserves an unclosed suffix after %s', (prefix) => {
const suffix = '<a '.repeat(20_000)
const text = prefix + suffix
const result = runInNewContext('parse(text)', { parse: parseInline, text }, { timeout: 250 })
expect(result).toEqual([{ kind: 'text', text: prefix.replace(ORIGINAL_TAGS, '') + suffix }])
})
it('preserves the original stripping grammar over generated markup', () => {
const parts = [
'<a ',
'<b>',
'</b>',
'<a href="x">',
'<custom-tag x>',
'<',
'>',
'a',
' ',
'/',
'\n',
'"',
'<1>',
'<a/>',
'<a x<',
'<a'
]
let seed = 17
const random = () => {
seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0
return seed
}
for (let index = 0; index < 5000; index++) {
const text = Array.from(
{ length: 1 + (random() % 40) },
() => parts[random() % parts.length]
).join('')
expect(stripHtmlTags(text), text).toBe(text.replace(ORIGINAL_TAGS, ''))
}
})
})