mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
21 lines
729 B
JavaScript
21 lines
729 B
JavaScript
import { toString } from 'mdast-util-to-string';
|
|
import getReadingTime from 'reading-time';
|
|
|
|
/**
|
|
* Remark plugin that calculates reading time for markdown/MDX content.
|
|
* Injects the reading time into remarkPluginFrontmatter for use in templates.
|
|
* adapted from https://docs.astro.build/en/recipes/reading-time/
|
|
*/
|
|
export function remarkReadingTime() {
|
|
return function (tree, { data }) {
|
|
const textOnPage = toString(tree);
|
|
const readingTime = getReadingTime(textOnPage);
|
|
|
|
// Inject reading time into frontmatter
|
|
data.astro.frontmatter.minutesRead = readingTime.text;
|
|
|
|
// Also provide the numeric minutes value for easier access
|
|
data.astro.frontmatter.readingTimeMinutes = readingTime.minutes;
|
|
};
|
|
}
|