aboutsummaryrefslogtreecommitdiffhomepage
path: root/nuldoc-src/pages/post.ts
diff options
context:
space:
mode:
Diffstat (limited to 'nuldoc-src/pages/post.ts')
-rw-r--r--nuldoc-src/pages/post.ts207
1 files changed, 207 insertions, 0 deletions
diff --git a/nuldoc-src/pages/post.ts b/nuldoc-src/pages/post.ts
new file mode 100644
index 0000000..a431c37
--- /dev/null
+++ b/nuldoc-src/pages/post.ts
@@ -0,0 +1,207 @@
+import { join } from "std/path/mod.ts";
+import { Config } from "../config.ts";
+import { Element } from "../dom.ts";
+import { Document } from "../docbook/document.ts";
+import { Page } from "../page.ts";
+import { Revision } from "../revision.ts";
+import {
+ el,
+ linkElement,
+ metaElement,
+ stylesheetLinkElement,
+ text,
+} from "./utils.ts";
+
+export interface PostPage extends Page {
+ title: string;
+ summary: string;
+ tags: string[];
+ revisions: Revision[];
+}
+
+export function getPostCreatedDate(page: { revisions: Revision[] }): string {
+ return page.revisions[0].date;
+}
+
+export function getPostUpdatedDate(page: { revisions: Revision[] }): string {
+ return page.revisions[page.revisions.length - 1].date;
+}
+
+export async function generatePostPage(
+ doc: Document,
+ config: Config,
+): Promise<PostPage> {
+ const headChildren = [
+ metaElement([["charset", "UTF-8"]]),
+ metaElement([["name", "viewport"], [
+ "content",
+ "width=device-width, initial-scale=1.0",
+ ]]),
+ metaElement([["name", "author"], ["content", config.blog.author]]),
+ metaElement([["name", "copyright"], [
+ "content",
+ `&copy; ${getPostCreatedDate(doc).substring(0, 4)} ${config.blog.author}`,
+ ]]),
+ metaElement([["name", "description"], ["content", doc.summary]]),
+ ];
+ if (doc.tags.length !== 0) {
+ headChildren.push(
+ metaElement([["name", "keywords"], [
+ "content",
+ doc.tags.map((slug) =>
+ (config.blog.tagLabels as { [key: string]: string })[slug]
+ ).join(","),
+ ]]),
+ );
+ }
+ headChildren.push(linkElement("icon", "/favicon.svg", "image/svg+xml"));
+ headChildren.push(
+ el("title", [], text(`${doc.title} | ${config.blog.siteName}`)),
+ );
+ headChildren.push(await stylesheetLinkElement("/style.css", config));
+ headChildren.push(await stylesheetLinkElement("/hl.css", config));
+ const head = el("head", [], ...headChildren);
+ const body = el(
+ "body",
+ [["class", "single"]],
+ el(
+ "header",
+ [["class", "header"]],
+ el(
+ "nav",
+ [["class", "nav"]],
+ el(
+ "ul",
+ [],
+ el(
+ "li",
+ [["class", "logo"]],
+ el("a", [["href", "/"]], text(config.blog.siteName)),
+ ),
+ el(
+ "li",
+ [],
+ el("a", [["href", "/about"]], text("About")),
+ ),
+ el(
+ "li",
+ [],
+ el("a", [["href", "/posts"]], text("Posts")),
+ ),
+ el(
+ "li",
+ [],
+ el("a", [["href", "/slides"]], text("Slides")),
+ ),
+ ),
+ ),
+ ),
+ el(
+ "main",
+ [["class", "main"]],
+ el(
+ "article",
+ [["class", "post-single"]],
+ el(
+ "header",
+ [["class", "post-header"]],
+ el(
+ "h1",
+ [["class", "post-title"]],
+ text(doc.title),
+ ),
+ ...(doc.tags.length === 0 ? [] : [
+ el(
+ "ul",
+ [["class", "post-tags"]],
+ ...doc.tags.map((slug) =>
+ el(
+ "li",
+ [["class", "tag"]],
+ el(
+ "a",
+ [["href", `/tags/${slug}`]],
+ text(
+ (config.blog.tagLabels as {
+ [key: string]: string;
+ })[slug],
+ ),
+ ),
+ )
+ ),
+ ),
+ ]),
+ ),
+ el(
+ "div",
+ [["class", "post-content"]],
+ el(
+ "section",
+ [],
+ el(
+ "h2",
+ [["id", "changelog"]],
+ text("更新履歴"),
+ ),
+ el(
+ "ol",
+ [],
+ ...doc.revisions.map((rev) =>
+ el(
+ "li",
+ [["class", "revision"]],
+ el(
+ "time",
+ [["datetime", rev.date]],
+ text(rev.date),
+ ),
+ text(`: ${rev.remark}`),
+ )
+ ),
+ ),
+ ),
+ // TODO: refactor
+ ...(doc.root.children[0] as Element).children,
+ // TODO: footnotes
+ // <div id="footnotes">
+ // <% for footnote in footnotes %>
+ // <div class="footnote" id="_footnotedef_<%= footnote.index %>">
+ // <a href="#_footnoteref_<%= footnote.index %>"><%= footnote.index %></a>. <%= footnote.text %>
+ // </div>
+ // <% end %>
+ // </div>
+ ),
+ ),
+ ),
+ el(
+ "footer",
+ [["class", "footer"]],
+ text(
+ `&copy; ${config.blog.siteCopyrightYear} ${config.blog.author}`,
+ ),
+ ),
+ );
+ const html = el(
+ "html",
+ [["lang", "ja-JP"]],
+ head,
+ body,
+ );
+
+ const cwd = Deno.cwd();
+ const contentDir = join(cwd, config.locations.contentDir);
+ const destFilePath = join(
+ doc.sourceFilePath.replace(contentDir, "").replace(".xml", ""),
+ "index.html",
+ );
+ return {
+ root: el("__root__", [], html),
+ renderer: "html",
+ destFilePath: destFilePath,
+ href: destFilePath.replace("index.html", ""),
+ title: doc.title,
+ summary: doc.summary,
+ tags: doc.tags,
+ revisions: doc.revisions,
+ };
+}