mirror of
https://github.com/reonokiy/blog.nokiy.net.git
synced 2025-06-16 19:51:07 +02:00
feat: markdown extended features from fuwari
This commit is contained in:
parent
9e9cdcb206
commit
5d77abf77c
3 changed files with 57 additions and 34 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue