diff --git a/content-collections.ts b/content-collections.ts index 9d82ae275..577e5172a 100644 --- a/content-collections.ts +++ b/content-collections.ts @@ -16,6 +16,7 @@ const posts = defineCollection({ ...post, slug: post._meta.path, excerpt: frontMatter.excerpt, + description: frontMatter.data.description, content, } }, diff --git a/src/routes/_libraries/blog.$.tsx b/src/routes/_libraries/blog.$.tsx index f05b0a7d1..bfa2b74be 100644 --- a/src/routes/_libraries/blog.$.tsx +++ b/src/routes/_libraries/blog.$.tsx @@ -34,7 +34,7 @@ const fetchBlogPost = createServerFn({ method: 'GET' }) return { title: post.title, - description: post.excerpt, + description: post.description, published: post.published, content: post.content, authors: post.authors, diff --git a/src/utils/documents.server.ts b/src/utils/documents.server.ts index b66374288..151c393b3 100644 --- a/src/utils/documents.server.ts +++ b/src/utils/documents.server.ts @@ -312,9 +312,17 @@ export async function fetchRepoFile( } export function extractFrontMatter(content: string) { - return graymatter.default(content, { - excerpt: (file: any) => (file.excerpt = createExcerpt(file.content)), + const result = graymatter.default(content, { + excerpt: (file: any) => (file.excerpt = createRichExcerpt(file.content)), }) + + return { + ...result, + data: { + ...result.data, + description: createExcerpt(result.content) + } as { [key: string]: any } & { description: string } + } } function createExcerpt(text: string, maxLength = 200) { @@ -335,6 +343,12 @@ function createExcerpt(text: string, maxLength = 200) { cleanText = cleanText.slice(0, maxLength).trim() + '...' } + return cleanText +} + +function createRichExcerpt(text: string, maxLength = 200) { + let cleanText = createExcerpt(text, maxLength) + const imageText = extractFirstImage(text) if (imageText) {