Skip to content

Commit

Permalink
Feat: Add frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
sandypockets committed Dec 16, 2023
1 parent 55be1fd commit 728c1bf
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dist/index.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions example/pages/blog/[slug].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ async function convertPostToHtml(fileContents) {
renderEmbeds: true,
};

const { contentHtml, toc, readingTime } = await processMarkdown(fileContents, options);
return { contentHtml, toc, readingTime };
const { contentHtml, toc, readingTime, frontMatter } = await processMarkdown(fileContents, options);
return { contentHtml, toc, readingTime, frontMatter };
}

export async function getStaticPaths() {
Expand All @@ -24,16 +24,16 @@ export async function getStaticProps({ params }) {
const allPosts = getAllPosts();
const { fileContents } = allPosts.find(post => post.id === params.slug);
const postData = await convertPostToHtml(fileContents);
return { props: { postData, slug: params.slug } };
return { props: { postData } };
}

export default function Post({ postData, slug }) {
export default function Post({ postData }) {
return (
<article>
<h1 className="text-5xl">{slug}</h1>
<p>Reading time: {postData.readingTime} min</p>
<article className="max-w-3xl mx-auto py-12">
<h1 className="text-5xl font-semibold">{postData.frontMatter.title}</h1>
<p className="my-2">Reading time: {postData.readingTime} min</p>
<div>
<h3 className="text-2xl">Table of Contents</h3>
<h3 className="text-xl">Table of Contents</h3>
<div dangerouslySetInnerHTML={{ __html: postData.toc }} />
</div>
<div dangerouslySetInnerHTML={{ __html: postData.contentHtml }} />
Expand Down
10 changes: 8 additions & 2 deletions example/posts/example.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Welcome to the Example Post
---
title: "Welcome to the Example Post"
date: "2023-12-15"
author: "Your Name"
summary: "This post demonstrates various Markdown features supported by our processor."
tags: ["Markdown", "Formatting", "Guide"]
---

This post demonstrates various Markdown features supported by our processor.
Welcome! This post demonstrates various Markdown features supported by our processor.

## Styling Text

Expand Down
90 changes: 89 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
"rehype-raw": "^7.0.0",
"rehype-stringify": "^10.0.0",
"remark": "^15.0.1",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.0",
"remark-html": "^16.0.1",
"remark-rehype": "^11.0.0"
"remark-rehype": "^11.0.0",
"yaml": "^2.3.4"
},
"devDependencies": {
"@babel/core": "^7.23.6",
Expand Down
22 changes: 19 additions & 3 deletions src/processMarkdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { remark } from 'remark';
import html from 'remark-html';
import gfm from 'remark-gfm';
import frontmatter from 'remark-frontmatter';
import yaml from 'yaml';
import remarkRehype from 'remark-rehype';
import rehypeStringify from 'rehype-stringify';
import rehypeRaw from 'rehype-raw';
Expand All @@ -9,7 +11,7 @@ import addHeadingIds from './plugins/addHeadingIds.js';
import addTableOfContents from './plugins/addTableOfContents.js';
import calculateReadingTime from './plugins/calculateReadingTime.js';
import embed from './plugins/embed.js';
import wrapWithDiv from './helpers/wrapWithDiv.js'
import wrapWithDiv from './helpers/wrapWithDiv.js';

export default async function processMarkdown(markdownContent, options = {}) {
const defaultWrapConfig = {
Expand All @@ -18,11 +20,19 @@ export default async function processMarkdown(markdownContent, options = {}) {
};

const wrapConfig = { ...defaultWrapConfig, ...(options.wrapConfig || {}) };

let frontMatterData = null;
let tableOfContents = null;
let readingTime = null;

const processor = remark().use(html).use(gfm).use(remarkRehype, { allowDangerousHtml: true }).use(wrapElements, wrapConfig);
const processor = remark()
.use(frontmatter, ['yaml'])
.use(() => tree => {
frontMatterData = extractFrontMatter(tree);
})
.use(html)
.use(gfm)
.use(remarkRehype, { allowDangerousHtml: true })
.use(wrapElements, wrapConfig);

if (options.addHeadingIds) {
processor.use(addHeadingIds);
Expand Down Expand Up @@ -53,8 +63,14 @@ export default async function processMarkdown(markdownContent, options = {}) {
const wrappedContentHtml = wrapWithDiv(processedContent.toString(), 'markdown');

return {
frontMatter: frontMatterData,
contentHtml: wrappedContentHtml,
toc: tableOfContents,
readingTime: readingTime,
};
}

function extractFrontMatter(tree) {
const frontMatterNode = tree.children.find(node => node.type === 'yaml');
return frontMatterNode ? yaml.parse(frontMatterNode.value) : {};
}

0 comments on commit 728c1bf

Please sign in to comment.