Skip to content

Commit

Permalink
URL to slug helper
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenwf committed Oct 26, 2023
1 parent 479cabf commit 4d25e49
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/dev/node-client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { IIIFRC } from "../util/get-config.ts";
import { join } from "node:path";
import { readFile } from "node:fs/promises";
import { compileReverseSlugConfig } from "../util/slug-engine.ts";
import {
compileReverseSlugConfig,
compileSlugConfig,
} from "../util/slug-engine.ts";

export function create(folderPath: string) {
const endpoints = {
Expand Down Expand Up @@ -74,6 +77,27 @@ export function create(folderPath: string) {
}
}

async function urlToSlug(url: string, type?: string) {
const slugs = await getSlugs();
const slugFns = Object.fromEntries(
Object.entries(slugs || {})
.map(([key, value]) => {
if (type && value.type !== type) {
return null as any;
}
return [key, { info: value, matches: compileSlugConfig(value) }];
})
.filter((t) => t !== null),
);

for (const slugFn of Object.values(slugFns || {})) {
const [matches] = slugFn.matches(url);
if (matches) {
return matches;
}
}
}

async function loadTopicType(name: string) {
const pathToTopic = join(folderPath, "topics", name);

Expand Down Expand Up @@ -155,5 +179,8 @@ export function create(folderPath: string) {
loadManifest,
loadTopicType,
loadTopic,
// Helpers.
urlToSlug,
resolveFromSlug,
};
}

0 comments on commit 4d25e49

Please sign in to comment.