Replies: 3 comments 12 replies
-
Thanks for getting the ball rolling @Pukimaa. The one thing I wasn't sure of when putting that syntax together was how important order is. Glob patterns are likely to overlap, e.g. to have a catch-all glob and then more specific patterns, so order would be the obvious way to decide which layout wins when multiple globs match. We might need to use an array of tuples like: markdown: {
defaultLayout: [
['pages/blog/**.md', 'layouts/Blog.astro'],
['pages/**.md', 'layouts/BaseLayout.astro'],
],
} |
Beta Was this translation helpful? Give feedback.
-
Here is my approach (in the PR) While looking for where to implement, I also saw the other 2 special frontmatter fields: I approached it by having a folder-level config, that way, by creating the special {
"layout": "relative path to a Layout.astro",
"components": "not sure how this one works yet.. seems similar but not quite to setup",
"setup": "import Component from 'path-to-file'"
} |
Beta Was this translation helpful? Give feedback.
-
Going to share my draft RFC from a couple of weeks ago for a Something in particular to think about is resolving imports for
SummaryThe ability to set defaults for Markdown frontmatter to avoid repetition across many files is often requested in Discord discussions. This RFC proposes a way to support this. ExampleIf this RFC is accepted, defaults could be provided via a // astro.config.mjs
export default defineConfig({
markdown: {
frontmatterDefaults: {
'./src/pages/blog/*.md': {
layout: '~/layouts/BlogPost.astro',
color: 'blue'
},
'./src/pages/products/*.md': {
layout: '~/layouts/Product.astro',
color: 'yellow'
}
}
}
}) The defaults could be used in expressions within Markdown like existing frontmatter: {frontmatter.color} And would be included in the payload from const [blogPost] = Astro.fetchContent('../pages/blog/*.md')
// blogPost.color === 'blue' MotivationThe initial motivation relates to default layouts (which is a common feature request). Astro treats Markdown files differently from other kinds of pages, allowing us to wrap them in a layout component and helping to pass data from Markdown to the layout for us. However, currently each Markdown file has to specify which layout to use, which for large amounts of content can become unwieldy and difficult to maintain. Supporting defaults makes it easier and quicker to make large changes. @natemoo-re has suggested a Potential use cases include:
Importantly, this RFC proposes ways to apply defaults with granularity, such that different collections of content can have different defaults. Detailed designConfig design
interface AdditionToCurrentConfig {
markdown?: {
frontmatterDefaults?: Record<string, FrontmatterDefault>
}
}
/** Shape of a single entry in the `frontmatterDefaults` map. */
interface FrontmatterDefault {
layout?: string
setup?: string
[key: string]: any
} Usage example// astro.config.mjs
export default defineConfig({
markdown: {
frontmatterDefaults: {
// Applies to all Markdown files
'./**/*.md': {
layout: '~/layouts/DefaultLayout.astro',
setup: 'import Components from "~/components/index.tsx"'
},
// Applies to Markdown files in the blog directory
'./src/pages/blog/**/*.md': {
layout: '~/layouts/BlogPost.astro',
collectionType: 'blog',
author: {
name: 'Laika',
url: 'https://space.dog'
}
},
// Applies only to `index.md` files
'./**/index.md': {
layout: '~/layouts/Index.astro'
}
}
}
}) Implementation
Notes
ExampleBased on the usage example above:
Additional usage possibilitiesThere may be cases where defaults should be easily edited via no-code tooling (CMS, CLI, etc.). I think this would be supported with this design. For example, storing your defaults in an easy to parse and write JSON file would allow them to be imported in the Astro config:
|
Beta Was this translation helpful? Give feedback.
-
Have a config system for Layouts
This is a simple example on how that could look like
// Simple
// Advanced
Using _config.json (already draft pr)
General questions
This proposal would see the default dropped in this scenario, but someone might expect both tags to survive.
Beta Was this translation helpful? Give feedback.
All reactions