2023-06-22 21:59:47 +04:00
|
|
|
import { select } from 'unist-util-select'
|
|
|
|
import { visit } from 'unist-util-visit'
|
|
|
|
import getReadingTime from 'reading-time'
|
2023-06-18 20:00:53 +04:00
|
|
|
|
2023-06-22 21:59:47 +04:00
|
|
|
export default function remarkPostMeta() {
|
|
|
|
return (tree, { data }) => {
|
|
|
|
const firstPara = select(':not(heading) paragraph:first-of-type', tree)
|
2023-06-18 20:00:53 +04:00
|
|
|
|
2023-06-22 21:59:47 +04:00
|
|
|
const firstParaText = mdToString(firstPara)
|
|
|
|
const articleText = mdToString(tree)
|
2023-06-18 20:00:53 +04:00
|
|
|
|
2023-06-22 21:59:47 +04:00
|
|
|
const readingTime = getReadingTime(articleText)
|
|
|
|
data.astro.frontmatter.readingTime = readingTime.minutes
|
2023-06-18 20:00:53 +04:00
|
|
|
|
2023-06-22 21:59:47 +04:00
|
|
|
data.astro.frontmatter.description = firstParaText
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mdToString(tree) {
|
|
|
|
let str = ''
|
|
|
|
visit(tree, 'text', node => {
|
|
|
|
str += node.value
|
|
|
|
str += ' '
|
|
|
|
})
|
|
|
|
return str.trimEnd()
|
2023-06-18 20:00:53 +04:00
|
|
|
}
|