2022-03-04 09:07:51 +07:00
|
|
|
function htmlToElement(html) {
|
2022-05-03 22:47:42 +07:00
|
|
|
const template = document.createElement("template")
|
2022-04-03 02:59:38 +07:00
|
|
|
html = html.trim()
|
|
|
|
template.innerHTML = html
|
|
|
|
return template.content.firstChild
|
2022-03-04 09:07:51 +07:00
|
|
|
}
|
|
|
|
|
2022-05-06 07:30:55 +07:00
|
|
|
function initPopover(baseURL, useContextualBacklinks, renderLatex) {
|
2022-04-03 02:59:38 +07:00
|
|
|
const basePath = baseURL.replace(window.location.origin, "")
|
2022-05-03 22:47:42 +07:00
|
|
|
fetchData.then(({ content }) => {
|
|
|
|
const links = [...document.getElementsByClassName("internal-link")]
|
|
|
|
links
|
2022-05-05 12:03:09 +07:00
|
|
|
.filter(li => li.dataset.src || (li.dataset.idx && useContextualBacklinks))
|
|
|
|
.forEach(li => {
|
2022-05-06 07:30:55 +07:00
|
|
|
var el
|
2022-05-05 12:03:09 +07:00
|
|
|
if (li.dataset.ctx) {
|
|
|
|
const linkDest = content[li.dataset.src]
|
2022-05-03 22:47:42 +07:00
|
|
|
const popoverElement = `<div class="popover">
|
2022-03-04 09:07:51 +07:00
|
|
|
<h3>${linkDest.title}</h3>
|
2022-05-05 11:58:50 +07:00
|
|
|
<p>${highlight(removeMarkdown(linkDest.content), li.dataset.ctx)}...</p>
|
2022-03-04 09:07:51 +07:00
|
|
|
<p class="meta">${new Date(linkDest.lastmodified).toLocaleDateString()}</p>
|
|
|
|
</div>`
|
2022-05-06 07:30:55 +07:00
|
|
|
el = htmlToElement(popoverElement)
|
2022-05-05 12:03:09 +07:00
|
|
|
} else {
|
|
|
|
const linkDest = content[li.dataset.src.replace(/\/$/g, "").replace(basePath, "")]
|
|
|
|
if (linkDest) {
|
|
|
|
const popoverElement = `<div class="popover">
|
|
|
|
<h3>${linkDest.title}</h3>
|
|
|
|
<p>${removeMarkdown(linkDest.content).split(" ", 20).join(" ")}...</p>
|
|
|
|
<p class="meta">${new Date(linkDest.lastmodified).toLocaleDateString()}</p>
|
2022-03-04 09:07:51 +07:00
|
|
|
</div>`
|
2022-05-06 07:30:55 +07:00
|
|
|
el = htmlToElement(popoverElement)
|
2022-04-06 10:44:39 +07:00
|
|
|
}
|
2022-05-03 22:47:42 +07:00
|
|
|
}
|
2022-05-06 08:11:23 +07:00
|
|
|
|
|
|
|
if (el) {
|
|
|
|
li.appendChild(el)
|
|
|
|
if (renderLatex) {
|
|
|
|
renderMathInElement(el, {
|
|
|
|
delimiters: [
|
|
|
|
{ left: '$$', right: '$$', display: false },
|
|
|
|
{ left: '$', right: '$', display: false },
|
|
|
|
{ left: '\\(', right: '\\)', display: false },
|
|
|
|
{ left: '\\[', right: '\\]', display: false }
|
|
|
|
],
|
|
|
|
throwOnError: false
|
|
|
|
})
|
|
|
|
}
|
2022-07-14 05:01:50 +07:00
|
|
|
|
2022-05-06 08:11:23 +07:00
|
|
|
li.addEventListener("mouseover", () => {
|
2022-07-14 05:01:50 +07:00
|
|
|
// fix tooltip positioning
|
|
|
|
window.FloatingUIDOM.computePosition(li, el, {
|
2022-07-14 13:37:54 +07:00
|
|
|
middleware: [window.FloatingUIDOM.offset(10), window.FloatingUIDOM.inline(), window.FloatingUIDOM.shift()],
|
2022-07-14 05:01:50 +07:00
|
|
|
}).then(({ x, y }) => {
|
|
|
|
Object.assign(el.style, {
|
|
|
|
left: `${x}px`,
|
|
|
|
top: `${y}px`,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-05-06 08:11:23 +07:00
|
|
|
el.classList.add("visible")
|
|
|
|
})
|
|
|
|
li.addEventListener("mouseout", () => {
|
|
|
|
el.classList.remove("visible")
|
2022-05-06 07:30:55 +07:00
|
|
|
})
|
|
|
|
}
|
2022-05-03 22:47:42 +07:00
|
|
|
})
|
2022-04-03 02:59:38 +07:00
|
|
|
})
|
2022-03-04 09:07:51 +07:00
|
|
|
}
|