-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change frontmatter injection ordering (#5687)
* feat: make user frontmatter accessible in md * test: new frontmatter injection * refactor: move injection utils to remark pkg * fix: add dist/internal to remark exports * feat: update frontmater injection in mdx * tests: new mdx injection * chore: changeset * chore: simplify frontmatter destructuring * fix: remove old _internal references * refactor: injectedFrontmatter -> remarkPluginFrontmatter * docs: add content collections change * chore: changeset heading levels
- Loading branch information
1 parent
16c7d0b
commit e2019be
Showing
29 changed files
with
234 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
'astro': major | ||
'@astrojs/markdown-remark': major | ||
'@astrojs/mdx': minor | ||
--- | ||
|
||
Give remark and rehype plugins access to user frontmatter via frontmatter injection. This means `data.astro.frontmatter` is now the _complete_ Markdown or MDX document's frontmatter, rather than an empty object. | ||
|
||
This allows plugin authors to modify existing frontmatter, or compute new properties based on other properties. For example, say you want to compute a full image URL based on an `imageSrc` slug in your document frontmatter: | ||
|
||
```ts | ||
export function remarkInjectSocialImagePlugin() { | ||
return function (tree, file) { | ||
const { frontmatter } = file.data.astro; | ||
frontmatter.socialImageSrc = new URL( | ||
frontmatter.imageSrc, | ||
'https://my-blog.com/', | ||
).pathname; | ||
} | ||
} | ||
``` | ||
|
||
#### Content Collections - new `remarkPluginFrontmatter` property | ||
|
||
We have changed _inject_ frontmatter to _modify_ frontmatter in our docs to improve discoverability. This is based on support forum feedback, where "injection" is rarely the term used. | ||
|
||
To reflect this, the `injectedFrontmatter` property has been renamed to `remarkPluginFrontmatter`. This should clarify this plugin is still separate from the `data` export Content Collections expose today. | ||
|
||
|
||
#### Migration instructions | ||
|
||
Plugin authors should now **check for user frontmatter when applying defaults.** | ||
|
||
For example, say a remark plugin wants to apply a default `title` if none is present. Add a conditional to check if the property is present, and update if none exists: | ||
|
||
```diff | ||
export function remarkInjectTitlePlugin() { | ||
return function (tree, file) { | ||
const { frontmatter } = file.data.astro; | ||
+ if (!frontmatter.title) { | ||
frontmatter.title = 'Default title'; | ||
+ } | ||
} | ||
} | ||
``` | ||
|
||
This differs from previous behavior, where a Markdown file's frontmatter would _always_ override frontmatter injected via remark or reype. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/astro/test/fixtures/astro-markdown-frontmatter-injection/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import { rehypeReadingTime, remarkTitle } from './src/markdown-plugins.mjs' | ||
import { rehypeReadingTime, remarkTitle, remarkDescription } from './src/markdown-plugins.mjs' | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
site: 'https://astro.build/', | ||
markdown: { | ||
remarkPlugins: [remarkTitle], | ||
remarkPlugins: [remarkTitle, remarkDescription], | ||
rehypePlugins: [rehypeReadingTime], | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...es/astro/test/fixtures/astro-markdown-frontmatter-injection/src/pages/page-1.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
--- | ||
description: 'Page 1 description' | ||
--- | ||
|
||
# Page 1 | ||
|
||
Look at that! |
4 changes: 4 additions & 0 deletions
4
...es/astro/test/fixtures/astro-markdown-frontmatter-injection/src/pages/page-2.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
--- | ||
description: 'Page 2 description' | ||
--- | ||
|
||
# Page 2 | ||
|
||
## Table of contents | ||
|
7 changes: 0 additions & 7 deletions
7
.../test/fixtures/astro-markdown-frontmatter-injection/src/pages/with-overrides.md
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.