Repository navigation
Expand file tree
/
Copy pathDhammapada.tsx
More file actions
69 lines (56 loc) · 1.88 KB
/
Copy pathDhammapada.tsx
File metadata and controls
69 lines (56 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { QuartzComponentProps } from "./types"
import { QuartzComponentConstructor } from "./types"
interface Options {
apiEndpoint: string
}
const defaultOpts: Options = {
apiEndpoint: "https://yoke.kassius.org/dhammapada",
// apiEndpoint: "https://yoke.kassius.org/dhammapada?space_padding=1",
}
export default ((userOpts?: Options) => {
const opts = { ...defaultOpts, ...userOpts }
function Dhammapada(props: QuartzComponentProps) {
const elementId = `api-result-dhammapada`
return (
<div class="external-api-box" style={{
fontSize: "inherit",
}}>
<div id={elementId} style={{
//fontSize: "inherit",
// textAlign: "center",
textAlign: "left",
border: "none",
boxShadow: "none",
outline: "none",
}}>
</div>
</div>
)
}
Dhammapada.afterDOMLoaded = `
// 1. Define the logic as a named function
async function loadRemoteText() {
const box = document.getElementById("api-result-dhammapada");
if (!box) return; // Component not present on this page
try {
const res = await fetch("${opts.apiEndpoint}");
if (!res.ok) throw new Error("HTTP " + res.status);
const text = await res.text();
box.innerText = text;
} catch (err) {
box.innerText = "Failed to load: " + err.message;
box.style.color = "#aaa";
}
}
// 2. Listen for the 'nav' event (Fired by Quartz on initial load AND page changes)
document.addEventListener("nav", loadRemoteText);
// 3. Cleanup: Remove listener when navigating AWAY to prevent duplicates
// (Quartz provides window.addCleanup for this exact purpose)
if (window.addCleanup) {
window.addCleanup(() => {
document.removeEventListener("nav", loadRemoteText);
});
}
`
return Dhammapada
}) satisfies QuartzComponentConstructor