2022-03-04 02:07:51 +00:00
|
|
|
function htmlToElement(html) {
|
2022-05-03 15:47:42 +00:00
|
|
|
const template = document.createElement("template")
|
2022-04-02 19:59:38 +00:00
|
|
|
html = html.trim()
|
|
|
|
template.innerHTML = html
|
|
|
|
return template.content.firstChild
|
2022-03-04 02:07:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 05:03:12 +00:00
|
|
|
function initPopover(baseURL, useContextualBacklinks) {
|
2022-04-02 19:59:38 +00:00
|
|
|
const basePath = baseURL.replace(window.location.origin, "")
|
2022-05-03 15:47:42 +00:00
|
|
|
fetchData.then(({ content }) => {
|
|
|
|
const links = [...document.getElementsByClassName("internal-link")]
|
|
|
|
links
|
2022-05-05 05:03:09 +00:00
|
|
|
.filter(li => li.dataset.src || (li.dataset.idx && useContextualBacklinks))
|
|
|
|
.forEach(li => {
|
2022-12-29 15:43:41 +00:00
|
|
|
let el
|
2022-05-05 05:03:09 +00:00
|
|
|
if (li.dataset.ctx) {
|
|
|
|
const linkDest = content[li.dataset.src]
|
2022-05-03 15:47:42 +00:00
|
|
|
const popoverElement = `<div class="popover">
|
2022-03-04 02:07:51 +00:00
|
|
|
<h3>${linkDest.title}</h3>
|
2022-05-05 04:58:50 +00:00
|
|
|
<p>${highlight(removeMarkdown(linkDest.content), li.dataset.ctx)}...</p>
|
2022-03-04 02:07:51 +00:00
|
|
|
<p class="meta">${new Date(linkDest.lastmodified).toLocaleDateString()}</p>
|
|
|
|
</div>`
|
2022-05-06 00:30:55 +00:00
|
|
|
el = htmlToElement(popoverElement)
|
2022-05-05 05:03:09 +00:00
|
|
|
} else {
|
|
|
|
const linkDest = content[li.dataset.src.replace(/\/$/g, "").replace(basePath, "")]
|
|
|
|
if (linkDest) {
|
2022-08-21 01:31:06 +00:00
|
|
|
let splitLink = li.href.split("#")
|
|
|
|
let cleanedContent = removeMarkdown(linkDest.content)
|
|
|
|
if (splitLink.length > 1) {
|
2022-10-12 15:21:28 +00:00
|
|
|
let headingName = decodeURIComponent(splitLink[1]).replace(/\-/g, " ")
|
2022-08-21 01:31:06 +00:00
|
|
|
let headingIndex = cleanedContent.toLowerCase().indexOf("<b>" + headingName + "</b>")
|
|
|
|
cleanedContent = cleanedContent.substring(headingIndex, cleanedContent.length)
|
|
|
|
}
|
2022-05-05 05:03:09 +00:00
|
|
|
const popoverElement = `<div class="popover">
|
|
|
|
<h3>${linkDest.title}</h3>
|
2023-06-18 08:24:54 +00:00
|
|
|
<p>${cleanedContent.split("\n", 5).join(" | ")}...</p>
|
2022-05-05 05:03:09 +00:00
|
|
|
<p class="meta">${new Date(linkDest.lastmodified).toLocaleDateString()}</p>
|
2022-03-04 02:07:51 +00:00
|
|
|
</div>`
|
2022-05-06 00:30:55 +00:00
|
|
|
el = htmlToElement(popoverElement)
|
2022-04-06 03:44:39 +00:00
|
|
|
}
|
2022-05-03 15:47:42 +00:00
|
|
|
}
|
2022-05-06 01:11:23 +00:00
|
|
|
|
|
|
|
if (el) {
|
|
|
|
li.appendChild(el)
|
2022-12-04 05:03:12 +00:00
|
|
|
if (LATEX_ENABLED) {
|
2022-05-06 01:11:23 +00:00
|
|
|
renderMathInElement(el, {
|
|
|
|
delimiters: [
|
|
|
|
{ left: '$$', right: '$$', display: false },
|
|
|
|
{ left: '$', right: '$', display: false },
|
|
|
|
],
|
|
|
|
throwOnError: false
|
|
|
|
})
|
|
|
|
}
|
2022-07-13 22:01:50 +00:00
|
|
|
|
2022-05-06 01:11:23 +00:00
|
|
|
li.addEventListener("mouseover", () => {
|
2022-07-13 22:01:50 +00:00
|
|
|
// fix tooltip positioning
|
|
|
|
window.FloatingUIDOM.computePosition(li, el, {
|
2022-07-14 06:37:54 +00:00
|
|
|
middleware: [window.FloatingUIDOM.offset(10), window.FloatingUIDOM.inline(), window.FloatingUIDOM.shift()],
|
2022-07-13 22:01:50 +00:00
|
|
|
}).then(({ x, y }) => {
|
|
|
|
Object.assign(el.style, {
|
|
|
|
left: `${x}px`,
|
|
|
|
top: `${y}px`,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-05-06 01:11:23 +00:00
|
|
|
el.classList.add("visible")
|
2023-02-05 19:34:39 +00:00
|
|
|
plausible("Popover Hover", {
|
|
|
|
props: {
|
|
|
|
href: li.dataset.src
|
|
|
|
}
|
|
|
|
})
|
2022-05-06 01:11:23 +00:00
|
|
|
})
|
|
|
|
li.addEventListener("mouseout", () => {
|
|
|
|
el.classList.remove("visible")
|
2022-05-06 00:30:55 +00:00
|
|
|
})
|
|
|
|
}
|
2022-05-03 15:47:42 +00:00
|
|
|
})
|
2022-04-02 19:59:38 +00:00
|
|
|
})
|
2022-03-04 02:07:51 +00:00
|
|
|
}
|