2023-07-24 17:52:51 +04:00
|
|
|
// Adds metadata to a Markdown page:
|
|
|
|
// description -- text extracted from the first paragraph
|
|
|
|
// readingTime -- reading time of the whole text, in float (not integer)
|
|
|
|
|
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)
|
|
|
|
const readingTime = getReadingTime(articleText)
|
2023-06-18 20:00:53 +04:00
|
|
|
|
2023-07-24 17:52:51 +04:00
|
|
|
data.astro.frontmatter.readingTime = readingTime.minutes
|
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
|
|
|
}
|