forked from saberhaoren/haoren
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix relative pathing for dynamic fetch
- Loading branch information
Showing
5 changed files
with
63 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,38 @@ | ||
{{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 = /\.\.?(\/\.\.)*/ | ||
const baseUrl = {{strings.TrimRight "/" .Site.BaseURL }}.replace(window.location.origin, "") | ||
document.addEventListener("DOMContentLoaded", () => { | ||
[...document.getElementsByClassName("internal-link")] | ||
.forEach(li => { | ||
const linkDest = content[li.dataset.src.replace(pathRegex, '')] | ||
if (linkDest) { | ||
const popoverElement = `<div class="popover"> | ||
fetchData().then(({content}) => { | ||
const links = [...document.getElementsByClassName("internal-link")] | ||
links.forEach(li => { | ||
console.log(li.dataset.src.replace(baseUrl, "")) | ||
console.log(content[li.dataset.src.replace(baseUrl, "")]) | ||
console.log(content) | ||
const linkDest = content[li.dataset.src.replace(baseUrl, "")] | ||
// const linkDest = content[li.dataset.src] | ||
if (linkDest) { | ||
const popoverElement = `<div class="popover"> | ||
<h3>${linkDest.title}</h3> | ||
<p>${removeMarkdown(linkDest.content).split(" ", 15).join(" ")}...</p> | ||
<p>${removeMarkdown(linkDest.content).split(" ", 20).join(" ")}...</p> | ||
<p class="meta">${new Date(linkDest.lastmodified).toLocaleDateString()}</p> | ||
</div>` | ||
const el = htmlToElement(popoverElement) | ||
li.appendChild(el) | ||
li.addEventListener("mouseover", () => { | ||
el.classList.add("visible") | ||
}) | ||
li.addEventListener("mouseout", () => { | ||
el.classList.remove("visible") | ||
}) | ||
} | ||
}) | ||
const el = htmlToElement(popoverElement) | ||
li.appendChild(el) | ||
li.addEventListener("mouseover", () => { | ||
el.classList.add("visible") | ||
}) | ||
li.addEventListener("mouseout", () => { | ||
el.classList.remove("visible") | ||
}) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
run() | ||
</script> | ||
{{end}} |