master
1import type { RemarkPlugin } from '@astrojs/markdown-remark';
2import { toString } from 'mdast-util-to-string';
3import getReadingTime from 'reading-time';
4
5export const remarkReadingTime: RemarkPlugin = function () {
6 return (tree, { data }) => {
7 const textOnPage = toString(tree);
8 const readingTime = getReadingTime(textOnPage);
9
10 // @ts-expect-error data.astro.frontmatter must be defined
11 data.astro.frontmatter.minutes = Math.max(1, Math.round(readingTime.minutes));
12
13 // @ts-expect-error data.astro.frontmatter must be defined
14 data.astro.frontmatter.words = readingTime.words;
15 };
16};