1
0

Don't blow up when frontmatter YAML is invalid

This commit is contained in:
Zef Hemel 2022-11-23 10:04:51 +01:00
parent 5d89e15959
commit 56e5466b19

View File

@ -81,25 +81,29 @@ export function extractMeta(
// Find FrontMatter and parse it // Find FrontMatter and parse it
if (t.type === "FrontMatter") { if (t.type === "FrontMatter") {
const yamlText = renderToText(t.children![1].children![0]); const yamlText = renderToText(t.children![1].children![0]);
const parsedData: any = YAML.parse(yamlText); try {
const newData = { ...parsedData }; const parsedData: any = YAML.parse(yamlText);
data = { ...data, ...parsedData }; const newData = { ...parsedData };
if (removeKeys.length > 0) { data = { ...data, ...parsedData };
let removedOne = false; if (removeKeys.length > 0) {
let removedOne = false;
for (const key of removeKeys) { for (const key of removeKeys) {
if (key in newData) { if (key in newData) {
delete newData[key]; delete newData[key];
removedOne = true; removedOne = true;
}
}
if (removedOne) {
t.children![0].text = YAML.stringify(newData);
} }
} }
if (removedOne) { // If nothing is left, let's just delete this whole block
t.children![0].text = YAML.stringify(newData); if (Object.keys(newData).length === 0) {
return null;
} }
} } catch (e: any) {
// If nothing is left, let's just delete this whole block console.error("Could not parse frontmatter", e);
if (Object.keys(newData).length === 0) {
return null;
} }
} }