2022-03-04 05:07:51 +03:00
|
|
|
function htmlToElement(html) {
|
2022-05-03 18:47:42 +03:00
|
|
|
const template = document.createElement("template")
|
2022-04-02 22:59:38 +03:00
|
|
|
html = html.trim()
|
|
|
|
template.innerHTML = html
|
|
|
|
return template.content.firstChild
|
2022-03-04 05:07:51 +03:00
|
|
|
}
|
|
|
|
|
2022-05-05 07:58:50 +03:00
|
|
|
function initPopover(baseURL, useContextualBacklinks) {
|
2022-04-02 22:59:38 +03:00
|
|
|
const basePath = baseURL.replace(window.location.origin, "")
|
2022-05-03 18:47:42 +03:00
|
|
|
fetchData.then(({ content }) => {
|
|
|
|
const links = [...document.getElementsByClassName("internal-link")]
|
|
|
|
links
|
2022-05-05 08:03:09 +03:00
|
|
|
.filter(li => li.dataset.src || (li.dataset.idx && useContextualBacklinks))
|
|
|
|
.forEach(li => {
|
|
|
|
if (li.dataset.ctx) {
|
|
|
|
const linkDest = content[li.dataset.src]
|
2022-05-03 18:47:42 +03:00
|
|
|
const popoverElement = `<div class="popover">
|
2022-03-04 05:07:51 +03:00
|
|
|
<h3>${linkDest.title}</h3>
|
2022-05-05 07:58:50 +03:00
|
|
|
<p>${highlight(removeMarkdown(linkDest.content), li.dataset.ctx)}...</p>
|
2022-03-04 05:07:51 +03:00
|
|
|
<p class="meta">${new Date(linkDest.lastmodified).toLocaleDateString()}</p>
|
|
|
|
</div>`
|
2022-05-03 18:47:42 +03:00
|
|
|
const el = htmlToElement(popoverElement)
|
|
|
|
li.appendChild(el)
|
|
|
|
li.addEventListener("mouseover", () => {
|
|
|
|
el.classList.add("visible")
|
|
|
|
})
|
|
|
|
li.addEventListener("mouseout", () => {
|
|
|
|
el.classList.remove("visible")
|
|
|
|
})
|
2022-05-05 08:03:09 +03: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 05:07:51 +03:00
|
|
|
</div>`
|
2022-04-06 06:44:39 +03:00
|
|
|
const el = htmlToElement(popoverElement)
|
|
|
|
li.appendChild(el)
|
|
|
|
li.addEventListener("mouseover", () => {
|
|
|
|
el.classList.add("visible")
|
|
|
|
})
|
|
|
|
li.addEventListener("mouseout", () => {
|
|
|
|
el.classList.remove("visible")
|
|
|
|
})
|
|
|
|
}
|
2022-05-03 18:47:42 +03:00
|
|
|
}
|
|
|
|
})
|
2022-04-02 22:59:38 +03:00
|
|
|
})
|
2022-03-04 05:07:51 +03:00
|
|
|
}
|