-
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.
- Loading branch information
Showing
2 changed files
with
50 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,44 @@ | ||
if (typeof preFetch !== 'function') { function preFetch() {} } | ||
if (typeof postFetch !== 'function') { function postFetch() {} } | ||
|
||
async function updatePage(path) { | ||
preFetch(); | ||
preFetch(); | ||
|
||
if (typeof updateWithJSON === 'function') { | ||
updateWithJSON(await fetch(path+'index.json').then(r => r.json())); | ||
} else { | ||
let text = await fetch(path).then(r => r.text()); | ||
document.documentElement.innerHTML = text; | ||
} | ||
if (typeof updateWithJSON === "function") { | ||
updateWithJSON(await fetch(path + "index.json").then((r) => r.json())); | ||
} else { | ||
let text = await fetch(path).then((r) => r.text()); | ||
document.documentElement.innerHTML = text; | ||
} | ||
|
||
postFetch(); | ||
postFetch(); | ||
} | ||
|
||
postFetch(); | ||
let curPath = window.location.pathname; | ||
|
||
window.addEventListener('popstate', async _ => { | ||
if (window.location.pathname == curPath) { | ||
return; | ||
} | ||
window.addEventListener("popstate", async (_) => { | ||
if (window.location.pathname == curPath) { | ||
return; | ||
} | ||
|
||
curPath = window.location.pathname; | ||
await updatePage(window.location.href); | ||
curPath = window.location.pathname; | ||
await updatePage(window.location.href); | ||
}); | ||
|
||
window.addEventListener('click', e => { | ||
let targetAnchor = e.target.closest('a'); | ||
if (!targetAnchor || !targetAnchor.hasAttribute("href")) { | ||
return; | ||
} | ||
window.addEventListener("click", (e) => { | ||
let targetAnchor = e.target.closest("a"); | ||
if (!targetAnchor || !targetAnchor.hasAttribute("href")) { | ||
return; | ||
} | ||
|
||
href = (targetAnchor) ? targetAnchor.href : e.target.href; | ||
e.preventDefault(); | ||
href = targetAnchor ? targetAnchor.href : e.target.href; | ||
e.preventDefault(); | ||
|
||
if (href == window.location.href) { | ||
return; | ||
} | ||
if (href == window.location.href) { | ||
return; | ||
} | ||
|
||
if (href.startsWith(window.location.origin)) { | ||
e.preventDefault(); | ||
updatePage(href); | ||
window.history.pushState({}, '', href); | ||
} | ||
}) | ||
if (href.startsWith(window.location.origin)) { | ||
e.preventDefault(); | ||
updatePage(href); | ||
window.history.pushState({}, "", href); | ||
} | ||
}); |
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,28 +1,31 @@ | ||
let loader = document.getElementById('loader'); | ||
let input = document.getElementById('show-nav') | ||
let loader = document.getElementById("loader"); | ||
let input = document.getElementById("show-nav"); | ||
|
||
function preFetch() { | ||
loader.classList.add('active'); | ||
loader.classList.add("active"); | ||
} | ||
|
||
function updateWithJSON(json) { | ||
document.title = `Markdown SPA - ${json.name}`; | ||
document.querySelector('meta[name="description"]').content = json.description; | ||
document.querySelector('#content').innerHTML = `<h1>${json.name}</h1>${json.page_content}` | ||
document.title = `Markdown SPA - ${json.name}`; | ||
document.querySelector('meta[name="description"]').content = json.description; | ||
document.querySelector("#content").innerHTML = | ||
`<h1>${json.name}</h1>${json.page_content}`; | ||
} | ||
|
||
function postFetch() { | ||
for (let h2 of document.getElementsByName('h2')) { | ||
h2.classList.add('anchor'); | ||
h2.id = h2.innerText.toLowerCase().replace(/[^a-z0-9]/g, '-'); | ||
for (let h2 of document.getElementsByTagName("h2")) { | ||
h2.classList.add("anchor"); | ||
h2.id = h2.innerText.toLowerCase().replace(/[^a-z0-9]/g, "-"); | ||
|
||
h2.addEventListener('click', e => { | ||
e.target.scrollIntoView(); | ||
history.pushState({}, '', `#${h2.id}`); | ||
navigator.clipboard.writeText(`${window.location.origin}${window.location.pathname}#${h2.id}`); | ||
}); | ||
} | ||
h2.addEventListener("click", (e) => { | ||
e.target.scrollIntoView(); | ||
history.pushState({}, "", `#${h2.id}`); | ||
navigator.clipboard.writeText( | ||
`${window.location.origin}${window.location.pathname}#${h2.id}`, | ||
); | ||
}); | ||
} | ||
|
||
loader.classList.remove('active'); | ||
input.checked = false; | ||
loader.classList.remove("active"); | ||
input.checked = false; | ||
} |