Upstream spencermountain/compromise state of the touched files before a9042f8c5f

This commit is contained in:
upstream committed 2026-10-14 19:36:06 +02:00
1 parent 398b3d626c
commit 219f140a9d
2 files changed
+84

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
const toText = function (term) {
const pre = term.pre || ''
const post = term.post || ''
return pre + term.text + post
}
const findStarts = function (doc, obj) {
const starts = {}
Object.keys(obj).forEach(reg => {
const m = doc.match(reg)
m.fullPointer.forEach(a => {
starts[a[3]] = { fn: obj[reg], end: a[2] }
})
})
return starts
}
const wrap = function (doc, obj) {
// index ids to highlight
const starts = findStarts(doc, obj)
let text = ''
doc.docs.forEach((terms, n) => {
for (let i = 0; i < terms.length; i += 1) {
const t = terms[i]
// do a span tag
if (Object.hasOwn(starts, t.id)) {
const { fn, end } = starts[t.id]
const m = doc.update([[n, i, end]])
text += terms[i].pre || ''
text += fn(m)
i = end - 1
text += terms[i].post || ''
} else {
text += toText(t)
}
}
})
return text
}
export default wrap