From 96b4d913ed8e9f7405b4b514e787eecc6c758506 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Thu, 26 Sep 2024 12:27:03 +0800 Subject: [PATCH] chore: move the image metadata generation to schema.ts --- package-lock.json | 90 ++++++++++++++++++++-------------------- package.json | 2 +- src/content/config.ts | 8 +--- src/helpers/schema.ts | 95 +++++++++++++++++++++++++------------------ 4 files changed, 103 insertions(+), 92 deletions(-) diff --git a/package-lock.json b/package-lock.json index 17f87d1..7a8960d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,7 +28,7 @@ "@napi-rs/canvas": "^0.1.56", "@types/lodash": "^4.17.9", "@types/luxon": "^3.4.2", - "@types/node": "^22.6.1", + "@types/node": "^22.7.2", "@types/pg": "^8.11.10", "@types/qrcode-svg": "^1.1.5", "@types/unist": "^3.0.3", @@ -2290,44 +2290,44 @@ ] }, "node_modules/@shikijs/core": { - "version": "1.18.0", - "resolved": "https://registry.npmjs.org/@shikijs/core/-/core-1.18.0.tgz", - "integrity": "sha512-VK4BNVCd2leY62Nm2JjyxtRLkyrZT/tv104O81eyaCjHq4Adceq2uJVFJJAIof6lT1mBwZrEo2qT/T+grv3MQQ==", + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/@shikijs/core/-/core-1.19.0.tgz", + "integrity": "sha512-314J5MPdS1wzfjuD856MXvbAI2wN03ofMnUGkZ5ZDBOza/d38paLwd+YVyuKrrjxJ4hfPMjc4tRmPkXd6UDMPQ==", "license": "MIT", "dependencies": { - "@shikijs/engine-javascript": "1.18.0", - "@shikijs/engine-oniguruma": "1.18.0", - "@shikijs/types": "1.18.0", + "@shikijs/engine-javascript": "1.19.0", + "@shikijs/engine-oniguruma": "1.19.0", + "@shikijs/types": "1.19.0", "@shikijs/vscode-textmate": "^9.2.2", "@types/hast": "^3.0.4", "hast-util-to-html": "^9.0.3" } }, "node_modules/@shikijs/engine-javascript": { - "version": "1.18.0", - "resolved": "https://registry.npmjs.org/@shikijs/engine-javascript/-/engine-javascript-1.18.0.tgz", - "integrity": "sha512-qoP/aO/ATNwYAUw1YMdaip/YVEstMZEgrwhePm83Ll9OeQPuxDZd48szZR8oSQNQBT8m8UlWxZv8EA3lFuyI5A==", + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/@shikijs/engine-javascript/-/engine-javascript-1.19.0.tgz", + "integrity": "sha512-D1sioU61n7fLWfDzTC9JNS19zEYZMr7qxkSVzv6ziEWDxnwzy2PvYoKPedJV4qUf+2VnrYPSaArDz2W0XgGB7A==", "license": "MIT", "dependencies": { - "@shikijs/types": "1.18.0", + "@shikijs/types": "1.19.0", "@shikijs/vscode-textmate": "^9.2.2", "oniguruma-to-js": "0.4.3" } }, "node_modules/@shikijs/engine-oniguruma": { - "version": "1.18.0", - "resolved": "https://registry.npmjs.org/@shikijs/engine-oniguruma/-/engine-oniguruma-1.18.0.tgz", - "integrity": "sha512-B9u0ZKI/cud+TcmF8Chyh+R4V5qQVvyDOqXC2l2a4x73PBSBc6sZ0JRAX3eqyJswqir6ktwApUUGBYePdKnMJg==", + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/@shikijs/engine-oniguruma/-/engine-oniguruma-1.19.0.tgz", + "integrity": "sha512-/JxwIefNVLGB4EmpB8i6P4JB/oVYRuzSixbqvx7m6iPW0lQ1T97c/0wmA+JlKbngEiExckSuPwa48fajlShB7A==", "license": "MIT", "dependencies": { - "@shikijs/types": "1.18.0", + "@shikijs/types": "1.19.0", "@shikijs/vscode-textmate": "^9.2.2" } }, "node_modules/@shikijs/types": { - "version": "1.18.0", - "resolved": "https://registry.npmjs.org/@shikijs/types/-/types-1.18.0.tgz", - "integrity": "sha512-O9N36UEaGGrxv1yUrN2nye7gDLG5Uq0/c1LyfmxsvzNPqlHzWo9DI0A4+fhW2y3bGKuQu/fwS7EPdKJJCowcVA==", + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/@shikijs/types/-/types-1.19.0.tgz", + "integrity": "sha512-NZvVp3k1bP4MTRUbmnkGhYzPdoNMjNLSAwczMRUbtUl4oj2LlNRNbwERyeIyJt56Ac9fvPVZ2nn13OXk86E5UQ==", "license": "MIT", "dependencies": { "@shikijs/vscode-textmate": "^9.2.2", @@ -2474,9 +2474,9 @@ } }, "node_modules/@types/node": { - "version": "22.6.1", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.6.1.tgz", - "integrity": "sha512-V48tCfcKb/e6cVUigLAaJDAILdMP0fUW6BidkPK4GpGjXcfbnoHasCZDwz3N3yVt5we2RHm4XTQCpv0KJz9zqw==", + "version": "22.7.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.7.2.tgz", + "integrity": "sha512-866lXSrpGpgyHBZUa2m9YNWqHDjjM0aBTJlNtYaGEw4rqY/dcD7deRVTbBBAJelfA7oaGDbNftXF/TL/A6RgoA==", "devOptional": true, "license": "MIT", "dependencies": { @@ -3257,9 +3257,9 @@ } }, "node_modules/browserslist": { - "version": "4.23.3", - "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.23.3.tgz", - "integrity": "sha512-btwCFJVjI4YWDNfau8RhZ+B1Q/VLoUITrm3RlP6y1tYGWIOa+InuYiRGXUBXo8nA1qKmHMyLB/iVQg5TT4eFoA==", + "version": "4.24.0", + "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.24.0.tgz", + "integrity": "sha512-Rmb62sR1Zpjql25eSanFGEhAxcFwfA1K0GuQcLoaJBAcENegrQut3hYdhXFF1obQfiDyqIW/cLM5HSJ/9k884A==", "funding": [ { "type": "opencollective", @@ -3276,8 +3276,8 @@ ], "license": "MIT", "dependencies": { - "caniuse-lite": "^1.0.30001646", - "electron-to-chromium": "^1.5.4", + "caniuse-lite": "^1.0.30001663", + "electron-to-chromium": "^1.5.28", "node-releases": "^2.0.18", "update-browserslist-db": "^1.1.0" }, @@ -3301,9 +3301,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001663", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001663.tgz", - "integrity": "sha512-o9C3X27GLKbLeTYZ6HBOLU1tsAcBZsLis28wrVzddShCS16RujjHp9GDHKZqrB3meE0YjhawvMFsGb/igqiPzA==", + "version": "1.0.30001664", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001664.tgz", + "integrity": "sha512-AmE7k4dXiNKQipgn7a2xg558IRqPN3jMQY/rOsbxDhrd0tyChwbITBfiwtnqz8bi2M5mIWbxAYBvk7W7QBUS2g==", "funding": [ { "type": "opencollective", @@ -3828,9 +3828,9 @@ } }, "node_modules/devalue": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/devalue/-/devalue-5.0.0.tgz", - "integrity": "sha512-gO+/OMXF7488D+u3ue+G7Y4AA3ZmUnB3eHJXmBTgNHvr4ZNzl36A0ZtG+XCRNYCkYx/bFmw4qtkoFLa+wSrwAA==", + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/devalue/-/devalue-5.1.1.tgz", + "integrity": "sha512-maua5KUiapvEwiEAe+XnlZ3Rh0GD+qI1J/nb9vrJc3muPXvcF/8gXYTWF76+5DAqHyDUtOIImEuo0YKE9mshVw==", "license": "MIT" }, "node_modules/devlop": { @@ -3998,9 +3998,9 @@ "license": "MIT" }, "node_modules/electron-to-chromium": { - "version": "1.5.28", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.28.tgz", - "integrity": "sha512-VufdJl+rzaKZoYVUijN13QcXVF5dWPZANeFTLNy+OSpHdDL5ynXTF35+60RSBbaQYB1ae723lQXHCrf4pyLsMw==", + "version": "1.5.29", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.29.tgz", + "integrity": "sha512-PF8n2AlIhCKXQ+gTpiJi0VhcHDb69kYX4MtCiivctc2QD3XuNZ/XIOlbGzt7WAjjEev0TtaH6Cu3arZExm5DOw==", "license": "ISC" }, "node_modules/emmet": { @@ -8181,15 +8181,15 @@ } }, "node_modules/shiki": { - "version": "1.18.0", - "resolved": "https://registry.npmjs.org/shiki/-/shiki-1.18.0.tgz", - "integrity": "sha512-8jo7tOXr96h9PBQmOHVrltnETn1honZZY76YA79MHheGQg55jBvbm9dtU+MI5pjC5NJCFuA6rvVTLVeSW5cE4A==", + "version": "1.19.0", + "resolved": "https://registry.npmjs.org/shiki/-/shiki-1.19.0.tgz", + "integrity": "sha512-Ng7Gd6XgWFLsv4Z3so65hOyXjV78qz1M117MuZHwdPQD6fgb5wR2IoLMvSlM/Ml14EXH7n+/YxIpTD74i7kDdw==", "license": "MIT", "dependencies": { - "@shikijs/core": "1.18.0", - "@shikijs/engine-javascript": "1.18.0", - "@shikijs/engine-oniguruma": "1.18.0", - "@shikijs/types": "1.18.0", + "@shikijs/core": "1.19.0", + "@shikijs/engine-javascript": "1.19.0", + "@shikijs/engine-oniguruma": "1.19.0", + "@shikijs/types": "1.19.0", "@shikijs/vscode-textmate": "^9.2.2", "@types/hast": "^3.0.4" } @@ -8869,9 +8869,9 @@ } }, "node_modules/vite": { - "version": "5.4.7", - "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.7.tgz", - "integrity": "sha512-5l2zxqMEPVENgvzTuBpHer2awaetimj2BGkhBPdnwKbPNOlHsODU+oiazEZzLK7KhAnOrO+XGYJYn4ZlUhDtDQ==", + "version": "5.4.8", + "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.8.tgz", + "integrity": "sha512-FqrItQ4DT1NC4zCUqMB4c4AZORMKIa0m8/URVCZ77OZ/QSNeJ54bU1vrFADbDsuwfIPcgknRkmqakQcgnL4GiQ==", "license": "MIT", "dependencies": { "esbuild": "^0.21.3", diff --git a/package.json b/package.json index 01c4022..91afbec 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "@napi-rs/canvas": "^0.1.56", "@types/lodash": "^4.17.9", "@types/luxon": "^3.4.2", - "@types/node": "^22.6.1", + "@types/node": "^22.7.2", "@types/pg": "^8.11.10", "@types/qrcode-svg": "^1.1.5", "@types/unist": "^3.0.3", diff --git a/src/content/config.ts b/src/content/config.ts index 2f4a9ed..7cbb222 100644 --- a/src/content/config.ts +++ b/src/content/config.ts @@ -1,4 +1,3 @@ -import { imageMetadata } from '@/helpers/images'; import { urlJoin } from '@/helpers/tools'; import options from '@/options'; import { defineCollection, z } from 'astro:content'; @@ -15,12 +14,7 @@ const slug = () => .max(200) .regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/i, 'Invalid slug'); -const image = (fallbackImage: string) => - z - .string() - .optional() - .default(fallbackImage) - .transform(async (arg) => await imageMetadata(arg)); +const image = (fallbackImage: string) => z.string().optional().default(fallbackImage); // Categories Collection const categoriesCollection = defineCollection({ diff --git a/src/helpers/schema.ts b/src/helpers/schema.ts index 7408977..5a10051 100644 --- a/src/helpers/schema.ts +++ b/src/helpers/schema.ts @@ -1,4 +1,5 @@ import { defaultCover } from '@/content/config.ts'; +import { imageMetadata, type Image } from '@/helpers/images'; import options from '@/options'; import { getCollection, getEntry, type Render } from 'astro:content'; @@ -10,16 +11,22 @@ const postsCollection = await getCollection('posts'); const tagsCollection = await getCollection('tags'); // Redefine the types from the astro content. -export type Category = (typeof categoriesCollection)[number]['data'] & { counts: number; permalink: string }; +export type Category = Omit<(typeof categoriesCollection)[number]['data'], 'cover'> & { + counts: number; + permalink: string; + cover: Image; +}; export type Friend = (typeof friendsCollection)[number]['data'][number]; -export type Page = (typeof pagesCollection)[number]['data'] & { +export type Page = Omit<(typeof pagesCollection)[number]['data'], 'cover'> & { slug: string; permalink: string; + cover: Image; render: () => Render['.mdx']; }; -export type Post = (typeof postsCollection)[number]['data'] & { +export type Post = Omit<(typeof postsCollection)[number]['data'], 'cover'> & { slug: string; permalink: string; + cover: Image; render: () => Render['.mdx']; raw: () => Promise; }; @@ -28,42 +35,52 @@ export type Tag = (typeof tagsCollection)[number]['data'][number] & { counts: nu // Translate the Astro content into the original content for dealing with different configuration types. export const friends: Friend[] = friendsCollection[0].data; // Override the website for local debugging -export const pages: Page[] = pagesCollection - .filter((page) => page.data.published || !options.isProd()) - .map((page) => ({ - slug: page.slug, - permalink: `/${page.slug}`, - render: async () => { - const entry = await getEntry('pages', page.slug); - return entry.render(); - }, - ...page.data, - })); -export const posts: Post[] = postsCollection - .filter((post) => post.data.published || !options.isProd()) - .map((post) => ({ - slug: post.slug, - permalink: `/posts/${post.slug}`, - render: async () => { - const entry = await getEntry('posts', post.slug); - return entry.render(); - }, - raw: async () => { - const entry = await getEntry('posts', post.slug); - return entry.body; - }, - ...post.data, - })) - .sort((left: Post, right: Post) => { - const a = left.date.getTime(); - const b = right.date.getTime(); - return options.settings.post.sort === 'asc' ? a - b : b - a; - }); -export const categories: Category[] = categoriesCollection.map((cat) => ({ - counts: posts.filter((post) => post.category === cat.data.name).length, - permalink: `/cats/${cat.data.slug}`, - ...cat.data, -})); +export const pages: Page[] = await Promise.all( + pagesCollection + .filter((page) => page.data.published || !options.isProd()) + .map(async (page) => ({ + slug: page.slug, + permalink: `/${page.slug}`, + render: async () => { + const entry = await getEntry('pages', page.slug); + return entry.render(); + }, + ...page.data, + cover: await imageMetadata(page.data.cover), + })), +); +export const posts: Post[] = ( + await Promise.all( + postsCollection + .filter((post) => post.data.published || !options.isProd()) + .map(async (post) => ({ + slug: post.slug, + permalink: `/posts/${post.slug}`, + render: async () => { + const entry = await getEntry('posts', post.slug); + return entry.render(); + }, + raw: async () => { + const entry = await getEntry('posts', post.slug); + return entry.body; + }, + ...post.data, + cover: await imageMetadata(post.data.cover), + })), + ) +).sort((left: Post, right: Post) => { + const a = left.date.getTime(); + const b = right.date.getTime(); + return options.settings.post.sort === 'asc' ? a - b : b - a; +}); +export const categories: Category[] = await Promise.all( + categoriesCollection.map(async (cat) => ({ + counts: posts.filter((post) => post.category === cat.data.name).length, + permalink: `/cats/${cat.data.slug}`, + ...cat.data, + cover: await imageMetadata(cat.data.cover), + })), +); export const tags: Tag[] = tagsCollection[0].data.map((tag) => ({ counts: posts.filter((post) => post.tags.includes(tag.name)).length, permalink: `/tags/${tag.slug}`,