Add Markdown editor, server statistics and content navigation
This commit is contained in:
1 parent
03c590e172
commit
1819f35f91
52 files changed
+7190
-28
No files matched your search
@@ -0,0 +1,166 @@
|
||||
import {
|
||||
loadTextRuntime,
|
||||
highlightedCode,
|
||||
normalizeLanguage,
|
||||
} from "./text-runtime.js";
|
||||
const versions = new WeakMap();
|
||||
let sequence = 0;
|
||||
export function mountMarkdown(
|
||||
root,
|
||||
{ source = "", headingPrefix, t = (ru) => ru } = {},
|
||||
) {
|
||||
const prefix = headingPrefix || `anidev-md-${++sequence}`;
|
||||
let disposed = false;
|
||||
root.classList.add("markdown-body");
|
||||
async function setSource(value) {
|
||||
const text = String(value);
|
||||
if (text.length > 262144)
|
||||
throw new RangeError("Markdown exceeds 262144 characters");
|
||||
const ticket = {};
|
||||
versions.set(root, ticket);
|
||||
root.setAttribute("aria-busy", "true");
|
||||
try {
|
||||
const { marked, purify } = await loadTextRuntime();
|
||||
const fragment = purify.sanitize(
|
||||
marked.parse(text.replace(/^\uFEFF/, ""), {
|
||||
gfm: true,
|
||||
breaks: false,
|
||||
async: false,
|
||||
}),
|
||||
{
|
||||
RETURN_DOM_FRAGMENT: true,
|
||||
ALLOWED_TAGS: [
|
||||
"h1",
|
||||
"h2",
|
||||
"h3",
|
||||
"h4",
|
||||
"h5",
|
||||
"h6",
|
||||
"p",
|
||||
"br",
|
||||
"hr",
|
||||
"strong",
|
||||
"em",
|
||||
"del",
|
||||
"blockquote",
|
||||
"ul",
|
||||
"ol",
|
||||
"li",
|
||||
"pre",
|
||||
"code",
|
||||
"a",
|
||||
"table",
|
||||
"thead",
|
||||
"tbody",
|
||||
"tr",
|
||||
"th",
|
||||
"td",
|
||||
"img",
|
||||
"input",
|
||||
"details",
|
||||
"summary",
|
||||
"kbd",
|
||||
"s",
|
||||
"sup",
|
||||
"sub",
|
||||
],
|
||||
ALLOWED_ATTR: [
|
||||
"href",
|
||||
"title",
|
||||
"alt",
|
||||
"src",
|
||||
"class",
|
||||
"start",
|
||||
"type",
|
||||
"checked",
|
||||
"disabled",
|
||||
"align",
|
||||
],
|
||||
},
|
||||
);
|
||||
const ids = new Map();
|
||||
fragment.querySelectorAll("h1,h2,h3,h4,h5,h6").forEach((heading) => {
|
||||
const slug =
|
||||
heading.textContent
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[^\p{L}\p{N}\s-]/gu, "")
|
||||
.replace(/\s+/g, "-") || "section";
|
||||
const count = ids.get(slug) || 0;
|
||||
ids.set(slug, count + 1);
|
||||
heading.id = `${prefix}-${slug}${count ? "-" + count : ""}`;
|
||||
});
|
||||
fragment.querySelectorAll("a").forEach((link) => {
|
||||
const href = link.getAttribute("href") || "";
|
||||
if (href.startsWith("#")) {
|
||||
let slug;
|
||||
try {
|
||||
slug = decodeURIComponent(href.slice(1));
|
||||
} catch {
|
||||
slug = href.slice(1);
|
||||
}
|
||||
if (ids.has(slug)) link.href = `#${prefix}-${slug}`;
|
||||
} else {
|
||||
let url;
|
||||
try {
|
||||
url = new URL(href, location.href);
|
||||
} catch {
|
||||
link.removeAttribute("href");
|
||||
return;
|
||||
}
|
||||
if (!["http:", "https:", "mailto:", "tel:"].includes(url.protocol))
|
||||
link.removeAttribute("href");
|
||||
else if (
|
||||
["http:", "https:"].includes(url.protocol) &&
|
||||
url.origin !== location.origin
|
||||
) {
|
||||
link.target = "_blank";
|
||||
link.rel = "noopener noreferrer";
|
||||
}
|
||||
}
|
||||
});
|
||||
fragment.querySelectorAll("img").forEach((img) => {
|
||||
img.loading = "lazy";
|
||||
img.decoding = "async";
|
||||
});
|
||||
fragment.querySelectorAll("input").forEach((input) => {
|
||||
if (input.type !== "checkbox") input.remove();
|
||||
else input.disabled = true;
|
||||
});
|
||||
await Promise.all(
|
||||
[...fragment.querySelectorAll("pre > code")].map(async (code) => {
|
||||
const match = [...code.classList].find((name) =>
|
||||
name.startsWith("language-"),
|
||||
);
|
||||
const language = normalizeLanguage(match?.slice(9) || "plaintext");
|
||||
code.innerHTML = await highlightedCode(code.textContent, language);
|
||||
code.className = `hljs language-${language.replace(/[^\w-]/g, "")}`;
|
||||
}),
|
||||
);
|
||||
if (disposed || versions.get(root) !== ticket) return;
|
||||
root.replaceChildren(fragment);
|
||||
} catch (error) {
|
||||
if (!disposed && versions.get(root) === ticket) {
|
||||
root.textContent = t(
|
||||
"Не удалось отобразить документ.",
|
||||
"Could not render the document.",
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
} finally {
|
||||
if (!disposed && versions.get(root) === ticket)
|
||||
root.setAttribute("aria-busy", "false");
|
||||
}
|
||||
}
|
||||
root.textContent = t("Загрузка документа…", "Loading document…");
|
||||
const ready = setSource(source);
|
||||
return {
|
||||
ready,
|
||||
setSource,
|
||||
dispose() {
|
||||
disposed = true;
|
||||
versions.delete(root);
|
||||
root.removeAttribute("aria-busy");
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user