fix relative pathing for dynamic fetch

This commit is contained in:
Jacky Zhao 2022-02-15 22:54:20 -05:00
parent 8e85e274f6
commit 3b3e6ec3b2
5 changed files with 63 additions and 45 deletions

View file

@ -470,7 +470,7 @@ header {
.popover { .popover {
z-index: 999; z-index: 999;
position: absolute; position: absolute;
width: 15em; width: 20em;
display: inline-block; display: inline-block;
visibility: hidden; visibility: hidden;
background-color: var(--light); background-color: var(--light);
@ -491,11 +491,18 @@ header {
} }
& > h3 { & > h3 {
margin: 0.5em 0; font-size: 1rem;
margin: 0.25em 0;
}
& > .meta {
margin-top: 0.25em;
opacity: 0.5;
} }
& > p { & > p {
margin: 0; margin: 0;
font-weight: 400; font-weight: 400;
user-select: none;
} }
} }

View file

@ -2,7 +2,7 @@ baseURL = "https://quartz.jzhao.xyz/"
languageCode = "en-us" languageCode = "en-us"
googleAnalytics = "G-XYFD95KB4J" googleAnalytics = "G-XYFD95KB4J"
pygmentsUseClasses = true pygmentsUseClasses = true
relativeURLs = true relativeURLs = false
disablePathToLower = true disablePathToLower = true
ignoreFiles = [ ignoreFiles = [
"/content/templates/*", "/content/templates/*",

View file

@ -5,5 +5,6 @@
<a href="{{ $trimmed }}" rel="noopener">{{ .Text | safeHTML }}</a> <a href="{{ $trimmed }}" rel="noopener">{{ .Text | safeHTML }}</a>
{{- else -}} {{- else -}}
{{$fixedUrl := (cond (hasPrefix $trimmed "/") $trimmed (print "/" $trimmed)) | urlize}} {{$fixedUrl := (cond (hasPrefix $trimmed "/") $trimmed (print "/" $trimmed)) | urlize}}
<a href="{{$fixedUrl}}" rel="noopener" class="internal-link" data-src="{{$fixedUrl}}">{{ .Text | safeHTML }}</a> {{$rooted := strings.TrimRight "/" (.Page.GetPage $fixedUrl).RelPermalink }}
<a href="{{$rooted}}" rel="noopener" class="internal-link" data-src="{{$rooted}}">{{ .Text | safeHTML }}</a>
{{- end -}} {{- end -}}

View file

@ -26,24 +26,33 @@
<!-- Preload page vars --> <!-- Preload page vars -->
<script> <script>
const fetchData = async () => { let saved = false
const promises = [ const fetchData = async () => {
fetch("/linkIndex.json") if (saved) {
.then(data => data.json()) return saved
.then(data => ({ } else {
index: data.index, const promises = [
links: data.links, fetch("{{ .Site.BaseURL }}/linkIndex.json")
})), .then(data => data.json())
fetch("/contentIndex.json") .then(data => ({
.then(data => data.json()), index: data.index,
] links: data.links,
const [{index, links}, content] = await Promise.all(promises) })),
return ({ fetch("{{ .Site.BaseURL }}/contentIndex.json")
index, .then(data => data.json()),
links, ]
content, const [{index, links}, content] = await Promise.all(promises)
}) const res = ({
} index,
links,
content,
})
saved = res
return res
}
}
fetchData()
</script> </script>
</head> </head>
{{ template "_internal/google_analytics.html" . }} {{ template "_internal/google_analytics.html" . }}

View file

@ -1,37 +1,38 @@
{{if $.Site.Data.config.enableLinkPreview}} {{if $.Site.Data.config.enableLinkPreview}}
<script> <script>
async function run() {
const {content} = await fetchData()
function htmlToElement(html) { function htmlToElement(html) {
const template = document.createElement('template') const template = document.createElement('template')
html = html.trim() html = html.trim()
template.innerHTML = html template.innerHTML = html
return template.content.firstChild return template.content.firstChild
} }
const baseUrl = {{strings.TrimRight "/" .Site.BaseURL }}.replace(window.location.origin, "")
const pathRegex = /\.\.?(\/\.\.)*/
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
[...document.getElementsByClassName("internal-link")] fetchData().then(({content}) => {
.forEach(li => { const links = [...document.getElementsByClassName("internal-link")]
const linkDest = content[li.dataset.src.replace(pathRegex, '')] links.forEach(li => {
if (linkDest) { console.log(li.dataset.src.replace(baseUrl, ""))
const popoverElement = `<div class="popover"> 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> <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>` </div>`
const el = htmlToElement(popoverElement) const el = htmlToElement(popoverElement)
li.appendChild(el) li.appendChild(el)
li.addEventListener("mouseover", () => { li.addEventListener("mouseover", () => {
el.classList.add("visible") el.classList.add("visible")
}) })
li.addEventListener("mouseout", () => { li.addEventListener("mouseout", () => {
el.classList.remove("visible") el.classList.remove("visible")
}) })
} }
}) })
})
}) })
}
run()
</script> </script>
{{end}} {{end}}