Add support to MDX in blog

This commit is contained in:
prototypa
2022-10-15 23:35:11 +02:00
parent e7fd6cc72c
commit 11b9782d6e
12 changed files with 41 additions and 19 deletions

11
src/utils/frontmatter.js Normal file
View File

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

View File

@ -1,13 +1,11 @@
import getReadingTime from 'reading-time';
const getNormalizedPost = async (post) => {
const { frontmatter, compiledContent, rawContent, file } = post;
const { frontmatter, Content, file } = post;
const ID = file.split('/').pop().split('.').shift();
return {
id: ID,
pubDate: frontmatter.pubDate,
publishDate: frontmatter.publishDate,
draft: frontmatter.draft,
canonical: frontmatter.canonical,
@ -15,19 +13,21 @@ const getNormalizedPost = async (post) => {
title: frontmatter.title,
description: frontmatter.description,
body: compiledContent(),
image: frontmatter.image,
Content: Content,
// or 'body' in case you consume from API
excerpt: frontmatter.excerpt,
authors: frontmatter.authors,
category: frontmatter.category,
tags: frontmatter.tags,
readingTime: Math.ceil(getReadingTime(rawContent()).minutes),
readingTime: frontmatter.readingTime,
};
};
const load = async function () {
const posts = import.meta.glob('~/../data/blog/**/*.md', {
const posts = import.meta.glob(['~/../data/blog/**/*.md', '~/../data/blog/**/*.mdx'], {
eager: true,
});
@ -37,7 +37,7 @@ const load = async function () {
});
const results = (await Promise.all(normalizedPosts))
.sort((a, b) => new Date(b.pubDate).valueOf() - new Date(a.pubDate).valueOf())
.sort((a, b) => new Date(b.publishDate).valueOf() - new Date(a.publishDate).valueOf())
.filter((post) => !post.draft);
return results;
};