diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-01-13 19:48:05 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-01-13 19:48:05 +0900 |
| commit | d2cc2bd091394008fd30cbe3606ff4a35ee829a4 (patch) | |
| tree | b71da3ad4c9f03b1cb1b1fe43beab705726e7f28 | |
| parent | 0be356bc6d468ee82ec0b8798409c1da48c653f3 (diff) | |
| download | nsfisis.dev-d2cc2bd091394008fd30cbe3606ff4a35ee829a4.tar.gz nsfisis.dev-d2cc2bd091394008fd30cbe3606ff4a35ee829a4.tar.zst nsfisis.dev-d2cc2bd091394008fd30cbe3606ff4a35ee829a4.zip | |
refactor(blog/nuldoc): remove unnecessary validation
| -rw-r--r-- | vhosts/blog/nuldoc-src/commands/build.ts | 2 | ||||
| -rw-r--r-- | vhosts/blog/nuldoc-src/slide/parse.ts | 31 | ||||
| -rw-r--r-- | vhosts/blog/nuldoc-src/slide/slide.ts | 134 |
3 files changed, 46 insertions, 121 deletions
diff --git a/vhosts/blog/nuldoc-src/commands/build.ts b/vhosts/blog/nuldoc-src/commands/build.ts index 0c066b40..2bfb9e02 100644 --- a/vhosts/blog/nuldoc-src/commands/build.ts +++ b/vhosts/blog/nuldoc-src/commands/build.ts @@ -109,7 +109,7 @@ async function parseSlides( const slides = []; for (const slideFile of slideFiles) { slides.push( - await generateSlidePage(await parseSlideFile(slideFile, config), config), + await generateSlidePage(await parseSlideFile(slideFile), config), ); } return slides; diff --git a/vhosts/blog/nuldoc-src/slide/parse.ts b/vhosts/blog/nuldoc-src/slide/parse.ts index 7ba17cb0..170b7484 100644 --- a/vhosts/blog/nuldoc-src/slide/parse.ts +++ b/vhosts/blog/nuldoc-src/slide/parse.ts @@ -1,33 +1,16 @@ import { parse as parseToml } from "std/toml/mod.ts"; -import { Config } from "../config.ts"; -import { createNewSlideFromTomlRootObject, Slide } from "./slide.ts"; -import { z } from "zod/mod.ts"; +import { + createNewSlideFromMetadata, + Slide, + SlideMetadataSchema, +} from "./slide.ts"; -const SlideMetadataSchema = z.object({ - slide: z.object({ - uuid: z.string(), - title: z.string(), - event: z.string(), - talkType: z.string(), - link: z.string(), - tags: z.array(z.string()), - revisions: z.array(z.object({ - date: z.string(), - remark: z.string(), - isInternal: z.boolean().optional(), - })), - }), -}); - -export async function parseSlideFile( - filePath: string, - config: Config, -): Promise<Slide> { +export async function parseSlideFile(filePath: string): Promise<Slide> { try { const root = SlideMetadataSchema.parse( parseToml(await Deno.readTextFile(filePath)), ); - return createNewSlideFromTomlRootObject(root, filePath, config); + return createNewSlideFromMetadata(root, filePath); } catch (e) { if (e instanceof Error) { e.message = `${e.message} in ${filePath}`; diff --git a/vhosts/blog/nuldoc-src/slide/slide.ts b/vhosts/blog/nuldoc-src/slide/slide.ts index 379d3bc7..8fe99eab 100644 --- a/vhosts/blog/nuldoc-src/slide/slide.ts +++ b/vhosts/blog/nuldoc-src/slide/slide.ts @@ -1,6 +1,24 @@ -import { Config } from "../config.ts"; import { SlideError } from "../errors.ts"; import { Revision, stringToDate } from "../revision.ts"; +import { z } from "zod/mod.ts"; + +export const SlideMetadataSchema = z.object({ + slide: z.object({ + uuid: z.string(), + title: z.string(), + event: z.string(), + talkType: z.string(), + link: z.string(), + tags: z.array(z.string()), + revisions: z.array(z.object({ + date: z.string(), + remark: z.string(), + isInternal: z.boolean().optional(), + })), + }), +}); + +export type SlideMetadata = z.infer<typeof SlideMetadataSchema>; export type Slide = { sourceFilePath: string; @@ -13,96 +31,15 @@ export type Slide = { revisions: Revision[]; }; -type Toml = { - slide: { - uuid: string; - title: string; - event: string; - talkType: string; - link: string; - tags: string[]; - revisions: { - date: string; - remark: string; - }[]; - }; -}; - -export function createNewSlideFromTomlRootObject( - root: Toml, +export function createNewSlideFromMetadata( + { slide }: SlideMetadata, sourceFilePath: string, - _config: Config, ): Slide { - const slide = root.slide ?? null; - if (root.slide === undefined) { - throw new SlideError( - `[slide.new] 'slide' field not found`, - ); - } - - const uuid = slide.uuid ?? null; - if (!uuid) { - throw new SlideError( - `[slide.new] 'slide.uuid' field not found`, - ); - } - - const title = slide.title ?? null; - if (!title) { - throw new SlideError( - `[slide.new] 'slide.title' field not found`, - ); - } - - const event = slide.event ?? null; - if (!event) { - throw new SlideError( - `[slide.new] 'slide.event' field not found`, - ); - } - - const talkType = slide.talkType ?? null; - if (!talkType) { - throw new SlideError( - `[slide.new] 'slide.talkType' field not found`, - ); - } - - const link = slide.link ?? null; - if (!link) { - throw new SlideError( - `[slide.new] 'slide.link' field not found`, - ); - } - - const tags = slide.tags ?? []; - - const revisions = slide.revisions ?? null; - if (!revisions) { - throw new SlideError( - `[slide.new] 'slide.revisions' field not found`, - ); - } - if (revisions.length === 0) { - throw new SlideError( - `[slide.new] 'slide.revisions' field not found`, - ); - } - const revisions_ = revisions.map( - (x: { date: string; remark: string; isInternal?: boolean }, i: number) => { - const date = x.date ?? null; - if (!date) { - throw new SlideError( - `[slide.new] 'date' field not found`, - ); - } - const remark = x.remark ?? null; - if (!remark) { - throw new SlideError( - `[slide.new] 'remark' field not found`, - ); - } - const isInternal = x.isInternal ?? false; + const revisions = slide.revisions.map( + (rev, i) => { + const date = rev.date; + const remark = rev.remark; + const isInternal = rev.isInternal ?? false; return { number: i + 1, date: stringToDate(date), @@ -111,15 +48,20 @@ export function createNewSlideFromTomlRootObject( }; }, ); + if (revisions.length === 0) { + throw new SlideError( + `[slide.new] 'slide.revisions' field is empty`, + ); + } return { sourceFilePath: sourceFilePath, - uuid: uuid, - title: title, - event: event, - talkType: talkType, - slideLink: link, - tags: tags, - revisions: revisions_, + uuid: slide.uuid, + title: slide.title, + event: slide.event, + talkType: slide.talkType, + slideLink: slide.link, + tags: slide.tags, + revisions: revisions, }; } |
