aboutsummaryrefslogtreecommitdiffhomepage
path: root/nuldoc-src/docbook
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-03-17 01:35:04 +0900
committernsfisis <nsfisis@gmail.com>2023-03-17 01:35:04 +0900
commit0766039bd9e6b9f5e6334e84666f5be698d41fc3 (patch)
tree0f3a52fdbf56496850f903d64dcf7725fb0aedbe /nuldoc-src/docbook
parentee72f8780cf3681e4202cc3a6358fb4038db1ec8 (diff)
downloadblog.nsfisis.dev-0766039bd9e6b9f5e6334e84666f5be698d41fc3.tar.gz
blog.nsfisis.dev-0766039bd9e6b9f5e6334e84666f5be698d41fc3.tar.zst
blog.nsfisis.dev-0766039bd9e6b9f5e6334e84666f5be698d41fc3.zip
feat(nuldoc): implement syntax highlight
Diffstat (limited to 'nuldoc-src/docbook')
-rw-r--r--nuldoc-src/docbook/to_html.ts31
1 files changed, 31 insertions, 0 deletions
diff --git a/nuldoc-src/docbook/to_html.ts b/nuldoc-src/docbook/to_html.ts
index 64d3492..24be139 100644
--- a/nuldoc-src/docbook/to_html.ts
+++ b/nuldoc-src/docbook/to_html.ts
@@ -1,3 +1,5 @@
+import hljs from "hljs/highlight.min.js";
+import hljsPhp from "hljs/languages/php.min.js";
import { Document } from "./document.ts";
import { DocBookError } from "../errors.ts";
import {
@@ -31,6 +33,7 @@ export default function toHtml(doc: Document): Document {
transformNoteElement(doc);
setDefaultLangAttribute(doc);
traverseFootnotes(doc);
+ highlightPrograms(doc);
return doc;
}
@@ -277,3 +280,31 @@ function traverseFootnotes(doc: Document) {
n.children = [];
});
}
+
+function highlightPrograms(doc: Document) {
+ forEachChildRecursively(doc.root, (n) => {
+ if (n.kind !== "element" || n.name !== "pre") {
+ return;
+ }
+ const preClass = n.attributes.get("class") || "";
+ if (!preClass.includes("highlight")) {
+ return;
+ }
+ const codeElement = findFirstChildElement(n, "code");
+ if (!codeElement) {
+ return;
+ }
+ const language = n.attributes.get("language");
+ if (!language) {
+ return;
+ }
+ const sourceCode = codeElement.children[0].content;
+
+ const validLanguage = hljs.getLanguage(language) ? language : "plaintext";
+ const highlighted =
+ hljs.highlight(sourceCode, { language: validLanguage }).value;
+
+ codeElement.children[0].content = highlighted;
+ codeElement.attributes.set("class", "highlight");
+ });
+}