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
27 changes: 14 additions & 13 deletions api/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ 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 getCss = async (path: string): Promise<string | undefined> => {
const cssPath = await db.get(`
export const getInheritable = async (path: string, type: string, postName: string | undefined = undefined): Promise<string | undefined> => {
const row = await db.get(`
SELECT path
FROM inheritables
WHERE type = 'css' AND ? LIKE path || '%'
WHERE type = ? AND ? LIKE path || '%'
ORDER BY LENGTH(path) DESC
LIMIT 1;`, [path]);
return cssPath && `/${cssPath.path}/style.css`;
LIMIT 1;`, [type, path ? path + "/" : ""]);
Comment thread
janezd marked this conversation as resolved.
return row === undefined ? undefined
: !postName ? row?.path
: row.path ? `/${row.path}/${postName}`
: `/${postName}`;
}

export const getCss = async (path: string): Promise<string | undefined> =>
await getInheritable(path, "css", "style.css");

export const getMetadata = async (path: string):
Promise<{title?: string, description?: string, icons?: {icon: string}} | undefined> => {
const item = await getItem(path);
Expand All @@ -42,15 +48,10 @@ export const getMetadata = async (path: string):
SELECT title, subtitle as description
FROM ${item.type}s
WHERE id = ?`, [item.id]);
const iconPath = await db.get(`
SELECT path
FROM inheritables
WHERE type = 'favicon' AND ? LIKE path || '%'
ORDER BY LENGTH(path) DESC
LIMIT 1;`, [path])
return {
const icon = await getInheritable(path, "favicon", "favicon.png");
return {
title, description,
...iconPath ? {icons: {icon: `/${iconPath.path}/favicon.png`}} : {}
...icon ? {icons: {icon}} : {}
};
}

Expand Down
5 changes: 4 additions & 1 deletion ingest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export async function updateDb(
&& statSync(joinedPath(entry)).isDirectory()
);
for(const prefix of prefixes) {
if (prefix === "/") {
continue;
}
let prevBuild = new Date(process.env.NEXT_PUBLIC_DEVELOPMENT && !force &&
(await db.get(
`SELECT MAX(timestamp) as time, path FROM builds WHERE path = ?`,
Expand Down Expand Up @@ -109,7 +112,7 @@ export async function updateDb(
}
}

if (!prefix && !check) {
if ((!prefix || prefixes.includes("/")) && !check) {
await updateRoot(db);
}
Comment thread
janezd marked this conversation as resolved.

Expand Down
17 changes: 10 additions & 7 deletions ingest/inheritables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ export type InheritableResources = {
db?: boolean
}[];

export const inheritableResourcesFromPath = (prefix: string): InheritableResources =>
resources
.filter(({file}) => pathExists(prefix, file))
.map(({type, db}) => ({
type,
path: prefix,
db: db ?? true
}));

export const getInheritableResources = (prefix: string): InheritableResources => [
...resources
.filter(({file}) => pathExists(prefix, file))
.map(({type, db}) => ({
type,
path: prefix,
db: db ?? true
})),
...inheritableResourcesFromPath(prefix),
...readPublicDir(prefix)
.map((subdir) => `${prefix}/${subdir}`)
.filter((path) => isDirectory(path))
Expand Down
31 changes: 17 additions & 14 deletions ingest/updatePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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 } from "@/ingest/inheritables";
import { InheritableResources, inheritableResourcesFromPath } from "@/ingest/inheritables";
import {joinedPath} from "@/ingest/paths";
import {load} from "js-yaml";
import { UnlockChaptersOnAnswersOptions } from "@/types";
Expand Down Expand Up @@ -625,8 +625,8 @@ const cleanup = async (
["chapters", "books", "collections", "inheritables", "loginmails"].map((table) =>
db.run(
`DELETE FROM ${table}
WHERE path LIKE ? || '%' AND lastBuildId <> ?`,
[pathPrefix ? pathPrefix + "/" : "", buildId]
WHERE (path = ? OR path LIKE ?) AND lastBuildId <> ?`,
[pathPrefix, pathPrefix ? pathPrefix + "/%" : "", buildId]
)
Comment thread
janezd marked this conversation as resolved.
)
);
Expand All @@ -640,6 +640,12 @@ const cleanup = async (
);
}

const getNewBuildId = async (db: Database, pathPrefix: string): Promise<number> =>
(await db.get(
`INSERT INTO builds (path) VALUES (?) RETURNING id`,
[pathPrefix || ""]
)).id;

export const updatePaths = async (
bookSlugs: string[][],
collectionSlugs: string[][],
Expand Down Expand Up @@ -683,10 +689,7 @@ export const updatePaths = async (
await db.exec("BEGIN TRANSACTION");
let buildId: number;
if (buildId_ === "new") {
buildId = (await db.get(
`INSERT INTO builds (path) VALUES (?) RETURNING id`,
[pathPrefix || ""]
)).id;
buildId = await getNewBuildId(db, pathPrefix);
}
else {
await db.run(
Expand Down Expand Up @@ -714,15 +717,15 @@ export const updatePaths = async (
};

export const updateRoot = async (db: Database) => {
const buildId = await getNewBuildId(db, "");

Comment thread
janezd marked this conversation as resolved.
const rootCollection = await parseCollection([], []);
if (rootCollection.frontmatter.public) {
Comment thread
janezd marked this conversation as resolved.
const buildId = (await db.get(
`INSERT INTO builds (path) VALUES (?) RETURNING id`,
[""])
).id;
await insertCollections([rootCollection], db, buildId);
}
else {
await db.run(`DELETE FROM collections WHERE path = ''`);
}

const resources = inheritableResourcesFromPath("");
await insertResourcePaths(resources, db, buildId);

await cleanup(db, "", buildId);
}
4 changes: 2 additions & 2 deletions scripts/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const startNext = () => {


program
.option("-p, --path <path>", "Top-level subdirectory to update/check", "")
.option("-p, --path <path>", "Top-level subdirectory to update/check; use `-p /` for root", "")
.option("--dev", "Run in development mode", false)
.option("--recreate", "Recreate the database from scratch", false)
.option("-f, --force", "Force parsing of unchanged files", false)
Expand All @@ -39,7 +39,7 @@ program
program.parse(process.argv);
const { path: prefix, dev, recreate, force, check } = program.opts();

if (prefix.includes("/") || prefix.includes("\\")) {
if (prefix !== "/" && (prefix.includes("/") || prefix.includes("\\"))) {
console.error("Error: The path may contain only a top-level directory name.");
process.exit(1);
}
Expand Down
Loading