Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions api/content.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,21 +22,21 @@ export const getItem = async (path: string): Promise<ItemDef | undefined> =>
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<string | undefined> => {
export const getInheritable = async (path: string, type: ResourceType): Promise<string | undefined> => {
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<string | undefined> =>
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> => {
Expand All @@ -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}} : {}
Expand Down
24 changes: 6 additions & 18 deletions ingest/inheritables.ts
Original file line number Diff line number Diff line change
@@ -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 => [
Expand Down
4 changes: 2 additions & 2 deletions ingest/updatePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,18 @@ export const extraCollectionMatter = {
} satisfies Record<string, unknown>;

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;
}[];
Loading