From 81ddec2e15ade7fbf68956054ada47395c6b233f Mon Sep 17 00:00:00 2001 From: grassblock Date: Tue, 19 Aug 2025 21:16:09 +0800 Subject: [PATCH 1/3] feat: add optional 'updated' front matter for more precise updated time --- src/content/posts/_schemas.ts | 23 ++++++++++++----------- src/pages/blog/[...slug].astro | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/content/posts/_schemas.ts b/src/content/posts/_schemas.ts index 431b29d..3337edd 100644 --- a/src/content/posts/_schemas.ts +++ b/src/content/posts/_schemas.ts @@ -1,14 +1,15 @@ -import { z, reference } from 'astro:content'; +import {z, reference} from 'astro:content'; // @ts-ignore -export const posts = ({ image }) => z.object({ - title: z.string(), - description: z.string().optional(), - draft: z.boolean().optional().default(false), - summary: z.string().optional(), - date: z.coerce.date(), - categories: z.union([z.array(z.string()), z.string()]).transform(val => Array.isArray(val) ? val : [val]).default(['uncategorized']), - tags: z.array(z.string()).optional(), - cover: image().optional(), - author: z.union([z.array(reference('authors')), reference('authors')]).optional(), +export const posts = ({image}) => z.object({ + title: z.string(), + description: z.string().optional(), + draft: z.boolean().optional().default(false), + summary: z.string().optional(), + date: z.coerce.date(), + updated: z.coerce.date().optional(), + categories: z.union([z.array(z.string()), z.string()]).transform(val => Array.isArray(val) ? val : [val]).default(['uncategorized']), + tags: z.array(z.string()).optional(), + cover: image().optional(), + author: z.union([z.array(reference('authors')), reference('authors')]).optional(), }); \ No newline at end of file diff --git a/src/pages/blog/[...slug].astro b/src/pages/blog/[...slug].astro index 9b14a75..3187d5c 100644 --- a/src/pages/blog/[...slug].astro +++ b/src/pages/blog/[...slug].astro @@ -37,7 +37,7 @@ const wordcount = remarkPluginFrontmatter.wordcount; const lastUpdated = remarkPluginFrontmatter.lastModified; const pubDate = new Date(entry.data.date).toISOString().split('T')[0] -const lastUpdatedDate = new Date(lastUpdated).toISOString().split('T')[0] +const lastUpdatedDate = entry.data.updated ? new Date(entry.data.updated).toISOString().split('T')[0] : new Date(lastUpdated).toISOString().split('T')[0]; // Get author data const authorData = await Promise.all((author).map((singleAuthor) => getEntry(singleAuthor).then(authorEntry => authorEntry?.data))) From 127e297e753d8132494f6ed5524224ddb2e34821 Mon Sep 17 00:00:00 2001 From: grassblock Date: Tue, 19 Aug 2025 21:17:02 +0800 Subject: [PATCH 2/3] fix: article cover not displaying and potential errors --- src/pages/blog/[...slug].astro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/blog/[...slug].astro b/src/pages/blog/[...slug].astro index 3187d5c..a4c5164 100644 --- a/src/pages/blog/[...slug].astro +++ b/src/pages/blog/[...slug].astro @@ -46,7 +46,7 @@ const authorInfo = authorData.includes(undefined) ? [{data: siteConfig.defaultA // get featured image and use it as og:image // use the custom cover image if it exists, otherwise use the featured image file in the same directory const featuredImages = import.meta.glob(`/src/content/posts/*/featured.{avif,png,jpg,jpeg,webp}`,{import:'default',eager:true}); -const customFeaturedImage = entry.data.cover?.src +const customFeaturedImage = entry.data.cover const matchedImage = Object.keys(featuredImages).find(path => path.includes(slug)); let matchedImage_src; if (matchedImage && !customFeaturedImage) { From ff91324a79333b42157157f3f875aaf7eea48b53 Mon Sep 17 00:00:00 2001 From: grassblock Date: Tue, 19 Aug 2025 21:47:14 +0800 Subject: [PATCH 3/3] feat: code copying and guide for alternative syntax highlighting methods --- README.md | 20 ++++++++ src/components/CodeScript.astro | 84 +++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/components/CodeScript.astro diff --git a/README.md b/README.md index f497770..9776b51 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,26 @@ To automatically output the text version when visiting the site via `curl`, you /blog/${1}.txt ``` 3. If you are using other web servers, you can use functions like `rewrite` or `redirect` by user agent (HTTP header) to achieve the same effect. +### Alternative Syntax Highlighting +Apart from the default shiki for highlighting, You can use [Prism](https://prismjs.com/) as code highlighting engine, see [Syntax Highlighting in Astro Docs](https://docs.astro.build/en/guides/syntax-highlighting/#add-a-prism-stylesheet) for details. + +To simply add features like code copying over the default Shiki formatter, you can simply import the `src/components/CodeScript.astro` to the Layout. + +You can also use [Expressive Code](https://expressive-code.com/) to get features like code copying over the syntax highlighting: +> Warning: Expressive Code will load JavaScript by default. This will not follow your `noClientJavaScript` settings. + +1. Install Expressive Code: + ```shell + pnpm astro add astro-expressive-code + ``` + You can answer all Yes to prompts as we will modify the config later. +2. Edit `astro.config.mjs`: + ```diff + - integrations: [sitemap(), mdx(), partytown(), expressiveCode()], + + integrations: [sitemap(), expressiveCode(), mdx(), partytown()], + ``` +3. Create `ec.config.mjs` in the project root and adjust the config to your liking. To make things easier you can use [this config file used by the author](https://raw.githubusercontent.com/BlockG-ws/gb-lab/refs/heads/master/ec.config.mjs) + ## 👀 Want to learn more? See the post [🕊](). I hope you like it. 💜 diff --git a/src/components/CodeScript.astro b/src/components/CodeScript.astro new file mode 100644 index 0000000..612466e --- /dev/null +++ b/src/components/CodeScript.astro @@ -0,0 +1,84 @@ +--- +// from https://www.jaluwibowo.id/blog/en/custom-syntax-highlight-and-copy-feature/ +--- + + \ No newline at end of file