Enable readingTime remark plugin

This commit is contained in:
prototypa
2023-01-31 14:54:36 -05:00
parent d4560c1226
commit 8cd0bbbdb4
6 changed files with 18 additions and 11 deletions

View File

@ -30,7 +30,7 @@ const generatePermalink = async ({ id, slug, publishDate, category }) => {
const getNormalizedPost = async (post: CollectionEntry<'post'>): Promise<Post> => {
const { id, slug: rawSlug = '', data } = post;
const { Content } = await post.render();
const { Content, remarkPluginFrontmatter } = await post.render();
const {
tags: rawTags = [],
@ -60,6 +60,8 @@ const getNormalizedPost = async (post: CollectionEntry<'post'>): Promise<Post> =
// or 'body' in case you consume from API
permalink: await generatePermalink({ id, slug, publishDate, category }),
readingTime: remarkPluginFrontmatter?.readingTime,
};
};

View File

@ -1,12 +1,11 @@
import getReadingTime from 'reading-time';
import { toString } from 'mdast-util-to-string';
export function remarkReadingTime() {
export function readingTimeRemarkPlugin() {
return function (tree, file) {
const text = toString(tree);
const readingTime = Math.ceil(getReadingTime(text).minutes);
const textOnPage = toString(tree);
const readingTime = Math.ceil(getReadingTime(textOnPage).minutes);
const { frontmatter } = file.data.astro;
frontmatter.readingTime = readingTime;
file.data.astro.frontmatter.readingTime = readingTime
};
}