Skip to content

Commit 7a76706

Browse files
authored
Add text fragments to quarto-search.js items (#14003)
1 parent d67f982 commit 7a76706

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/resources/projects/website/search/quarto-search.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,22 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
356356
},
357357

358358
item({ item, createElement }) {
359+
// process items to include text fragments as they are rendered
360+
if (item.text && item.href && !item.href.includes(':~:text=')) {
361+
// e.g. `item.text` for a search "def fiz": "bla bla bla<mark class='search-match'>def fiz</mark> bla bla"
362+
const fullMatches = item.text.matchAll(/<mark class='search-match'>(.*?)<\/mark>/g)
363+
// extract capture group with the search match
364+
// result e.g. ["def fiz"]
365+
const searchMatches = [...fullMatches].map(match => match[1])
366+
if (searchMatches[0]) {
367+
if (item.href.includes('#')) {
368+
item.href += ':~:text=' + encodeURIComponent(searchMatches[0])
369+
} else {
370+
item.href += '#:~:text=' + encodeURIComponent(searchMatches[0])
371+
}
372+
}
373+
}
374+
359375
return renderItem(
360376
item,
361377
createElement,

0 commit comments

Comments
 (0)