updates to frontmatter handling

This commit is contained in:
James Brumond 2023-05-06 14:07:15 -07:00
parent a2866cade6
commit 4ceff93214
Signed by: james
GPG Key ID: E8F2FC44BAA3357A

View File

@ -1,18 +1,26 @@
import * as yaml from 'yaml';
export function process_frontmatter(document: string, parse = true) {
if (! document.startsWith('---\n')) {
export function process_frontmatter(raw_document: string, parse = true) {
if (! raw_document.startsWith('---\n')) {
return {
frontmatter: null,
markdown: document,
document: raw_document,
};
}
const endIndex = document.slice(3).indexOf('\n---\n') + 3;
const frontmatterYaml = document.slice(3, endIndex);
const markdown = document.slice(endIndex + 4);
const frontmatter = parse ? frontmatterYaml : yaml.parse(frontmatterYaml);
const end_index = raw_document.slice(3).indexOf('\n---\n') + 3;
if (end_index === 2) {
return {
frontmatter: null,
document: raw_document,
};
}
return { frontmatter, markdown };
const formatter_yaml = raw_document.slice(3, end_index);
const document = raw_document.slice(end_index + 4);
const frontmatter = parse ? formatter_yaml : yaml.parse(formatter_yaml);
return { frontmatter, document };
}