From 5eb457751c7de12a1a76d3e3c14de1d363c1bfa3 Mon Sep 17 00:00:00 2001 From: janezd Date: Tue, 16 Jun 2026 10:37:49 +0200 Subject: [PATCH] Inheritables: refactor, harden --- api/content.ts | 14 +++++++------- ingest/inheritables.ts | 24 ++++++------------------ ingest/updatePaths.ts | 4 ++-- types/index.ts | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 27 deletions(-) diff --git a/api/content.ts b/api/content.ts index b8b2a54..f0af73c 100644 --- a/api/content.ts +++ b/api/content.ts @@ -1,7 +1,7 @@ "use server"; import db from "@/utils/db"; -import { LinkDesc } from "@/types"; +import { LinkDesc, resources, ResourceType } from "@/types"; export type ItemDesc = { id: number; @@ -22,21 +22,21 @@ export const getItem = async (path: string): Promise => await db.get(`SELECT 'book' as type, id, title FROM books WHERE path = ?`, [path]) || await db.get(`SELECT 'collection' as type, id, title FROM collections WHERE path = ?`, [path]); -export const getInheritable = async (path: string, type: string, postName: string | undefined = undefined): Promise => { +export const getInheritable = async (path: string, type: ResourceType): Promise => { const row = await db.get(` SELECT path FROM inheritables WHERE type = ? AND ? LIKE path || '%' ORDER BY LENGTH(path) DESC LIMIT 1;`, [type, path ? path + "/" : ""]); + const {file} = resources[type]; return row === undefined ? undefined - : !postName ? row?.path - : row.path ? `/${row.path}/${postName}` - : `/${postName}`; + : row.path ? `/${row.path}/${file}` + : `/${file}`; } export const getCss = async (path: string): Promise => - await getInheritable(path, "css", "style.css"); + await getInheritable(path, "css"); export const getMetadata = async (path: string): Promise<{title?: string, description?: string, icons?: {icon: string}} | undefined> => { @@ -48,7 +48,7 @@ export const getMetadata = async (path: string): SELECT title, subtitle as description FROM ${item.type}s WHERE id = ?`, [item.id]); - const icon = await getInheritable(path, "favicon", "favicon.png"); + const icon = await getInheritable(path, "favicon"); return { title, description, ...icon ? {icons: {icon}} : {} diff --git a/ingest/inheritables.ts b/ingest/inheritables.ts index 00c4568..3130071 100644 --- a/ingest/inheritables.ts +++ b/ingest/inheritables.ts @@ -1,25 +1,13 @@ import { isDirectory, pathExists, readPublicDir } from "./paths"; - - -const resources = [ - {type: "favicon", file: "favicon.png"}, - {type: "css", file: "style.css"}, - {type: "defaults", file: "defaults.yml", db: false}, -]; - -export type InheritableResources = { - type: string, - path: string, - db?: boolean -}[]; +import { InheritableResources, ResourceType, resources } from "@/types"; export const inheritableResourcesFromPath = (prefix: string): InheritableResources => - resources - .filter(({file}) => pathExists(prefix, file)) - .map(({type, db}) => ({ - type, + Object.entries(resources) + .filter(([, {file}]) => pathExists(prefix, file)) + .map(([type, {db}]) => ({ + type: type as ResourceType, path: prefix, - db: db ?? true + db })); export const getInheritableResources = (prefix: string): InheritableResources => [ diff --git a/ingest/updatePaths.ts b/ingest/updatePaths.ts index 9c5724b..cbafe4b 100644 --- a/ingest/updatePaths.ts +++ b/ingest/updatePaths.ts @@ -9,10 +9,10 @@ import { parseBook, RawBookDef, RawChapterDef } from "./book"; import { gatherRedirections, updateRedirections } from "./redirections"; import { catchErrors, hasError, logError, logWarning, printWarnings, resetError } from "./errors"; import { MailPath } from "@/ingest/mail"; -import { InheritableResources, inheritableResourcesFromPath } from "@/ingest/inheritables"; +import { inheritableResourcesFromPath } from "@/ingest/inheritables"; import {joinedPath} from "@/ingest/paths"; import {load} from "js-yaml"; -import { UnlockChaptersOnAnswersOptions } from "@/types"; +import { UnlockChaptersOnAnswersOptions, InheritableResources } from "@/types"; const extractBookQuestions = async ( book: RawBookDef, diff --git a/types/index.ts b/types/index.ts index efbe66e..25c0931 100644 --- a/types/index.ts +++ b/types/index.ts @@ -91,3 +91,18 @@ export const extraCollectionMatter = { } satisfies Record; export type LinkDesc = { href: string, title: string } | false; + + +export const resources = { + favicon: {file: "favicon.png", db: true}, + css: {file: "style.css", db: true}, + defaults: {file: "defaults.yml", db: false}, +} as const; + +export type ResourceType = keyof typeof resources; + +export type InheritableResources = { + type: ResourceType; + path: string; + db: boolean; +}[];