From ec2ae41b815c4c465a856d800709de6147e5e54f Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 17 Mar 2023 02:27:53 +0900 Subject: refactor: make Document type class --- nuldoc-src/commands/build.ts | 4 +-- nuldoc-src/docbook/document.ts | 56 +++++++++++++++++++++++++++++++-------- nuldoc-src/docbook/to_html.ts | 16 ++++++----- nuldoc-src/templates/about.ts | 19 +++++++------ nuldoc-src/templates/post.ts | 2 +- nuldoc-src/templates/post_list.ts | 30 ++++++++++----------- nuldoc-src/templates/tag.ts | 28 ++++++++++---------- 7 files changed, 96 insertions(+), 59 deletions(-) (limited to 'nuldoc-src') diff --git a/nuldoc-src/commands/build.ts b/nuldoc-src/commands/build.ts index 734f520..8765802 100644 --- a/nuldoc-src/commands/build.ts +++ b/nuldoc-src/commands/build.ts @@ -117,8 +117,8 @@ function collectTags(posts: Document[]): [string, Document[]][] { result.push([ tag, tagsAndPosts.get(tag).sort((a: Document, b: Document) => { - const ta = a.revisions[0].date; - const tb = b.revisions[0].date; + const ta = a.getCreatedDate(); + const tb = b.getCreatedDate(); if (ta > tb) return -1; if (ta < tb) return 1; return 0; 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"); }); } diff --git a/nuldoc-src/templates/about.ts b/nuldoc-src/templates/about.ts index c41418c..ccd7b1a 100644 --- a/nuldoc-src/templates/about.ts +++ b/nuldoc-src/templates/about.ts @@ -110,14 +110,13 @@ export default async function generateAbout( head, body, ); - const doc = { - root: el("__root__", [], html), - sourceFilePath: "", - link: "/about/", - title: "About", - summary: "このサイトの著者について", - tags: [], - revisions: [], - }; - return doc; + return new Document( + el("__root__", [], html), + "", + "/about/", + "About", + "このサイトの著者について", + [], + [], + ); } diff --git a/nuldoc-src/templates/post.ts b/nuldoc-src/templates/post.ts index c89240c..9a89a73 100644 --- a/nuldoc-src/templates/post.ts +++ b/nuldoc-src/templates/post.ts @@ -28,7 +28,7 @@ export default async function convertPost( metaElement([["name", "author"], ["content", config.blog.author]]), metaElement([["name", "copyright"], [ "content", - `© ${doc.revisions[0].date.substring(0, 4)} ${config.blog.author}`, + `© ${doc.getCreatedDate().substring(0, 4)} ${config.blog.author}`, ]]), metaElement([["name", "description"], ["content", doc.summary]]), ]; diff --git a/nuldoc-src/templates/post_list.ts b/nuldoc-src/templates/post_list.ts index f4a541b..cdd2253 100644 --- a/nuldoc-src/templates/post_list.ts +++ b/nuldoc-src/templates/post_list.ts @@ -19,15 +19,15 @@ export default async function convertPostList( posts: Document[], config: Config, ): Promise { - const doc = { - root: el("__root__", []), - sourceFilePath: "", - link: "/posts/", - title: "投稿一覧", - summary: "投稿した記事の一覧", - tags: [], - revisions: [], - }; + const doc = new Document( + el("__root__", []), + "", + "/posts/", + "投稿一覧", + "投稿した記事の一覧", + [], + [], + ); const head = el( "head", @@ -95,8 +95,8 @@ export default async function convertPostList( ), ), ...Array.from(posts).sort((a, b) => { - const ta = a.revisions[0].date; - const tb = b.revisions[0].date; + const ta = a.getCreatedDate(); + const tb = b.getCreatedDate(); if (ta > tb) return -1; if (ta < tb) return 1; return 0; @@ -123,16 +123,16 @@ export default async function convertPostList( text("Posted on"), el( "time", - [["datetime", post.revisions[0].date]], - text(post.revisions[0].date), + [["datetime", post.getCreatedDate()]], + text(post.getCreatedDate()), ), ...(post.revisions.length > 1 ? [ text(", updated on "), el("time", [[ "datetime", - post.revisions[post.revisions.length - 1].date, - ]], text(post.revisions[post.revisions.length - 1].date)), + post.getUpdatedDate(), + ]], text(post.getUpdatedDate())), ] : []), ), diff --git a/nuldoc-src/templates/tag.ts b/nuldoc-src/templates/tag.ts index a53e59d..8c296d8 100644 --- a/nuldoc-src/templates/tag.ts +++ b/nuldoc-src/templates/tag.ts @@ -22,15 +22,15 @@ export default async function convertTag( ): Promise { const tagLabel = (config.blog.tagLabels as { [key: string]: string })[tag]; - const doc = { - root: el("__root__", []), - sourceFilePath: ``, - link: `/tags/${tag}/`, - title: tagLabel, - summary: `タグ「${tagLabel}」のついた記事一覧`, - tags: [], - revisions: [], - }; + const doc = new Document( + el("__root__", []), + ``, + `/tags/${tag}/`, + tagLabel, + `タグ「${tagLabel}」のついた記事一覧`, + [], + [], + ); const headChildren = [ metaElement([["charset", "UTF-8"]]), @@ -42,7 +42,7 @@ export default async function convertTag( metaElement([["name", "copyright"], [ "content", `© ${ - posts[posts.length - 1].revisions[0].date.substring(0, 4) + posts[posts.length - 1].getCreatedDate().substring(0, 4) } ${config.blog.author}`, ]]), metaElement([["name", "description"], [ @@ -117,16 +117,16 @@ export default async function convertTag( text("Posted on"), el( "time", - [["datetime", post.revisions[0].date]], - text(post.revisions[0].date), + [["datetime", post.getCreatedDate()]], + text(post.getCreatedDate()), ), ...(post.revisions.length > 1 ? [ text(", updated on "), el("time", [[ "datetime", - post.revisions[post.revisions.length - 1].date, - ]], text(post.revisions[post.revisions.length - 1].date)), + post.getUpdatedDate(), + ]], text(post.getUpdatedDate())), ] : []), ), -- cgit v1.2.3-70-g09d2