From 00292744842c72576760be1759de1862113f7dd6 Mon Sep 17 00:00:00 2001 From: lew Date: Thu, 26 Mar 2026 22:02:40 +0000 Subject: [PATCH] feat: extracts the rss excerpt utility to use it for og:description on post pages --- www/src/lib/format.ts | 12 ++++++++++++ www/src/pages/[slug].astro | 5 +++-- www/src/pages/feed.xml.ts | 13 +------------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/www/src/lib/format.ts b/www/src/lib/format.ts index a011492..9628159 100644 --- a/www/src/lib/format.ts +++ b/www/src/lib/format.ts @@ -7,6 +7,18 @@ export function escapeHtml(str: string): string { .replace(/'/g, '''); } +export function excerpt(markdown: string | undefined, maxLen = 160): string { + if (!markdown) return ''; + return markdown + .replace(/^#+\s+.*$/gm, '') + .replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1') + .replace(/[*_~`]/g, '') + .replace(/:[a-z]+\[([^\]]*)\]/g, '$1') + .replace(/\n+/g, ' ') + .trim() + .slice(0, maxLen); +} + export function formatDate(date: Date): string { const d = String(date.getDate()).padStart(2, '0'); const m = String(date.getMonth() + 1).padStart(2, '0'); diff --git a/www/src/pages/[slug].astro b/www/src/pages/[slug].astro index caeea05..7974b9d 100644 --- a/www/src/pages/[slug].astro +++ b/www/src/pages/[slug].astro @@ -1,7 +1,7 @@ --- import { getCollection, render } from 'astro:content'; import Layout from '../layouts/Layout.astro'; -import { formatDate, formatListItem } from '../lib/format'; +import { formatDate, formatListItem, excerpt } from '../lib/format'; import { getSlug, enrichPostWithDates, enrichPostsWithDates, resolveRelatedPosts } from '../lib/md'; export async function getStaticPaths() { @@ -16,8 +16,9 @@ export async function getStaticPaths() { const { post, allPosts } = Astro.props; const { Content } = await render(post); const related = post.data.related ? resolveRelatedPosts(post.data.related, allPosts) : []; +const description = excerpt((post as any).body) || undefined; --- - +

{post.data.title}

diff --git a/www/src/pages/feed.xml.ts b/www/src/pages/feed.xml.ts index e01b520..55c43cc 100644 --- a/www/src/pages/feed.xml.ts +++ b/www/src/pages/feed.xml.ts @@ -3,18 +3,7 @@ import { getCollection } from 'astro:content'; import type { APIContext } from 'astro'; import { getSlug, enrichPostsWithDates } from '../lib/md'; import { getTxtFiles } from '../lib/txt'; - -function excerpt(markdown: string | undefined, maxLen = 160): string { - if (!markdown) return ''; - return markdown - .replace(/^#+\s+.*$/gm, '') - .replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1') - .replace(/[*_~`]/g, '') - .replace(/:[a-z]+\[([^\]]*)\]/g, '$1') - .replace(/\n+/g, ' ') - .trim() - .slice(0, maxLen); -} +import { excerpt } from '../lib/format'; export async function GET(context: APIContext) { const rawPosts = await getCollection('md');