aboutsummaryrefslogtreecommitdiffhomepage
path: root/nuldoc-src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-03-18 15:47:05 +0900
committernsfisis <nsfisis@gmail.com>2023-03-18 15:47:05 +0900
commit2b50e1778b164e641c03c2e77176b6f47ca1e278 (patch)
tree3fa48f5c5c3c2b9d8753ce82d438aa2523856660 /nuldoc-src
parent4ce1f674055ac1dd1c4864f366aac212f5643248 (diff)
downloadblog.nsfisis.dev-2b50e1778b164e641c03c2e77176b6f47ca1e278.tar.gz
blog.nsfisis.dev-2b50e1778b164e641c03c2e77176b6f47ca1e278.tar.zst
blog.nsfisis.dev-2b50e1778b164e641c03c2e77176b6f47ca1e278.zip
refactor: add RawHTML type to represent text node not being escaped
Diffstat (limited to 'nuldoc-src')
-rw-r--r--nuldoc-src/docbook/to_html.ts12
-rw-r--r--nuldoc-src/dom.ts9
-rw-r--r--nuldoc-src/html.ts8
-rw-r--r--nuldoc-src/templates/utils.ts1
-rw-r--r--nuldoc-src/types/highlight-js.d.ts4
-rw-r--r--nuldoc-src/xml.ts2
6 files changed, 31 insertions, 5 deletions
diff --git a/nuldoc-src/docbook/to_html.ts b/nuldoc-src/docbook/to_html.ts
index c824780..9f176d4 100644
--- a/nuldoc-src/docbook/to_html.ts
+++ b/nuldoc-src/docbook/to_html.ts
@@ -9,6 +9,7 @@ import {
forEachChild,
forEachChildRecursively,
Node,
+ RawHTML,
removeChildElements,
Text,
} from "../dom.ts";
@@ -239,6 +240,7 @@ function transformNoteElement(doc: Document) {
children: [{
kind: "text",
content: "Note",
+ raw: true,
}],
};
const contentElement: Element = {
@@ -298,12 +300,18 @@ function highlightPrograms(doc: Document) {
if (!language) {
return;
}
- const sourceCode = (codeElement.children[0] as Text).content;
+ const sourceCodeNode = codeElement.children[0] as Text | RawHTML;
+ const sourceCode = sourceCodeNode.content;
+
+ if (!hljs.getLanguage(language)) {
+ return;
+ }
const highlighted =
hljs.highlight(sourceCode, { language: language }).value;
- (codeElement.children[0] as Text).content = highlighted;
+ sourceCodeNode.content = highlighted;
+ sourceCodeNode.raw = true;
codeElement.attributes.set("class", "highlight");
});
}
diff --git a/nuldoc-src/dom.ts b/nuldoc-src/dom.ts
index 51ef25a..626d400 100644
--- a/nuldoc-src/dom.ts
+++ b/nuldoc-src/dom.ts
@@ -1,6 +1,13 @@
export type Text = {
kind: "text";
content: string;
+ raw: false;
+};
+
+export type RawHTML = {
+ kind: "text";
+ content: string;
+ raw: true;
};
export type Element = {
@@ -10,7 +17,7 @@ export type Element = {
children: Node[];
};
-export type Node = Element | Text;
+export type Node = Element | Text | RawHTML;
export function addClass(e: Element, klass: string) {
const classes = e.attributes.get("class");
diff --git a/nuldoc-src/html.ts b/nuldoc-src/html.ts
index 70b5748..b94877a 100644
--- a/nuldoc-src/html.ts
+++ b/nuldoc-src/html.ts
@@ -115,15 +115,19 @@ function toHtmlText(dom: Document): string {
function nodeToHtmlText(n: Node, ctx: Context): string {
if (n.kind === "text") {
- return textNodeToHtmlText(n, ctx);
+ if (n.raw) {
+ return n.content;
+ } else {
+ return textNodeToHtmlText(n, ctx);
+ }
} else {
return elementNodeToHtmlText(n, ctx);
}
}
function textNodeToHtmlText(t: Text, ctx: Context): string {
- if (ctx.isInPre) return t.content;
const s = encodeSpecialCharacters(t.content);
+ if (ctx.isInPre) return s;
// TODO: 日本語で改行するときはスペースを入れない
return s
diff --git a/nuldoc-src/templates/utils.ts b/nuldoc-src/templates/utils.ts
index a86803d..65dd506 100644
--- a/nuldoc-src/templates/utils.ts
+++ b/nuldoc-src/templates/utils.ts
@@ -7,6 +7,7 @@ export function text(content: string): Text {
return {
kind: "text",
content: content,
+ raw: false,
};
}
diff --git a/nuldoc-src/types/highlight-js.d.ts b/nuldoc-src/types/highlight-js.d.ts
index 312cd06..d7bd0b5 100644
--- a/nuldoc-src/types/highlight-js.d.ts
+++ b/nuldoc-src/types/highlight-js.d.ts
@@ -1,4 +1,8 @@
declare module "highlight.js" {
+ function getLanguage(
+ name: string,
+ ): string | undefined;
+
function highlight(
code: string,
options: { language: string },
diff --git a/nuldoc-src/xml.ts b/nuldoc-src/xml.ts
index bb7d499..9c0ea45 100644
--- a/nuldoc-src/xml.ts
+++ b/nuldoc-src/xml.ts
@@ -80,6 +80,7 @@ function parseTextNode(p: Parser): Text {
return {
kind: "text",
content: replaceEntityReferences(content),
+ raw: false,
};
}
@@ -90,6 +91,7 @@ function parseCdata(p: Parser): Text {
return {
kind: "text",
content: formatCdata(content),
+ raw: false,
};
}