From 260c91e4b1a1fd4daf92b5917b1bbf9a6abe0286 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 14 Jun 2023 10:20:21 +0200 Subject: [PATCH] Fixes #416: remove frontmatter when including or using a template --- plug-api/lib/frontmatter.ts | 3 ++- plugs/directive/template_directive.ts | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/plug-api/lib/frontmatter.ts b/plug-api/lib/frontmatter.ts index 12964b15..58638945 100644 --- a/plug-api/lib/frontmatter.ts +++ b/plug-api/lib/frontmatter.ts @@ -14,6 +14,7 @@ import { export async function extractFrontmatter( tree: ParseTree, removeKeys: string[] = [], + removeFrontmatterSection = false, ): Promise { let data: any = {}; addParentPointers(tree); @@ -55,7 +56,7 @@ export async function extractFrontmatter( } } // If nothing is left, let's just delete this whole block - if (Object.keys(newData).length === 0) { + if (Object.keys(newData).length === 0 || removeFrontmatterSection) { return null; } } catch (e: any) { diff --git a/plugs/directive/template_directive.ts b/plugs/directive/template_directive.ts index ee57b266..3c20d8f9 100644 --- a/plugs/directive/template_directive.ts +++ b/plugs/directive/template_directive.ts @@ -45,15 +45,15 @@ export async function templateDirectiveRenderer( } else { templateText = await space.readPage(template); } - let newBody = templateText; + const tree = await markdown.parseMarkdown(templateText); + await extractFrontmatter(tree, [], true); // Remove entire frontmatter section, if any + let newBody = renderToText(tree); + // if it's a template injection (not a literal "include") if (directive === "use") { - const tree = await markdown.parseMarkdown(templateText); - await extractFrontmatter(tree, ["$disableDirectives"]); - templateText = renderToText(tree); registerHandlebarsHelpers(); const templateFn = Handlebars.compile( - replaceTemplateVars(templateText, pageName), + replaceTemplateVars(newBody, pageName), { noEscape: true }, ); if (typeof parsedArgs !== "string") {