blog/plugin/remark-reading-time.ts
Michael Zhang 8e1fac9bd5
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
admonitions
2023-09-01 10:09:01 -05:00

16 lines
531 B
TypeScript

import getReadingTime from "reading-time";
import { toString } from "mdast-util-to-string";
import type { RemarkPlugin } from "@astrojs/markdown-remark";
const remarkReadingTime: RemarkPlugin = () => {
return function (tree, { data }) {
const textOnPage = toString(tree);
const readingTime = getReadingTime(textOnPage);
// readingTime.text will give us minutes read as a friendly string,
// i.e. "3 min read"
data.astro.frontmatter.minutesRead = readingTime.text;
};
};
export default remarkReadingTime;