mirror of
https://github.com/stablyai/orca.git
synced 2026-10-30 16:47:34 +00:00
fix(docs): avoid backtracking in excerpt link destinations (#31884)
This commit is contained in:
@@ -27,8 +27,8 @@ export function stripSearchExcerptMarkdown(value) {
|
||||
})
|
||||
const linkedText = protectedCode.includes(']')
|
||||
? protectedCode
|
||||
.replace(/!\[([^\]]*)\]\((?:\\.|[^)])*\)/g, '$1')
|
||||
.replace(/\[([^\]]+)\]\((?:\\.|[^)])*\)/g, '$1')
|
||||
.replace(/!\[([^\]]*)\]\((?:\\[\s\S]|[^)\\])*\\?\)/g, '$1')
|
||||
.replace(/\[([^\]]+)\]\((?:\\[\s\S]|[^)\\])*\\?\)/g, '$1')
|
||||
.replace(/\[([^\]]+)\]\s*\[[^\]]*\]/g, '$1')
|
||||
.replace(/\[\[([^|\]]+)\|([^\]]+)\]\]/g, '$2')
|
||||
.replace(/\[\[([^\]]+)\]\]/g, '$1')
|
||||
|
||||
@@ -1,7 +1,32 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import { runInNewContext } from 'node:vm'
|
||||
import { stripSearchExcerptMarkdown } from '../src/components/docs/search-excerpt.mjs'
|
||||
|
||||
test('unterminated escaped link destinations do not backtrack exponentially', () => {
|
||||
for (const prefix of [' {
|
||||
const input = prefix + '\\('.repeat(32_768)
|
||||
const result = runInNewContext(
|
||||
'strip(input)',
|
||||
{ strip: stripSearchExcerptMarkdown, input },
|
||||
{
|
||||
timeout: 250
|
||||
}
|
||||
)
|
||||
assert.equal(result, input.replace(/\\\(/g, '('))
|
||||
}
|
||||
})
|
||||
|
||||
test('link destinations retain escaped closing parentheses and literal escapes', () => {
|
||||
for (const prefix of [' {
|
||||
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\)b)'), 'label')
|
||||
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\(b)'), 'label')
|
||||
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\)'), 'label')
|
||||
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\\\))'), 'label)')
|
||||
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\\nb)'), 'label')
|
||||
}
|
||||
})
|
||||
|
||||
test('search excerpts omit inline Markdown syntax', () => {
|
||||
assert.equal(
|
||||
stripSearchExcerptMarkdown(
|
||||
|
||||
Reference in New Issue
Block a user