updates to frontmatter handling
This commit is contained in:
parent
a2866cade6
commit
4ceff93214
@ -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;
|
||||
|
||||
return { frontmatter, markdown };
|
||||
if (end_index === 2) {
|
||||
return {
|
||||
frontmatter: null,
|
||||
document: raw_document,
|
||||
};
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user