aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-06-25 18:20:53 +0900
committernsfisis <nsfisis@gmail.com>2023-06-25 18:25:31 +0900
commit2e135352d0b3662ef44336994465ec7d75d400f1 (patch)
treeb8b16c6d6c519c0f9854691fb99b2c40e632311c
parent85bdb29b8dfbf6027fc2f98289433e254ec6ff50 (diff)
downloadblog.nsfisis.dev-2e135352d0b3662ef44336994465ec7d75d400f1.tar.gz
blog.nsfisis.dev-2e135352d0b3662ef44336994465ec7d75d400f1.tar.zst
blog.nsfisis.dev-2e135352d0b3662ef44336994465ec7d75d400f1.zip
feat(nuldoc): improve error handling when a tag is undefined
-rw-r--r--nuldoc-src/config.ts7
-rw-r--r--nuldoc-src/pages/post.ts10
-rw-r--r--nuldoc-src/pages/slide.ts10
-rw-r--r--nuldoc-src/pages/tag.ts5
4 files changed, 15 insertions, 17 deletions
diff --git a/nuldoc-src/config.ts b/nuldoc-src/config.ts
index cbbabca..76a05fe 100644
--- a/nuldoc-src/config.ts
+++ b/nuldoc-src/config.ts
@@ -33,3 +33,10 @@ export const config = {
};
export type Config = typeof config;
+
+export function getTagLabel(c: Config, slug: string): string {
+ if (!(slug in c.blog.tagLabels)) {
+ throw new Error(`Unknown tag: ${slug}`);
+ }
+ return (c.blog.tagLabels as { [slug: string]: string })[slug];
+}
diff --git a/nuldoc-src/pages/post.ts b/nuldoc-src/pages/post.ts
index d870e99..24a6d5f 100644
--- a/nuldoc-src/pages/post.ts
+++ b/nuldoc-src/pages/post.ts
@@ -2,7 +2,7 @@ import { join } from "std/path/mod.ts";
import { globalFooter } from "../components/global_footer.ts";
import { globalHeader } from "../components/global_header.ts";
import { pageLayout } from "../components/page_layout.ts";
-import { Config } from "../config.ts";
+import { Config, getTagLabel } from "../config.ts";
import { el, Element, text } from "../dom.ts";
import { Document } from "../docbook/document.ts";
import { Page } from "../page.ts";
@@ -57,9 +57,7 @@ export async function generatePostPage(
"a",
[["href", `/tags/${slug}/`]],
text(
- (config.blog.tagLabels as {
- [key: string]: string;
- })[slug],
+ getTagLabel(config, slug),
),
),
)
@@ -115,9 +113,7 @@ export async function generatePostPage(
{
metaCopyrightYear: getPostCreatedDate(doc).year,
metaDescription: doc.summary,
- metaKeywords: doc.tags.map((slug) =>
- (config.blog.tagLabels as { [key: string]: string })[slug]
- ),
+ metaKeywords: doc.tags.map((slug) => getTagLabel(config, slug)),
metaTitle: `${doc.title} | ${config.blog.siteName}`,
requiresSyntaxHighlight: true,
},
diff --git a/nuldoc-src/pages/slide.ts b/nuldoc-src/pages/slide.ts
index e5f40a0..a75aeb6 100644
--- a/nuldoc-src/pages/slide.ts
+++ b/nuldoc-src/pages/slide.ts
@@ -3,7 +3,7 @@ import { globalFooter } from "../components/global_footer.ts";
import { globalHeader } from "../components/global_header.ts";
import { pageLayout } from "../components/page_layout.ts";
import { staticScriptElement } from "../components/utils.ts";
-import { Config } from "../config.ts";
+import { Config, getTagLabel } from "../config.ts";
import { el, text } from "../dom.ts";
import { Page } from "../page.ts";
import { dateToString, Revision } from "../revision.ts";
@@ -53,9 +53,7 @@ export async function generateSlidePage(
"a",
[["href", `/tags/${slug}/`]],
text(
- (config.blog.tagLabels as {
- [key: string]: string;
- })[slug],
+ getTagLabel(config, slug),
),
),
)
@@ -113,9 +111,7 @@ export async function generateSlidePage(
{
metaCopyrightYear: getPostCreatedDate(slide).year,
metaDescription: slide.title,
- metaKeywords: slide.tags.map((slug) =>
- (config.blog.tagLabels as { [key: string]: string })[slug]
- ),
+ metaKeywords: slide.tags.map((slug) => getTagLabel(config, slug)),
metaTitle: `${slide.event} (${slide.talkType}) | ${config.blog.siteName}`,
requiresSyntaxHighlight: true,
},
diff --git a/nuldoc-src/pages/tag.ts b/nuldoc-src/pages/tag.ts
index cb60d21..f501cb3 100644
--- a/nuldoc-src/pages/tag.ts
+++ b/nuldoc-src/pages/tag.ts
@@ -3,7 +3,7 @@ import { globalHeader } from "../components/global_header.ts";
import { pageLayout } from "../components/page_layout.ts";
import { postPageEntry } from "../components/post_page_entry.ts";
import { slidePageEntry } from "../components/slide_page_entry.ts";
-import { Config } from "../config.ts";
+import { Config, getTagLabel } from "../config.ts";
import { el, text } from "../dom.ts";
import { Page } from "../page.ts";
import { getPostCreatedDate } from "./post.ts";
@@ -19,8 +19,7 @@ export async function generateTagPage(
pages: TaggedPage[],
config: Config,
): Promise<TagPage> {
- const tagLabel =
- (config.blog.tagLabels as { [key: string]: string })[tagSlug];
+ const tagLabel = getTagLabel(config, tagSlug);
const pageTitle = `タグ「${tagLabel}」一覧`;
const body = el(