feat: use the first paragraph as the excerpt if description is not set

This commit is contained in:
saicaca
2024-08-03 16:03:03 +08:00
parent 9af6cf956a
commit 1f93499ece
3 changed files with 20 additions and 3 deletions

View File

@@ -48,8 +48,8 @@ const { remarkPluginFrontmatter } = await entry.render();
<PostMetadata published={published} tags={tags} category={category} hideTagsForMobile={true} class="mb-4"></PostMetadata>
<!-- description -->
<div class="transition text-75 mb-3.5 pr-4">
{ description }
<div class:list={["transition text-75 mb-3.5 pr-4", {"line-clamp-2 md:line-clamp-1": !description}]}>
{ description || remarkPluginFrontmatter.excerpt }
</div>
<!-- word count and read time -->

View File

@@ -0,0 +1,16 @@
import { toString } from 'mdast-util-to-string'
/* Use the post's first paragraph as the excerpt */
export function remarkExcerpt() {
return (tree, { data }) => {
let excerpt = '';
for (let node of tree.children) {
if (node.type !== 'paragraph') {
continue
}
excerpt = toString(node)
break
}
data.astro.frontmatter.excerpt = excerpt
};
}