From ece5feef0217522eeceef03ad93d27efceeeed70 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 29 Apr 2022 16:17:57 -0400 Subject: [PATCH] feat: expose frontmatter on (undocumented) routes key --- packages/astro/src/@types/astro.ts | 2 ++ packages/astro/src/core/build/index.ts | 23 +++++++++++++++-------- packages/astro/src/integrations/index.ts | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index f2c5577de0..dd250a35f7 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -719,6 +719,8 @@ export interface ComponentInstance { default: AstroComponentFactory; css?: string[]; getStaticPaths?: (options: GetStaticPathsOptions) => GetStaticPathsResult; + // supplied by .md files + frontmatter?: Record; } export interface MarkdownInstance> { diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index d0c0a026c2..8663496103 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -123,13 +123,10 @@ class AstroBuilder { // Filter pages by using conditions based on their frontmatter. Object.entries(allPages).forEach(([page, data]) => { - if ('frontmatter' in data.preload[1]) { - // TODO: add better type inference to data.preload[1] - const frontmatter = (data.preload[1] as any).frontmatter; - if (Boolean(frontmatter.draft) && !this.config.markdown.drafts) { - debug('build', timerMessage(`Skipping draft page ${page}`, this.timer.loadStart)); - delete allPages[page]; - } + const [, componentData] = data.preload; + if (Boolean(componentData.frontmatter?.draft) && !this.config.markdown.drafts) { + debug('build', timerMessage(`Skipping draft page ${page}`, this.timer.loadStart)); + delete allPages[page]; } }); @@ -176,7 +173,17 @@ class AstroBuilder { config: this.config, buildConfig, pages: pageNames, - routes: Object.values(allPages).map((pd) => pd.route), + routes: Object.values(allPages).map((pageData) => { + const { + route, + preload: [, componentData], + } = pageData; + if (componentData.frontmatter) { + return { ...route, frontmatter: componentData.frontmatter }; + } else { + return route; + } + }), }); if (this.logging.level && levels[this.logging.level] <= levels['info']) { diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 452081e485..c39171ad16 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -144,7 +144,7 @@ export async function runHookBuildDone({ config: AstroConfig; buildConfig: BuildConfig; pages: string[]; - routes: RouteData[]; + routes: RouteData[] & { frontmatter?: Record }; }) { const dir = isBuildingToSSR(config) ? buildConfig.client : config.outDir;