From 4811bcdd23efd1709fded6df66d2039902720282 Mon Sep 17 00:00:00 2001 From: matthieu42morin Date: Sat, 30 Mar 2024 05:05:33 +0100 Subject: [PATCH] parse formatters - frontmatter and json --- src/lib/utils/formatParse.ts | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/lib/utils/formatParse.ts b/src/lib/utils/formatParse.ts index a0d5302..2d99924 100644 --- a/src/lib/utils/formatParse.ts +++ b/src/lib/utils/formatParse.ts @@ -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(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(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 }; +// }