Custom mdast util
This commit is contained in:
parent
3d07dddef4
commit
f325cebda0
3 changed files with 63 additions and 15 deletions
|
@ -1,16 +1,26 @@
|
|||
import getReadingTime from 'reading-time';
|
||||
import { toString as mdToString } from 'mdast-util-to-string';
|
||||
import { select } from 'unist-util-select'
|
||||
import { visit } from 'unist-util-visit'
|
||||
import getReadingTime from 'reading-time'
|
||||
|
||||
const ELLIPSIS = '\u2026';
|
||||
export default function remarkPostMeta() {
|
||||
return (tree, { data }) => {
|
||||
const firstPara = select(':not(heading) paragraph:first-of-type', tree)
|
||||
|
||||
export default function() {
|
||||
return function (tree, { data }) {
|
||||
const textOnPage = mdToString(tree);
|
||||
const firstParaText = mdToString(firstPara)
|
||||
const articleText = mdToString(tree)
|
||||
|
||||
const readingTime = getReadingTime(textOnPage);
|
||||
data.astro.frontmatter.readingTime = readingTime.text;
|
||||
const readingTime = getReadingTime(articleText)
|
||||
data.astro.frontmatter.readingTime = readingTime.minutes
|
||||
|
||||
const description = textOnPage.slice(0, 100) + ELLIPSIS;
|
||||
data.astro.frontmatter.description = description;
|
||||
};
|
||||
data.astro.frontmatter.description = firstParaText
|
||||
}
|
||||
}
|
||||
|
||||
function mdToString(tree) {
|
||||
let str = ''
|
||||
visit(tree, 'text', node => {
|
||||
str += node.value
|
||||
str += ' '
|
||||
})
|
||||
return str.trimEnd()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue