Skip to content

Commit

Permalink
Fixed h2 anchor generation
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSpaar committed Aug 4, 2024
1 parent 2a94029 commit 036d706
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
63 changes: 30 additions & 33 deletions doc/static/markdown_spa.js
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);
}
});
37 changes: 20 additions & 17 deletions doc/static/script.js
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;
}

0 comments on commit 036d706

Please sign in to comment.