0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

feat: expose frontmatter on (undocumented) routes key

This commit is contained in:
bholmesdev 2022-04-29 16:17:57 -04:00
parent c8f5fa35c4
commit ece5feef02
3 changed files with 18 additions and 9 deletions

View file

@ -719,6 +719,8 @@ export interface ComponentInstance {
default: AstroComponentFactory;
css?: string[];
getStaticPaths?: (options: GetStaticPathsOptions) => GetStaticPathsResult;
// supplied by .md files
frontmatter?: Record<string, any>;
}
export interface MarkdownInstance<T extends Record<string, any>> {

View file

@ -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']) {

View file

@ -144,7 +144,7 @@ export async function runHookBuildDone({
config: AstroConfig;
buildConfig: BuildConfig;
pages: string[];
routes: RouteData[];
routes: RouteData[] & { frontmatter?: Record<string, any> };
}) {
const dir = isBuildingToSSR(config) ? buildConfig.client : config.outDir;