feat: extracts the rss excerpt utility to use it for og:description on post pages
This commit is contained in:
parent
331d843f68
commit
0029274484
3 changed files with 16 additions and 14 deletions
|
|
@ -7,6 +7,18 @@ export function escapeHtml(str: string): string {
|
||||||
.replace(/'/g, ''');
|
.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 {
|
export function formatDate(date: Date): string {
|
||||||
const d = String(date.getDate()).padStart(2, '0');
|
const d = String(date.getDate()).padStart(2, '0');
|
||||||
const m = String(date.getMonth() + 1).padStart(2, '0');
|
const m = String(date.getMonth() + 1).padStart(2, '0');
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
import { getCollection, render } from 'astro:content';
|
import { getCollection, render } from 'astro:content';
|
||||||
import Layout from '../layouts/Layout.astro';
|
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';
|
import { getSlug, enrichPostWithDates, enrichPostsWithDates, resolveRelatedPosts } from '../lib/md';
|
||||||
|
|
||||||
export async function getStaticPaths() {
|
export async function getStaticPaths() {
|
||||||
|
|
@ -16,8 +16,9 @@ export async function getStaticPaths() {
|
||||||
const { post, allPosts } = Astro.props;
|
const { post, allPosts } = Astro.props;
|
||||||
const { Content } = await render(post);
|
const { Content } = await render(post);
|
||||||
const related = post.data.related ? resolveRelatedPosts(post.data.related, allPosts) : [];
|
const related = post.data.related ? resolveRelatedPosts(post.data.related, allPosts) : [];
|
||||||
|
const description = excerpt((post as any).body) || undefined;
|
||||||
---
|
---
|
||||||
<Layout title={`${post.data.title} - lewis m.w.`}>
|
<Layout title={`${post.data.title} - lewis m.w.`} description={description}>
|
||||||
|
|
||||||
<article>
|
<article>
|
||||||
<h1>{post.data.title}</h1>
|
<h1>{post.data.title}</h1>
|
||||||
|
|
|
||||||
|
|
@ -3,18 +3,7 @@ import { getCollection } from 'astro:content';
|
||||||
import type { APIContext } from 'astro';
|
import type { APIContext } from 'astro';
|
||||||
import { getSlug, enrichPostsWithDates } from '../lib/md';
|
import { getSlug, enrichPostsWithDates } from '../lib/md';
|
||||||
import { getTxtFiles } from '../lib/txt';
|
import { getTxtFiles } from '../lib/txt';
|
||||||
|
import { excerpt } from '../lib/format';
|
||||||
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 async function GET(context: APIContext) {
|
export async function GET(context: APIContext) {
|
||||||
const rawPosts = await getCollection('md');
|
const rawPosts = await getCollection('md');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue