parse formatters - frontmatter and json
This commit is contained in:
parent
fdb2b9b4ed
commit
4811bcdd23
|
@ -31,20 +31,38 @@ export const removeTrailingSlash = (site: string) => {
|
||||||
return site.replace(/\/$/, '');
|
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 ========
|
// ======= MARKDOWN PARSER ========
|
||||||
|
|
||||||
|
|
||||||
// https://github.com/jonschlinkert/gray-matter
|
// https://github.com/jonschlinkert/gray-matter
|
||||||
import matter from 'gray-matter';
|
import * as matter from 'gray-matter';
|
||||||
// https://github.com/markedjs/marked
|
// https://github.com/markedjs/marked
|
||||||
import marked from 'marked';
|
// import marked from 'marked';
|
||||||
|
|
||||||
export function parseMarkdown<T>(content: string): { frontmatter: T; content: string } {
|
export async function parseMarkdownFile(filePath: string) {
|
||||||
const { data, content: rawContent } = matter(content);
|
const markdownData = await fs.readFileSync(path.join(process.cwd(), 'src', 'content', filePath), 'utf-8');
|
||||||
const markdownContent = marked(rawContent);
|
const { data, content } = matter(markdownData);
|
||||||
return {
|
return { frontmatter: data, content };
|
||||||
frontmatter: data as T,
|
|
||||||
content: markdownContent,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
// 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 };
|
||||||
|
// }
|
||||||
|
|
Loading…
Reference in New Issue