mirror of
https://github.com/stablyai/orca.git
synced 2026-09-23 08:02:31 +00:00
perf: stop heading lookup at the requested occurrence (#20246)
Co-authored-by: Orca Worker <orca-worker@localhost>
This commit is contained in:
@@ -14,6 +14,14 @@ function makeTocItem(
|
||||
}
|
||||
|
||||
describe('findRichMarkdownTocHeadingTarget', () => {
|
||||
it('returns undefined when the requested duplicate occurrence is missing', () => {
|
||||
const container = document.createElement('div')
|
||||
container.innerHTML = '<h2>Repeat</h2><h2>Other</h2>'
|
||||
const items = [makeTocItem('repeat', 2, 'Repeat'), makeTocItem('repeat-1', 2, 'Repeat')]
|
||||
expect(findRichMarkdownTocHeadingTarget(container, items, 'repeat-1')).toBeUndefined()
|
||||
expect(findRichMarkdownTocHeadingTarget(container, items, 'missing')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('finds deep headings that the table of contents exposes', () => {
|
||||
const container = document.createElement('div')
|
||||
container.innerHTML = `
|
||||
|
||||
@@ -18,9 +18,16 @@ export function findRichMarkdownTocHeadingTarget(
|
||||
const sameTitleIndex = items
|
||||
.filter((item) => item.title === target.title)
|
||||
.findIndex((item) => item.id === target.id)
|
||||
const matchingHeadings = Array.from(
|
||||
container.querySelectorAll<HTMLElement>(RICH_MARKDOWN_TOC_HEADING_SELECTOR)
|
||||
).filter((candidate) => candidate.textContent?.trim() === target.title)
|
||||
|
||||
return matchingHeadings.at(Math.max(0, sameTitleIndex))
|
||||
let remaining = Math.max(0, sameTitleIndex)
|
||||
for (const candidate of container.querySelectorAll<HTMLElement>(
|
||||
RICH_MARKDOWN_TOC_HEADING_SELECTOR
|
||||
)) {
|
||||
if (candidate.textContent?.trim() === target.title) {
|
||||
if (remaining === 0) {
|
||||
return candidate
|
||||
}
|
||||
remaining -= 1
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user