blog/src/plugins/remark-excerpt.ts
2025-01-14 01:06:09 +00:00

21 lines
573 B
TypeScript

import type { Root } from 'mdast'
import type { VFile } from 'vfile'
import { toString } from 'mdast-util-to-string'
export function remarkExcerpt() {
return function (tree: Root, file: VFile) {
let excerpt = ''
for (const node of tree.children) {
if (node.type === 'paragraph') {
excerpt = toString(node)
break
}
}
// 确保 data.astro.frontmatter 存在
file.data.astro = file.data.astro || {}
file.data.astro.frontmatter = file.data.astro.frontmatter || {}
file.data.astro.frontmatter.excerpt = excerpt
}
}