Skip to content

Commit

Permalink
feat: introduce occurence param and jump to occurence
Browse files Browse the repository at this point in the history
  • Loading branch information
babslgam committed Sep 9, 2024
1 parent 38496e9 commit 2dd20c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
22 changes: 18 additions & 4 deletions wpn-utils/init-mark.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
const markInstance = new Mark(document.querySelector("#textcontent"));
const contentElement = document.querySelector("#textcontent");
const headerElm = document.querySelector("wpn-header");
const markInstance = new Mark(contentElement);
const url = new URL(window.location.href);
const urlParam = new URLSearchParams(url.search);
if (urlParam.get("mark")) {
markInstance.mark(urlParam.get("mark"));
}
const markValue = urlParam.get("mark");
const occurence = urlParam.get("occurence");
if (markValue) {
markInstance.mark(markValue,{
done: () => {
const marks = contentElement.querySelectorAll("mark");
let matchNr = 0;
if (occurence && (/\d+/g).test(occurence)) {
matchNr = Number(occurence) - 1;
}
const position = marks[matchNr].offsetTop - headerElm.clientHeight;
window.scrollTo(0, position);
}
})
};
5 changes: 3 additions & 2 deletions wpn-utils/init-typesense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ search.addWidgets([
empty: "No results for <q>{{ query }}</q>",
item(hit, { html, components }) {
const snippets = snippetsFromHighlightedContent(hit.id, hit._highlightResult.full_text.value);
console.log(snippets);

return `
<p><a class="link-black-grey text-decoration-none" href="${hit.id}">${hit.title}</a></p>
Expand Down Expand Up @@ -102,6 +101,7 @@ search.addWidgets([

const snippetsFromHighlightedContent = (hitid, highlightedContent) => {
const snippetArray = getHighlightedParts(highlightedContent);
let hitNo = 1;
return snippetArray
.map((elm, idx) => {
if (elm.isHighlighted) {
Expand All @@ -112,12 +112,13 @@ const snippetsFromHighlightedContent = (hitid, highlightedContent) => {
.slice(1)
.slice(-10)
.join("");
snippet += `<a href="${hitid}?mark=${elm.value}" class="link-primary text-decoration-none ff-ubuntu">${elm.value}</a>`;
snippet += `<a href="${hitid}?mark=${elm.value}&occurence=${String(hitNo)}" class="link-primary text-decoration-none ff-ubuntu">${elm.value}</a>`;
snippet += Array.from(segmenter.segment(snippetArray[idx + 1]?.value))
.map((seg) => seg.segment)
.slice(0, 10)
.join("");
snippet += "&#x2026;</p>";
hitNo++;
return snippet;
}
})
Expand Down

0 comments on commit 2dd20c5

Please sign in to comment.