-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
128 lines (114 loc) · 4.31 KB
/
Copy pathscript.js
File metadata and controls
128 lines (114 loc) · 4.31 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { getCookie, setCookie, getQueryValue } from "./src/utilities/helpers.js";
import { loadAssets } from "./src/utilities/assets.js";
import { createModalElement } from "./src/utilities/elements.js";
import { pages } from "./pages.js";
import showdown from "./src/external/showdown.js";
const currentTheme = getCookie("theme") || "dark";
document.documentElement.setAttribute("data-theme", currentTheme);
const allIcons = document.querySelectorAll("img.theme-toggle");
for (const icon of allIcons) {
icon.src = icon.dataset[`${currentTheme}Icon`];
}
const button = document.createElement("a");
button.title = "Theme toggle";
button.innerText = currentTheme === "dark" ? "🌕" : "🌘";
button.onclick = () => {
const toggleTheme = document.documentElement.getAttribute("data-theme") === "dark" ? "light" : "dark";
document.documentElement.setAttribute("data-theme", toggleTheme);
setCookie("theme", toggleTheme);
button.innerText = toggleTheme === "dark" ? "🌕" : "🌘";
const allIcons = document.querySelectorAll("img.theme-toggle");
for (const icon of allIcons) {
icon.src = icon.dataset[`${toggleTheme}Icon`];
}
};
document.getElementById("theme-toggle").appendChild(button);
const page = getQueryValue("page", false);
const available = Object.hasOwn(pages, page);
const path = page && available ? `./${page}` : ".";
const markdown = await loadAssets([{ name: "content", url: `${path}/INDEX.md`, type: "text" }]);
const converter = new showdown.Converter({ openLinksInNewWindow: false, tasklists: true, parseImgDimensions: true });
const main = document.querySelector("main");
const section = document.querySelector("section");
section.innerHTML = converter.makeHtml(markdown.content.data);
const header = document.querySelector("header");
const heading1 = document.querySelector("h1");
const heading6 = document.querySelector("h6");
if (header) {
if (heading1) {
header.appendChild(heading1);
}
if (heading6) {
header.appendChild(heading6);
document.title = heading6 ? heading6.innerText : "WebGPU Playground";
if (page && available) {
let pagePath = `?page=${page}`;
let pageIndex = 0;
const crumbsNow = heading6.innerText.split(" • ");
const crumbsFinal = [];
while (pageIndex < crumbsNow.length) {
crumbsFinal.push(`<a href="./index.html${pagePath}">${crumbsNow[pageIndex++]}</a>`);
pagePath = pagePath.substring(0, pagePath.lastIndexOf("/"));
}
heading6.innerHTML = crumbsFinal.toReversed().join(" » ");
}
}
}
const article = document.querySelector("article");
if (available) {
if (pages[page].includes("script")) {
const script = document.createElement("script");
script.src = `${path}/script.js`;
script.type = "module";
document.body.appendChild(script);
if (!pages[page].includes("fullscreen")) {
const blockquote = document.querySelector("blockquote");
if (blockquote) {
article.prepend(...blockquote.children);
blockquote.remove();
}
}
}
if (pages[page].includes("fullscreen")) {
main.style.padding = "0";
const modal = createModalElement(header, section, { container: main, open: false });
if (new URLSearchParams(location.search).size === 1) {
modal.open = true;
}
const hrs = document.querySelectorAll("hr");
hrs.forEach((element) => {
element.remove();
});
const footer = document.querySelector("footer");
footer.classList.add("fullscreen-mode");
const firstListParent = document.querySelectorAll("ul")[0];
const firstListItem = document.querySelectorAll("li")[0];
firstListItem.remove();
{
const blockquote = document.querySelector("blockquote");
if (blockquote) {
const li = document.createElement("li");
li.appendChild(...blockquote.children);
firstListParent.appendChild(li);
blockquote.remove();
}
}
{
const li = document.createElement("li");
const button = document.createElement("a");
button.title = "Pop-up info modal";
button.innerHTML = "<strong>ℹ️</strong>";
button.onclick = (event) => {
modal.open = true;
event.preventDefault();
};
li.appendChild(button);
firstListParent.appendChild(li);
}
}
if (pages[page].includes("noarticle")) {
article.remove();
}
} else {
article.remove();
}