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,78 @@
|
||||
export const escapeText = (value) =>
|
||||
String(value).replace(
|
||||
/[&<>"']/g,
|
||||
(c) =>
|
||||
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[
|
||||
c
|
||||
],
|
||||
);
|
||||
export const codeLanguages = Object.freeze([
|
||||
"plaintext",
|
||||
"markdown",
|
||||
"yaml",
|
||||
"json",
|
||||
"javascript",
|
||||
"typescript",
|
||||
"python",
|
||||
"bash",
|
||||
"nginx",
|
||||
"ini",
|
||||
"xml",
|
||||
"css",
|
||||
"sql",
|
||||
]);
|
||||
export const normalizeLanguage = (value) =>
|
||||
({
|
||||
text: "plaintext",
|
||||
txt: "plaintext",
|
||||
md: "markdown",
|
||||
yml: "yaml",
|
||||
js: "javascript",
|
||||
ts: "typescript",
|
||||
sh: "bash",
|
||||
html: "xml",
|
||||
})[String(value).toLowerCase()] || String(value).toLowerCase();
|
||||
let runtime;
|
||||
export function loadTextRuntime() {
|
||||
runtime ||= Promise.all([
|
||||
import("../vendor/text/marked.js"),
|
||||
import("../vendor/text/purify.js"),
|
||||
import("../vendor/text/highlight/core.js"),
|
||||
])
|
||||
.then(async ([marked, purify, highlight]) => {
|
||||
const hljs = highlight.default;
|
||||
const languages = codeLanguages.filter(
|
||||
(language) => language !== "plaintext",
|
||||
);
|
||||
const grammars = await Promise.all(
|
||||
languages.map(
|
||||
(language) =>
|
||||
import(`../vendor/text/highlight/languages/${language}.js`),
|
||||
),
|
||||
);
|
||||
languages.forEach((language, index) =>
|
||||
hljs.registerLanguage(language, grammars[index].default),
|
||||
);
|
||||
return { marked: marked.marked, purify: purify.default, hljs };
|
||||
})
|
||||
.catch((error) => {
|
||||
runtime = null;
|
||||
throw error;
|
||||
});
|
||||
return runtime;
|
||||
}
|
||||
export async function highlightedCode(value, language = "plaintext") {
|
||||
const text = String(value),
|
||||
name = normalizeLanguage(language);
|
||||
if (
|
||||
name === "plaintext" ||
|
||||
!codeLanguages.includes(name) ||
|
||||
text.length > 65536
|
||||
)
|
||||
return escapeText(text);
|
||||
const { hljs, purify } = await loadTextRuntime();
|
||||
return purify.sanitize(
|
||||
hljs.highlight(text, { language: name, ignoreIllegals: true }).value,
|
||||
{ ALLOWED_TAGS: ["span"], ALLOWED_ATTR: ["class"] },
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user