feat: markdown extended features from fuwari

This commit is contained in:
radishzzz 2025-01-14 01:06:09 +00:00
parent 9e9cdcb206
commit 5d77abf77c
3 changed files with 57 additions and 34 deletions

View file

@ -1,26 +1,21 @@
import type { Root } from 'mdast'
import type { VFile } from 'vfile'
import { toString } from 'mdast-util-to-string'
interface AstroData {
data: {
astro: {
frontmatter: {
excerpt: string
}
}
}
}
export function remarkExcerpt() {
return (tree: Root, file: AstroData) => {
return function (tree: Root, file: VFile) {
let excerpt = ''
for (const node of tree.children) {
if (node.type !== 'paragraph') {
continue
if (node.type === 'paragraph') {
excerpt = toString(node)
break
}
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
}
}

View file

@ -1 +1,18 @@
import type { Root } from 'mdast'
import type { VFile } from 'vfile'
import { toString } from 'mdast-util-to-string'
import getReadingTime from 'reading-time'
export function remarkReadingTime() {
return function (tree: Root, file: VFile) {
const textOnPage = toString(tree)
const readingTime = getReadingTime(textOnPage)
// 确保 data.astro.frontmatter 存在
file.data.astro = file.data.astro || {}
file.data.astro.frontmatter = file.data.astro.frontmatter || {}
file.data.astro.frontmatter.minutes = Math.max(1, Math.round(readingTime.minutes))
file.data.astro.frontmatter.words = readingTime.words
}
}