feat: support Zola's native code block names (#489)

This commit is contained in:
Óscar 2025-02-15 22:31:55 +01:00 committed by GitHub
parent 6837f6f4f1
commit 61793b2e56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 228 additions and 195 deletions

View file

@ -1,20 +0,0 @@
document.addEventListener("DOMContentLoaded", function() {
document.querySelectorAll('.code-source').forEach(function(marker) {
let sourceUrl = marker.getAttribute('data-source');
let nextPre = marker.nextElementSibling;
if (nextPre && nextPre.tagName === 'PRE') {
let codeElement = nextPre.querySelector('code');
if (codeElement) {
// Use a span element for the source path if it's not a link.
let sourceElement = document.createElement(sourceUrl.startsWith('http') ? 'a' : 'span');
sourceElement.textContent = sourceUrl;
sourceElement.className = 'source-path';
if (sourceUrl.startsWith('http')) {
sourceElement.href = sourceUrl;
}
codeElement.prepend(sourceElement);
}
}
});
});

View file

@ -1 +0,0 @@
document.addEventListener("DOMContentLoaded",function(){document.querySelectorAll(".code-source").forEach(function(t){var e,n=t.getAttribute("data-source"),t=t.nextElementSibling;t&&"PRE"===t.tagName&&(t=t.querySelector("code"))&&((e=document.createElement(n.startsWith("http")?"a":"span")).textContent=n,e.className="source-path",n.startsWith("http")&&(e.href=n),t.prepend(e))})});

View file

@ -0,0 +1,36 @@
document.addEventListener("DOMContentLoaded", function() {
// Convert URLs in data-name to links.
document.querySelectorAll('code[data-name]').forEach(function(code) {
const name = code.getAttribute('data-name');
if (name.startsWith('http')) {
const link = document.createElement('a');
link.href = name;
link.className = 'source-path';
link.textContent = name;
code.insertBefore(link, code.firstChild);
// Remove data-name to avoid overlap with Zola's native display.
code.removeAttribute('data-name');
code.parentElement?.removeAttribute('data-name');
}
});
// Legacy support for old shortcode. https://github.com/welpo/tabi/pull/489
document.querySelectorAll('.code-source').forEach(function(marker) {
const sourceUrl = marker.getAttribute('data-source');
const nextPre = marker.nextElementSibling;
if (nextPre?.tagName === 'PRE') {
const code = nextPre.querySelector('code');
if (code) {
if (sourceUrl.startsWith('http')) {
const link = document.createElement('a');
link.href = sourceUrl;
link.className = 'source-path';
link.textContent = sourceUrl;
code.insertBefore(link, code.firstChild);
} else {
code.setAttribute('data-name', sourceUrl);
}
}
}
});
});

1
static/js/codeBlockNameLinks.min.js vendored Normal file
View file

@ -0,0 +1 @@
document.addEventListener("DOMContentLoaded",function(){document.querySelectorAll("code[data-name]").forEach(function(e){var t,a=e.getAttribute("data-name");a.startsWith("http")&&((t=document.createElement("a")).href=a,t.className="source-path",t.textContent=a,e.insertBefore(t,e.firstChild),e.removeAttribute("data-name"),e.parentElement?.removeAttribute("data-name"))}),document.querySelectorAll(".code-source").forEach(function(e){var t,a=e.getAttribute("data-source");"PRE"===(e=e.nextElementSibling)?.tagName&&(e=e.querySelector("code"))&&(a.startsWith("http")?((t=document.createElement("a")).href=a,t.className="source-path",t.textContent=a,e.insertBefore(t,e.firstChild)):e.setAttribute("data-name",a))})});