-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
💡 RFC: Support a default layout #397
Comments
as mentioned on discord can use sed trickery to do this |
I think this makes sense. Not sure the best way to solve though. If we could get one or two other similar use-cases together, I could see a use-case where a |
I was just asking about this on the Discord channel — Eleventy addresses this by letting you define folder-level data (as |
I think we'll wait until we've officially settled on the Markdown file format. There's talk of moving Markdown frontmatter syntax to JS (instead of YAML) to match This convo is currently being kicked off so we should be able to unblock this soon. |
Thanks for the update! |
Action item from RFC Call: This sounds good, let's work on a potential design so we can discuss this in more detail. |
I'd like to use this together with typedoc & typedoc-plugin-markdown, would this be possible with this proposal? |
I hope you're just planning to add JS frontmatter as an option, and not talking about changing the default from YAML to JS. That would be a serious problem (switching default frontmatter sytax to JS). By convention, people have come to expect YAML as the frontmatter syntax (by default) in MD files. There's plenty of MD content that already uses YAML frontmatter. If you change it, it would be harder to migrate existing content into Astro. It might hamper Astro's adoption. Already I'm waiting for this issue to get fixed (default layout) before I can migrate my site from 11ty to Astro. In general, the "data" features of 11ty are very useful, and I'd love to see them in Astro. But all those things can be implemented within the layout.astro file once I can supply a default layout. |
In #1267, I proposed the following options to achieve a default template: The example file structure to discuss the logic:
I proposed to allow a file name & placement based default template logic. So let's say, the following layouts would exist:
Then, the following heuristic could be used to process markdown files. **In
The heuristic could have an additional layer of flexibility, as demonstrated in
Alternatives consideredAllow to place a Risks, downsides, and/or tradeoffsHigher amount of framework logic a user has to consider when naming / setting up layouts. Less explicit mapping of pages and layouts. But higher flexibility and reduced verbosity for simple use-cases. |
I've built a sort of a solution to this w/ Routing! |
@jasikpark we should hold up until Astro components in markdown are supported. |
Hey everyone! Our current RFC process is beginning to break down at this size, with over 50 open RFCs currently in the "discussing" stage. A growing community is a great problem to have, but our ability to give you RFC feedback has suffered as a result. In an effort to improve our RFC process, we are making some changes to better organize things. From now on, all RFCs will live in a standalone repo: https://github.com/withastro/rfcs This allows us to do three things: 1) Use threaded discussions for high-level ideas and improvements, without necessarily requiring an implementation for every idea. 2) Improve the quality of our RFC template and the speed/quality of all feedback. 3) Support inline comments and explicit approvals on RFCs, via a new Pull Request review process. We hope that this new process leads to better RFC weekly calls and faster feedback on your RFCs from maintainers. More detail can be found in the new RFC repo README. We can't automatically convert this issue to an RFC in the new repo because new RFC template is more detailed that this one. But, you can still continue this discussion in the new repo by creating a new Discussion in the RFC repo and copy-and-pasting this post (and any relevant follow-up comments) into it. Discussions are available for high-level ideas and suggestions without the requirement of a full implementation proposal. Then, when you are ready to propose (or re-propose) an implementation for feedback and approval, you can create a new RFC using the new RFC template. More detail about how to do this can be found in the new RFC repo README. Thanks for your patience as we attempt to improve things for both authors and reviewers. If you have any questions, don't hesitate to reach out on Discord. https://astro.build/chat |
This strategy kind of solves it for me using aliases. Granted it's not a "default" layout, but it saves having to set a relative path for all my markdown files. I can just make all of the layout strings the same. https://docs.astro.build/en/guides/aliases/ in "paths": {
"@components/*": ["src/components/*"],
"@layouts/*": ["src/layouts/*"],
"@lib/*": ["src/lib/*"]
} Then in my
|
#3411 should provide this functionality once we have a chance to update and merge it! |
My workaround uses a combination of @phocks's alias idea, and a remark plugin: 1. Create an alias to your layouts "baseUrl": ".",
"paths": {
"@layouts/*": ["./src/layouts/*"],
} (Ensure that 2. Inject function defaultLayoutPlugin () {
return function (tree, file) {
file.data.astro.frontmatter.layout = '@layouts/Layout.astro'
}
}
export default defineConfig({
markdown: {
remarkPlugins: [defaultLayoutPlugin],
extendDefaultPlugins: true
},
}); I've created a Rails-like convention where it looks for a layout with the same name as the file's directory e.g. for import { existsSync } from 'fs'
function defaultLayoutPlugin () {
const layouts = new Set
return function (tree, file) {
const fileName = file.history[0]
const directory = fileName.split('/').slice(-2)[0]
const layout = (
directory.charAt(0).toUpperCase() + directory.slice(1) + '.astro'
)
if (layouts.has(layout) || existsSync(`./src/layouts/${layout}`)) {
layouts.add(layout)
file.data.astro.frontmatter.layout = `@layouts/${layout}`
}
}
} Frontmatter overrides in the file also appear to work. I hope that helps! |
Slightly more complex version of @domchristie's "Rails-like"
import { existsSync } from 'fs';
import { join } from 'path';
const pagesPath = 'src/pages';
const layoutPath = 'src/layouts';
const layoutAlias = '@layouts';
const defaultLayout = 'MainLayout';
const pascalCache = {};
function toPascalCase(str) {
pascalCache[str] =
pascalCache[str] ||
(
/^[\p{L}\d]+$/iu.test(str) &&
str.charAt(0).toUpperCase() + str.slice(1)
) ||
str.replace(
/([\p{L}\d])([\p{L}\d]*)/giu,
(g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase()
).replace(/[^\p{L}\d]/giu, '');
return pascalCache[str];
}
const knownLayouts = new Set;
const knownNotLayouts = new Set;
function defaultLayoutPlugin() {
return function (tree, file) {
const filePathFull = file.history[0].replace(/\/[^\/]+$/, '');
const pagesPathFull = join(file.cwd, pagesPath);
const nestedDirs = filePathFull.slice(pagesPathFull.length + 1);
const directories = nestedDirs ? nestedDirs.split('/').reverse() : [];
let layout = defaultLayout;
directories.some(directory => {
const layoutName = toPascalCase(directory);
if (
knownLayouts.has(layoutName) ||
(
!knownNotLayouts.has(layoutName) &&
existsSync(join(layoutPath, layoutName + '.astro'))
)
) {
knownLayouts.add(layoutName);
layout = layoutName;
return true;
} else {
knownNotLayouts.add(layoutName);
}
});
file.data.astro.frontmatter.layout = `${layoutAlias}/${layout}.astro`;
}
} Notable changes:
|
export default defineConfig({
markdown: {
remarkPlugins: [defaultLayoutPlugin],
extendDefaultPlugins: true
},
}); |
Does anyone know where this is now at? The docs don't mention anything about this. Has progress halted? |
There hasn't been any progress. The only pull request implementing this RFC was closed and there hasn't been anything since. |
FYI, if anyone is still trying to solve this, I solved this by running the following in the terminal (from the root directory) to insert the string for file in src/pages/articles/*; do (head -n 1 $file && echo "layout: ../../layouts/ArticleLayout.astro" && tail -n +2 $file) > temp && mv temp $file; done You can modify Explanation
|
Just a heads up that this is possible in v2 using a custom remark plugin. const DEFAULT_LAYOUT = '../layouts/ArticleLayout.astro';
function setDefaultLayout() {
return function (_, file) {
const { frontmatter } = file.data.astro;
if (!frontmatter.layout) frontmatter.layout = DEFAULT_LAYOUT;
};
} // astro.config.mjs
export default defineConfig({
// ...
markdown: {
remarkPlugins: [setDefaultLayout],
},
}); You could even make it dynamic based on other |
Heads up, this is also no longer necessary if you use content collections. You can edit the |
I'm glad that recent updates have made it easier to work around the lack of default layouts, but I still haven't seen anything that officially addresses the original RFC: Support a default layout |
If anyone wishes to revive this conversation, here are instructions on starting a proposal (RFC precursor): Interested parties should also take a look at the Might be wise to request feedback on Discord early on, to try to feel out which approach is likely to gain traction with Astro's maintainer team. |
Currently each markdown page needs a layout set, like this:
But I'd like to be able to have everything in a directory use a default layout if one isn't already set. That way I can drag 1000 markdown files into a directory and have everything have a layout automatically.
The text was updated successfully, but these errors were encountered: