parse formatters - frontmatter and json

This commit is contained in:
matthieu42morin 2024-03-30 05:05:33 +01:00
parent fdb2b9b4ed
commit 4811bcdd23
1 changed files with 27 additions and 9 deletions

View File

@ -31,20 +31,38 @@ export const removeTrailingSlash = (site: string) => {
return site.replace(/\/$/, '');
};
// ======= JSON PARSER ========
import fs from 'fs';
import path from 'path';
export async function readJsonFile(filePath: string) {
const jsonData = await fs.readFileSync(path.join(process.cwd(), 'src', 'content', filePath), 'utf-8');
return JSON.parse(jsonData);
}
// ======= MARKDOWN PARSER ========
// https://github.com/jonschlinkert/gray-matter
import matter from 'gray-matter';
import * as matter from 'gray-matter';
// https://github.com/markedjs/marked
import marked from 'marked';
// import marked from 'marked';
export function parseMarkdown<T>(content: string): { frontmatter: T; content: string } {
const { data, content: rawContent } = matter(content);
const markdownContent = marked(rawContent);
return {
frontmatter: data as T,
content: markdownContent,
};
export async function parseMarkdownFile(filePath: string) {
const markdownData = await fs.readFileSync(path.join(process.cwd(), 'src', 'content', filePath), 'utf-8');
const { data, content } = matter(markdownData);
return { frontmatter: data, content };
}
// export function parseMarkdown<T>(filePath: string): { frontmatter: T; content: string } {
// const data = matter.read(filePath).data;
// return {
// frontmatter: data as T,
// };
// }
// export function parseMarkdownFile(filePath: string) {
// const markdownData = fs.readFileSync(path.join(process.cwd(), filePath), 'utf-8');
// const { data, content } = grayMatter(markdownData);
// return { frontmatter: data, content };
// }