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

@ -9,6 +9,7 @@ import image from '@astrojs/image';
import mdx from '@astrojs/mdx'; import mdx from '@astrojs/mdx';
import partytown from '@astrojs/partytown'; import partytown from '@astrojs/partytown';
import compress from 'astro-compress'; import compress from 'astro-compress';
import { readingTimeRemarkPlugin } from './src/utils/frontmatter.mjs';
import { SITE } from './src/config.mjs'; import { SITE } from './src/config.mjs';
@ -24,6 +25,10 @@ export default defineConfig({
output: 'static', output: 'static',
markdown: {
remarkPlugins: [readingTimeRemarkPlugin],
},
integrations: [ integrations: [
tailwind({ tailwind({
config: { config: {
@ -55,8 +60,6 @@ export default defineConfig({
}), }),
], ],
markdown: {},
vite: { vite: {
resolve: { resolve: {
alias: { alias: {

View File

@ -43,13 +43,13 @@ const link = !BLOG?.post?.disabled ? getPermalink(post.permalink, 'post') : '';
<header> <header>
<div class="mb-1"> <div class="mb-1">
<span class="text-sm"> <span class="text-sm">
<Icon name="tabler:clock" class="w-3.5 h-3.5 inline-block -mt-0.5 dark:text-gray-400" /> <Icon name="tabler:clock" class="w-3.5 h-3.5 inline-block -mt-0.5 dark:text-gray-400" />
<time datetime={String(post.publishDate)}>{getFormattedDate(post.publishDate)}</time> <time datetime={String(post.publishDate)}>{getFormattedDate(post.publishDate)}</time>
{ {
post.category && ( post.category && (
<> <>
{' '} · {' '}
·{' '}
<a class="capitalize hover:underline" href={getPermalink(post.category, 'category')}> <a class="capitalize hover:underline" href={getPermalink(post.category, 'category')}>
{post.category.replaceAll('-', ' ')} {post.category.replaceAll('-', ' ')}
</a> </a>

View File

@ -29,13 +29,14 @@ const { post, url } = Astro.props;
post.category && ( post.category && (
<> <>
{' '} {' '}
· ·{' '}
<a class="capitalize hover:underline" href={getPermalink(post.category, 'category')}> <a class="capitalize hover:underline" href={getPermalink(post.category, 'category')}>
{post.category.replaceAll('-', ' ')} {post.category.replaceAll('-', ' ')}
</a> </a>
</> </>
) )
} }
{' '}·{' '}{post.readingTime} min read
</p> </p>
</div> </div>
<h1 <h1

View File

@ -20,6 +20,8 @@ export interface Post {
Content: unknown; Content: unknown;
content?: string; content?: string;
readingTime?: number;
} }
export interface MetaSEO { export interface MetaSEO {

View File

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

View File

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