From 56e5466b19f3f7de98b3818c802812372edc8315 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 23 Nov 2022 10:04:51 +0100 Subject: [PATCH] Don't blow up when frontmatter YAML is invalid --- plugs/directive/data.ts | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/plugs/directive/data.ts b/plugs/directive/data.ts index 938c536..457acbb 100644 --- a/plugs/directive/data.ts +++ b/plugs/directive/data.ts @@ -81,25 +81,29 @@ export function extractMeta( // Find FrontMatter and parse it if (t.type === "FrontMatter") { const yamlText = renderToText(t.children![1].children![0]); - const parsedData: any = YAML.parse(yamlText); - const newData = { ...parsedData }; - data = { ...data, ...parsedData }; - if (removeKeys.length > 0) { - let removedOne = false; + try { + const parsedData: any = YAML.parse(yamlText); + const newData = { ...parsedData }; + data = { ...data, ...parsedData }; + if (removeKeys.length > 0) { + let removedOne = false; - for (const key of removeKeys) { - if (key in newData) { - delete newData[key]; - removedOne = true; + for (const key of removeKeys) { + if (key in newData) { + delete newData[key]; + removedOne = true; + } + } + if (removedOne) { + t.children![0].text = YAML.stringify(newData); } } - if (removedOne) { - t.children![0].text = YAML.stringify(newData); + // If nothing is left, let's just delete this whole block + if (Object.keys(newData).length === 0) { + return null; } - } - // If nothing is left, let's just delete this whole block - if (Object.keys(newData).length === 0) { - return null; + } catch (e: any) { + console.error("Could not parse frontmatter", e); } }