refactor: remove some duplicated code
This commit is contained in:
58
lib/crud.ts
Normal file
58
lib/crud.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import {
|
||||
createDocument,
|
||||
Document,
|
||||
getDocument,
|
||||
getDocuments,
|
||||
transformDocument,
|
||||
} from "@lib/documents.ts";
|
||||
import { Root } from "https://esm.sh/remark-frontmatter@4.0.1";
|
||||
|
||||
export function createCrud<T>(
|
||||
{ prefix, parse }: {
|
||||
prefix: string;
|
||||
parse: (doc: string, id: string) => T;
|
||||
},
|
||||
) {
|
||||
function pathFromId(id: string) {
|
||||
return `${prefix}${id}.md`;
|
||||
}
|
||||
|
||||
async function read(id: string) {
|
||||
const path = pathFromId(id);
|
||||
const content = await getDocument(path);
|
||||
|
||||
return parse(content, id);
|
||||
}
|
||||
function create(id: string, content: string | ArrayBuffer) {
|
||||
const path = pathFromId(id);
|
||||
return createDocument(path, content);
|
||||
}
|
||||
|
||||
async function update(id: string, updater: (r: Root) => Root) {
|
||||
const path = pathFromId(id);
|
||||
const content = await getDocument(path);
|
||||
const newDoc = transformDocument(content, updater);
|
||||
await createDocument(path, newDoc);
|
||||
}
|
||||
|
||||
async function readAll() {
|
||||
const allDocuments = await getDocuments();
|
||||
return Promise.all(
|
||||
allDocuments.filter((d) => {
|
||||
return d.name.startsWith(prefix) &&
|
||||
d.contentType === "text/markdown" &&
|
||||
!d.name.endsWith("index.md");
|
||||
}).map((doc) => {
|
||||
const id = doc.name.replace(prefix, "").replace(/\.md$/, "");
|
||||
return read(id);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
read,
|
||||
readAll,
|
||||
create,
|
||||
update,
|
||||
};
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import { unified } from "npm:unified";
|
||||
import remarkParse from "npm:remark-parse";
|
||||
import { unified } from "https://esm.sh/unified";
|
||||
import remarkParse from "https://esm.sh/remark-parse";
|
||||
import remarkStringify from "https://esm.sh/remark-stringify@10.0.3";
|
||||
import remarkFrontmatter, {
|
||||
Root,
|
||||
@ -7,10 +7,8 @@ import remarkFrontmatter, {
|
||||
import remarkRehype from "https://esm.sh/remark-rehype@10.1.0";
|
||||
import rehypeSanitize from "https://esm.sh/rehype-sanitize@5.0.1";
|
||||
import rehypeStringify from "https://esm.sh/rehype-stringify@9.0.3";
|
||||
import { parse } from "https://deno.land/std@0.194.0/yaml/mod.ts";
|
||||
import * as cache from "@lib/cache/documents.ts";
|
||||
|
||||
const SILVERBULLET_SERVER = Deno.env.get("SILVERBULLET_SERVER");
|
||||
import { SILVERBULLET_SERVER } from "@lib/env.ts";
|
||||
|
||||
export type Document = {
|
||||
name: string;
|
||||
@ -20,10 +18,6 @@ export type Document = {
|
||||
perm: string;
|
||||
};
|
||||
|
||||
export function parseFrontmatter(yaml: string) {
|
||||
return parse(yaml);
|
||||
}
|
||||
|
||||
export async function getDocuments(): Promise<Document[]> {
|
||||
const cachedDocuments = await cache.getDocuments();
|
||||
if (cachedDocuments) return cachedDocuments;
|
||||
@ -31,7 +25,7 @@ export async function getDocuments(): Promise<Document[]> {
|
||||
const headers = new Headers();
|
||||
headers.append("Accept", "application/json");
|
||||
|
||||
const response = await fetch(SILVERBULLET_SERVER + "/index.json", {
|
||||
const response = await fetch(`${SILVERBULLET_SERVER}/index.json`, {
|
||||
headers: headers,
|
||||
});
|
||||
|
||||
|
4
lib/env.ts
Normal file
4
lib/env.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export const SILVERBULLET_SERVER = Deno.env.get("SILVERBULLET_SERVER");
|
||||
export const REDIS_HOST = Deno.env.get("REDIS_HOST");
|
||||
export const REDIS_PASS = Deno.env.get("REDIS_PASS");
|
||||
export const TMDB_API_KEY = Deno.env.get("TMDB_API_KEY");
|
25
lib/errors.ts
Normal file
25
lib/errors.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||
|
||||
class DomainError extends Error {
|
||||
status = 500;
|
||||
render?: (ctx: MiddlewareHandlerContext) => void;
|
||||
constructor(public statusText = "Internal Server Error") {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
class NotFoundError extends DomainError {
|
||||
status = 404;
|
||||
constructor(public statusText = "Not Found") {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
class BadRequestError extends DomainError {
|
||||
status = 400;
|
||||
constructor(public statusText = "Bad Request") {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
export { BadRequestError, DomainError, NotFoundError };
|
7
lib/helpers.ts
Normal file
7
lib/helpers.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export function json(content: unknown) {
|
||||
const headers = new Headers();
|
||||
headers.append("Content-Type", "application/json");
|
||||
return new Response(JSON.stringify(content), {
|
||||
headers,
|
||||
});
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
import {
|
||||
parseDocument,
|
||||
parseFrontmatter,
|
||||
renderMarkdown,
|
||||
} from "@lib/documents.ts";
|
||||
import { parseDocument, renderMarkdown } from "@lib/documents.ts";
|
||||
import { parse } from "yaml";
|
||||
import { createCrud } from "@lib/crud.ts";
|
||||
|
||||
export type Movie = {
|
||||
id: string;
|
||||
@ -28,7 +26,7 @@ export function parseMovie(original: string, id: string): Movie {
|
||||
|
||||
for (const child of doc.children) {
|
||||
if (child.type === "yaml") {
|
||||
meta = parseFrontmatter(child.value) as Movie["meta"];
|
||||
meta = parse(child.value) as Movie["meta"];
|
||||
|
||||
if (meta["rating"] && typeof meta["rating"] === "string") {
|
||||
meta.rating = [...meta.rating?.matchAll("⭐")].length;
|
||||
@ -68,3 +66,11 @@ export function parseMovie(original: string, id: string): Movie {
|
||||
meta,
|
||||
};
|
||||
}
|
||||
|
||||
const crud = createCrud<Movie>({
|
||||
prefix: "Media/movies/",
|
||||
parse: parseMovie,
|
||||
});
|
||||
|
||||
export const getMovie = crud.read;
|
||||
export const getAllMovies = crud.readAll;
|
@ -3,11 +3,11 @@ import {
|
||||
getTextOfChild,
|
||||
getTextOfRange,
|
||||
parseDocument,
|
||||
parseFrontmatter,
|
||||
renderMarkdown,
|
||||
} from "@lib/documents.ts";
|
||||
|
||||
import { parseIngredient } from "npm:parse-ingredient";
|
||||
import { parse } from "yaml";
|
||||
import { parseIngredient } from "https://esm.sh/parse-ingredient";
|
||||
import { createCrud } from "@lib/crud.ts";
|
||||
|
||||
export type IngredientGroup = {
|
||||
name: string;
|
||||
@ -132,7 +132,7 @@ export function parseRecipe(original: string, id: string): Recipe {
|
||||
let group: DocumentChild[] = [];
|
||||
for (const child of doc.children) {
|
||||
if (child.type === "yaml") {
|
||||
meta = parseFrontmatter(child.value) as Recipe["meta"];
|
||||
meta = parse(child.value) as Recipe["meta"];
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
@ -169,3 +169,13 @@ export function parseRecipe(original: string, id: string): Recipe {
|
||||
preparation: preparation ? renderMarkdown(preparation) : "",
|
||||
};
|
||||
}
|
||||
|
||||
const crud = createCrud<Recipe>({
|
||||
prefix: `Recipes/`,
|
||||
parse: parseRecipe,
|
||||
});
|
||||
|
||||
export const getAllRecipes = crud.readAll;
|
||||
export const getRecipe = crud.read;
|
||||
export const updateRecipe = crud.update;
|
||||
export const createRecipe = crud.create;
|
@ -2,3 +2,16 @@ export function formatDate(date: Date): string {
|
||||
const options = { year: "numeric", month: "long", day: "numeric" } as const;
|
||||
return new Intl.DateTimeFormat("en-US", options).format(date);
|
||||
}
|
||||
|
||||
export function safeFileName(inputString: string): string {
|
||||
// Convert the string to lowercase
|
||||
let fileName = inputString.toLowerCase();
|
||||
|
||||
// Replace spaces with underscores
|
||||
fileName = fileName.replace(/ /g, "_");
|
||||
|
||||
// Remove characters that are not safe for file names
|
||||
fileName = fileName.replace(/[^\w.-]/g, "");
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
Reference in New Issue
Block a user