diff --git a/src/frontmatter.ts b/src/frontmatter.ts index 7ef5d48..01b4a0d 100644 --- a/src/frontmatter.ts +++ b/src/frontmatter.ts @@ -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 }; }