From b95cfcc5b41cc6cafa928785d059171365f23519 Mon Sep 17 00:00:00 2001 From: Max Richter Date: Wed, 2 Aug 2023 18:13:31 +0200 Subject: [PATCH] fix: some issues --- fresh.gen.ts | 12 +++++------ lib/resource/articles.ts | 2 ++ lib/resource/movies.ts | 8 +++++++- lib/resource/recipes.ts | 23 ++++++++++++++++----- routes/api/query/index.ts | 42 +++++++++++++++++++++++++++++++++++++++ routes/api/test.ts | 30 ---------------------------- static/global.css | 6 +++--- 7 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 routes/api/query/index.ts delete mode 100644 routes/api/test.ts diff --git a/fresh.gen.ts b/fresh.gen.ts index d5e0848..3791f39 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -14,9 +14,9 @@ import * as $8 from "./routes/api/index.ts"; import * as $9 from "./routes/api/movies/[name].ts"; import * as $10 from "./routes/api/movies/enhance/[name].ts"; import * as $11 from "./routes/api/movies/index.ts"; -import * as $12 from "./routes/api/recipes/[name].ts"; -import * as $13 from "./routes/api/recipes/index.ts"; -import * as $14 from "./routes/api/test.ts"; +import * as $12 from "./routes/api/query/index.ts"; +import * as $13 from "./routes/api/recipes/[name].ts"; +import * as $14 from "./routes/api/recipes/index.ts"; import * as $15 from "./routes/api/tmdb/[id].ts"; import * as $16 from "./routes/api/tmdb/credits/[id].ts"; import * as $17 from "./routes/api/tmdb/query.ts"; @@ -47,9 +47,9 @@ const manifest = { "./routes/api/movies/[name].ts": $9, "./routes/api/movies/enhance/[name].ts": $10, "./routes/api/movies/index.ts": $11, - "./routes/api/recipes/[name].ts": $12, - "./routes/api/recipes/index.ts": $13, - "./routes/api/test.ts": $14, + "./routes/api/query/index.ts": $12, + "./routes/api/recipes/[name].ts": $13, + "./routes/api/recipes/index.ts": $14, "./routes/api/tmdb/[id].ts": $15, "./routes/api/tmdb/credits/[id].ts": $16, "./routes/api/tmdb/query.ts": $17, diff --git a/lib/resource/articles.ts b/lib/resource/articles.ts index b092964..8bb44a7 100644 --- a/lib/resource/articles.ts +++ b/lib/resource/articles.ts @@ -7,6 +7,7 @@ import { fixRenderedMarkdown } from "@lib/helpers.ts"; export type Article = { id: string; + type: "article"; content: string; name: string; tags: string[]; @@ -86,6 +87,7 @@ function parseArticle(original: string, id: string): Article { } return { + type: "article", id, name, tags, diff --git a/lib/resource/movies.ts b/lib/resource/movies.ts index 21f9abd..09e5e52 100644 --- a/lib/resource/movies.ts +++ b/lib/resource/movies.ts @@ -7,6 +7,7 @@ export type Movie = { id: string; name: string; description: string; + type: "movie"; tags: string[]; meta: { date: Date; @@ -27,7 +28,11 @@ export function parseMovie(original: string, id: string): Movie { for (const child of doc.children) { if (child.type === "yaml") { - meta = parse(child.value) as Movie["meta"]; + try { + meta = (parse(child.value) || {}) as Movie["meta"]; + } catch (_) { + // ignore here + } if (meta["rating"] && typeof meta["rating"] === "string") { meta.rating = [...meta.rating?.matchAll("⭐")].length; @@ -59,6 +64,7 @@ export function parseMovie(original: string, id: string): Movie { } return { + type: "movie", id, name, tags, diff --git a/lib/resource/recipes.ts b/lib/resource/recipes.ts index 6767edf..160b25a 100644 --- a/lib/resource/recipes.ts +++ b/lib/resource/recipes.ts @@ -8,6 +8,7 @@ import { import { parse } from "yaml"; import { parseIngredient } from "https://esm.sh/parse-ingredient"; import { createCrud } from "@lib/crud.ts"; +import { extractHashTags } from "@lib/string.ts"; export type IngredientGroup = { name: string; @@ -23,17 +24,20 @@ export type Ingredient = { export type Ingredients = (Ingredient | IngredientGroup)[]; export type Recipe = { + type: "recipe"; id: string; + name: string; + description?: string; + ingredients: Ingredients; + preparation?: string; + tags: string[]; meta?: { link?: string; image?: string; rating?: number; portion?: number; + author?: string; }; - name: string; - description?: string; - ingredients: Ingredients; - preparation?: string; }; function parseIngredientItem(listItem: DocumentChild): Ingredient | undefined { @@ -154,16 +158,25 @@ export function parseRecipe(original: string, id: string): Recipe { groups.push(group); } - const description = getTextOfRange(groups[0], original); + let description = getTextOfRange(groups[0], original); const ingredients = parseIngredients(groups[1]); const preparation = getTextOfRange(groups[2], original); + const tags = extractHashTags(description || ""); + if (description) { + for (const tag of tags) { + description = description.replace("#" + tag, ""); + } + } + return { + type: "recipe", id, meta, name, + tags, description: description ? renderMarkdown(description) : "", ingredients, preparation: preparation ? renderMarkdown(preparation) : "", diff --git a/routes/api/query/index.ts b/routes/api/query/index.ts new file mode 100644 index 0000000..ab19f6a --- /dev/null +++ b/routes/api/query/index.ts @@ -0,0 +1,42 @@ +import { Handlers } from "$fresh/server.ts"; +import { json } from "@lib/helpers.ts"; +import { getAllMovies, Movie } from "@lib/resource/movies.ts"; +import { Article, getAllArticles } from "@lib/resource/articles.ts"; +import { getAllRecipes, Recipe } from "@lib/resource/recipes.ts"; + +const isResource = ( + item: Movie | Article | Recipe | boolean, +): item is Movie | Article | Recipe => { + return !!item; +}; + +export const handler: Handlers = { + async GET(req) { + const url = new URL(req.url); + + const types = url.searchParams.get("type")?.split(", "); + + let resources = (await Promise.all([ + (!types || types.includes("movie")) && getAllMovies(), + (!types || types.includes("article")) && getAllArticles(), + (!types || types.includes("recipe")) && getAllRecipes(), + ])).flat().filter(isResource); + + const tags = url.searchParams?.get("tags")?.split(","); + if (tags?.length) { + resources = resources.filter((r) => { + return tags?.every((t) => r.tags.includes(t)); + }); + } + + const authors = url.searchParams?.get("author")?.split(","); + if (authors?.length) { + console.log({ authors }); + resources = resources.filter((r) => { + return r?.meta?.author && authors.includes(r?.meta?.author); + }); + } + + return json(resources); + }, +}; diff --git a/routes/api/test.ts b/routes/api/test.ts deleted file mode 100644 index 259f151..0000000 --- a/routes/api/test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { Handlers } from "$fresh/server.ts"; - -function GET() { - let timer: number | undefined = undefined; - const body = new ReadableStream({ - start(controller) { - timer = setInterval(() => { - const message = `It is ${new Date().toISOString()}\n`; - controller.enqueue(new TextEncoder().encode(message)); - }, 1000); - }, - - cancel() { - if (timer !== undefined) { - clearInterval(timer); - } - }, - }); - - return new Response(body, { - headers: { - "content-type": "text/plain", - "x-content-type-options": "nosniff", - }, - }); -} - -export const handler: Handlers = { - GET, -}; diff --git a/static/global.css b/static/global.css index 44e7cb1..4ff6709 100644 --- a/static/global.css +++ b/static/global.css @@ -15,9 +15,9 @@ a { } @media(min-width: 640px){ -.custom-grid { - grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) ; -} + .custom-grid { + grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) ; + } } .noisy-gradient::after {