diff options
| author | nsfisis <nsfisis@gmail.com> | 2023-03-17 02:27:53 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2023-03-17 02:27:53 +0900 |
| commit | ec2ae41b815c4c465a856d800709de6147e5e54f (patch) | |
| tree | ab05d0e2971cdbcfa55e531e6d977cc552c7adf5 /nuldoc-src/docbook | |
| parent | 29bcdc6c1bad2240d404de9dca2463e46fdc1e93 (diff) | |
| download | blog.nsfisis.dev-ec2ae41b815c4c465a856d800709de6147e5e54f.tar.gz blog.nsfisis.dev-ec2ae41b815c4c465a856d800709de6147e5e54f.tar.zst blog.nsfisis.dev-ec2ae41b815c4c465a856d800709de6147e5e54f.zip | |
refactor: make Document type class
Diffstat (limited to 'nuldoc-src/docbook')
| -rw-r--r-- | nuldoc-src/docbook/document.ts | 56 | ||||
| -rw-r--r-- | nuldoc-src/docbook/to_html.ts | 16 |
2 files changed, 55 insertions, 17 deletions
diff --git a/nuldoc-src/docbook/document.ts b/nuldoc-src/docbook/document.ts index b779ac7..ae3159a 100644 --- a/nuldoc-src/docbook/document.ts +++ b/nuldoc-src/docbook/document.ts @@ -9,7 +9,7 @@ import { innerText, } from "../dom.ts"; -export type Document = { +export class Document { root: Element; sourceFilePath: string; link: string; @@ -17,7 +17,41 @@ export type Document = { summary: string; // TODO: should it be markup text? tags: string[]; revisions: Revision[]; -}; + + constructor( + root: Element, + sourceFilePath: string, + link: string, + title: string, + summary: string, + tags: string[], + revisions: Revision[], + ) { + this.root = root; + this.sourceFilePath = sourceFilePath; + this.link = link; + this.title = title; + this.summary = summary; + this.tags = tags; + this.revisions = revisions; + } + + getLatestRevision(): Revision { + return this.revisions[this.revisions.length - 1]; + } + + getOldestRevision(): Revision { + return this.revisions[0]; + } + + getUpdatedDate(): string { + return this.getLatestRevision().date; + } + + getCreatedDate(): string { + return this.getOldestRevision().date; + } +} export function createNewDocumentFromRootElement( root: Element, @@ -96,13 +130,13 @@ export function createNewDocumentFromRootElement( const cwd = Deno.cwd(); const contentDir = join(cwd, config.locations.contentDir); const link = sourceFilePath.replace(contentDir, "").replace(".xml", "/"); - return { - root: root, - title: title, - summary: summary, - tags: tags, - revisions: revisions, - sourceFilePath: sourceFilePath, - link: link, - }; + return new Document( + root, + sourceFilePath, + link, + title, + summary, + tags, + revisions, + ); } diff --git a/nuldoc-src/docbook/to_html.ts b/nuldoc-src/docbook/to_html.ts index 24be139..0672ea6 100644 --- a/nuldoc-src/docbook/to_html.ts +++ b/nuldoc-src/docbook/to_html.ts @@ -1,5 +1,4 @@ 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 { @@ -9,6 +8,7 @@ import { forEachChildRecursively, Node, removeChildElements, + Text, } from "../dom.ts"; export default function toHtml(doc: Document): Document { @@ -298,13 +298,17 @@ function highlightPrograms(doc: Document) { if (!language) { return; } - const sourceCode = codeElement.children[0].content; + const sourceCode = (codeElement.children[0] as Text).content; - const validLanguage = hljs.getLanguage(language) ? language : "plaintext"; - const highlighted = - hljs.highlight(sourceCode, { language: validLanguage }).value; + const validLanguage = + (hljs as { getLanguage: (s: string) => string }).getLanguage(language) + ? language + : "plaintext"; + const highlighted = (hljs as { + highlight: (s: string, o: { language: string }) => { value: string }; + }).highlight(sourceCode, { language: validLanguage }).value; - codeElement.children[0].content = highlighted; + (codeElement.children[0] as Text).content = highlighted; codeElement.attributes.set("class", "highlight"); }); } |
