Jacky Zhao
2022-02-16 fcd5d2807d2bab68c6776e031e85d65fe88a6f7a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{{if $.Site.Data.config.enableLinkPreview}}
<script>
async function run() {
  const {content} = await fetchData()
  function htmlToElement(html) {
    const template = document.createElement('template')
    html = html.trim()
    template.innerHTML = html
    return template.content.firstChild
  }
 
  const pathRegex = /\.\.?(\/\.\.)*/
  document.addEventListener("DOMContentLoaded", () => {
    [...document.getElementsByClassName("internal-link")]
      .forEach(li => {
        const linkDest = content[li.dataset.src.replace(pathRegex, '')]
        if (linkDest) {
          const popoverElement = `<div class="popover">
    <h3>${linkDest.title}</h3>
    <p>${removeMarkdown(linkDest.content).split(" ", 15).join(" ")}...</p>
</div>`
          const el = htmlToElement(popoverElement)
          li.appendChild(el)
          li.addEventListener("mouseover", () => {
            el.classList.add("visible")
          })
          li.addEventListener("mouseout", () => {
            el.classList.remove("visible")
          })
        }
      })
  })
}
 
run()
</script>
{{end}}