From a27e9046c06fade8ce1892f2ceb98f2a62a6b1e1 Mon Sep 17 00:00:00 2001 From: Max Richter Date: Wed, 22 Oct 2025 12:58:18 +0200 Subject: [PATCH] fix: some stuff --- src/helpers/image.ts | 30 +++++------------- src/helpers/markdownToText.ts | 31 ++++++++++--------- src/helpers/memorium.ts | 1 - .../[resourceType]/[resourceName].astro | 10 +++--- .../resources/[resourceType]/index.astro | 10 +++++- src/pages/resources/resources.ts | 4 +-- 6 files changed, 39 insertions(+), 47 deletions(-) diff --git a/src/helpers/image.ts b/src/helpers/image.ts index e1d3144..73ef2e7 100644 --- a/src/helpers/image.ts +++ b/src/helpers/image.ts @@ -1,19 +1,11 @@ import { rgbaToThumbHash } from "thumbhash"; -import ExifReader from 'exifreader'; +import ExifReader from "exifreader"; import type { ImageMetadata } from "astro"; +import sharp from "sharp"; -let s: typeof import("sharp") | undefined; -async function getSharp(): Promise { - if (s) return s; - s = (await import("sharp")).default; - return s; -} - -export async function generateThumbHash(image: ImageMetadata & { fsPath?: string }) { - - const sharp = await getSharp(); - if (!sharp) return; - +export async function generateThumbHash( + image: ImageMetadata & { fsPath?: string }, +) { const scaleFactor = 100 / Math.max(image.width, image.height); const smallWidth = Math.floor(image.width * scaleFactor); @@ -30,10 +22,9 @@ export async function generateThumbHash(image: ImageMetadata & { fsPath?: string const buffer = rgbaToThumbHash(smallWidth, smallHeight, smallImg); return Buffer.from(buffer).toString("base64"); } catch (error) { - console.log(`Could not generate thumbhash for ${image.fsPath}`, error) - return "" + console.log(`Could not generate thumbhash for ${image.fsPath}`, error); + return ""; } - } const allowedExif = [ @@ -54,11 +45,8 @@ const allowedExif = [ export async function getExifData(image: ImageMetadata) { if (image.format === "svg") return undefined; // SVGs don't have EXIF data") - const sharp = await getSharp(); - if (!sharp) return; const imagePath = (image as ImageMetadata & { fsPath: string }).fsPath; try { - const buffer = await sharp(imagePath).toBuffer(); const tags = await ExifReader.load(buffer, { async: true }); @@ -74,9 +62,7 @@ export async function getExifData(image: ImageMetadata) { return hasExif ? out : undefined; } catch (error) { - console.log(`Error reading EXIF data from ${imagePath}`, error); - return undefined + return undefined; } - } diff --git a/src/helpers/markdownToText.ts b/src/helpers/markdownToText.ts index f4ba65d..92f250b 100644 --- a/src/helpers/markdownToText.ts +++ b/src/helpers/markdownToText.ts @@ -1,25 +1,26 @@ -import MarkdownIt from 'markdown-it'; +import MarkdownIt from "markdown-it"; const parser = new MarkdownIt(); -export default function markdownToText(markdown: string): string { +export default function markdownToText(markdown: string = ""): string { return parser .render(markdown) - .split('\n') + .split("\n") .map((str) => str.trim()) .map((str) => { - return str.replace(/<\/?[^>]+(>|$)/g, '').split('\n'); + return str.replace(/<\/?[^>]+(>|$)/g, "").split("\n"); }) .flat() - .filter((str) => !str.startsWith("import") - && !str.startsWith("export") - && !str.startsWith("#") - && !str.startsWith("const") - && !str.startsWith("function") - && !str.startsWith("export") - && !str.startsWith("import") - && !str.startsWith("<") - && !str.startsWith("let") - && str.length > 0 + .filter((str) => + !str.startsWith("import") && + !str.startsWith("export") && + !str.startsWith("#") && + !str.startsWith("const") && + !str.startsWith("function") && + !str.startsWith("export") && + !str.startsWith("import") && + !str.startsWith("<") && + !str.startsWith("let") && + str.length > 0 ) - .join(' '); + .join(" "); } diff --git a/src/helpers/memorium.ts b/src/helpers/memorium.ts index af7b5cd..1e04258 100644 --- a/src/helpers/memorium.ts +++ b/src/helpers/memorium.ts @@ -41,7 +41,6 @@ export async function listResource( } } catch (_e) { console.log("Failed to get: ", url); - console.log(_e); return; } } diff --git a/src/pages/resources/[resourceType]/[resourceName].astro b/src/pages/resources/[resourceType]/[resourceName].astro index d926f9e..95a08b4 100644 --- a/src/pages/resources/[resourceType]/[resourceName].astro +++ b/src/pages/resources/[resourceType]/[resourceName].astro @@ -26,13 +26,13 @@ export async function getStaticPaths() { ); return paths.flat().filter(Boolean); - }catch(err){ - return [] + } catch (err) { + return []; } } const resource = await memorium.listResource( - `/${resourceType}/${resourceName}.md`, + `${resourceType}/${resourceName}.md`, ); --- @@ -59,7 +59,7 @@ const resource = await memorium.listResource(

Ingredients

    { - resource.content.recipeIngredient.map((ingredient) => ( + resource?.content.recipeIngredient.map((ingredient) => (
  • {markdownToText(ingredient)}
  • )) } @@ -68,7 +68,7 @@ const resource = await memorium.listResource(

    Steps

      { - resource.content.recipeInstructions.map((ingredient) => ( + resource?.content.recipeInstructions.map((ingredient) => (
    1. {markdownToText(ingredient)}
    2. )) } diff --git a/src/pages/resources/[resourceType]/index.astro b/src/pages/resources/[resourceType]/index.astro index e411a84..d1d7790 100644 --- a/src/pages/resources/[resourceType]/index.astro +++ b/src/pages/resources/[resourceType]/index.astro @@ -6,7 +6,15 @@ import { resources as resourceTypes } from "../resources.ts"; const { resourceType } = Astro.params; -const resources = await memorium.listResource(resourceType); +async function safeGetResource(resType) { + try { + return await memorium.listResource(resourceType); + } catch (error) { + return {content:[]}; + } +} + +const resources = await safeGetResource(resourceType); export async function getStaticPaths() { return resourceTypes.map((type: any) => { diff --git a/src/pages/resources/resources.ts b/src/pages/resources/resources.ts index 7c23f47..a8b5d46 100644 --- a/src/pages/resources/resources.ts +++ b/src/pages/resources/resources.ts @@ -3,7 +3,6 @@ const collection = "resources"; type Resource = { id: string; collection: string; - body: string; data: { title: string; icon: string; @@ -31,9 +30,8 @@ type Resource = { // }; const recipes = { - id: "Recipes", + id: "recipes", collection, - body: "Recipes", data: { title: "Recipes", icon: "🍲",