-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Change frontmatter injection ordering #5687
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3c88348
feat: make user frontmatter accessible in md
bholmesdev 3e7f17e
test: new frontmatter injection
bholmesdev c7e3263
refactor: move injection utils to remark pkg
bholmesdev a890717
fix: add dist/internal to remark exports
bholmesdev dfd26ff
feat: update frontmater injection in mdx
bholmesdev cdc2599
tests: new mdx injection
bholmesdev 0a541b2
chore: changeset
bholmesdev 13f9900
chore: simplify frontmatter destructuring
bholmesdev df8b606
fix: remove old _internal references
bholmesdev 38dfcc6
refactor: injectedFrontmatter -> remarkPluginFrontmatter
bholmesdev 9fd2fbb
docs: add content collections change
bholmesdev 3d50fa8
chore: changeset heading levels
bholmesdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -520,6 +520,20 @@ See https://docs.astro.build/en/guides/server-side-rendering/ for more informati | |
}, | ||
hint: 'See https://docs.astro.build/en/guides/content-collections/ for more information on content schemas.', | ||
}, | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💚 this |
||
* @docs | ||
* @see | ||
* - [Frontmatter injection](https://docs.astro.build/en/guides/markdown-content/#example-injecting-frontmatter) | ||
* @description | ||
* A remark or rehype plugin attempted to inject invalid frontmatter. This occurs when "astro.frontmatter" is set to `null`, `undefined`, or an invalid JSON object. | ||
*/ | ||
InvalidFrontmatterInjectionError: { | ||
title: 'Invalid frontmatter injection.', | ||
code: 6003, | ||
message: | ||
'A remark or rehype plugin attempted to inject invalid frontmatter. Ensure "astro.frontmatter" is set to a valid JSON object that is not `null` or `undefined`.', | ||
hint: 'See the frontmatter injection docs https://docs.astro.build/en/guides/markdown-content/#example-injecting-frontmatter for more information.', | ||
}, | ||
// Config Errors - 7xxx | ||
UnknownConfigError: { | ||
title: 'Unknown configuration error.', | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import ancestor from 'common-ancestor-path'; | ||
import type { Data } from 'vfile'; | ||
import type { AstroConfig, MarkdownAstroData } from '../@types/astro'; | ||
import type { AstroConfig } from '../@types/astro'; | ||
import { | ||
appendExtension, | ||
appendForwardSlash, | ||
|
@@ -36,33 +35,6 @@ export function getFileInfo(id: string, config: AstroConfig) { | |
return { fileId, fileUrl }; | ||
} | ||
|
||
function isValidAstroData(obj: unknown): obj is MarkdownAstroData { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move frontmatter injection utils to |
||
if (typeof obj === 'object' && obj !== null && obj.hasOwnProperty('frontmatter')) { | ||
const { frontmatter } = obj as any; | ||
try { | ||
// ensure frontmatter is JSON-serializable | ||
JSON.stringify(frontmatter); | ||
} catch { | ||
return false; | ||
} | ||
return typeof frontmatter === 'object' && frontmatter !== null; | ||
} | ||
return false; | ||
} | ||
|
||
export function safelyGetAstroData(vfileData: Data): MarkdownAstroData { | ||
const { astro } = vfileData; | ||
|
||
if (!astro) return { frontmatter: {} }; | ||
if (!isValidAstroData(astro)) { | ||
throw Error( | ||
`[Markdown] A remark or rehype plugin tried to add invalid frontmatter. Ensure "astro.frontmatter" is a JSON object!` | ||
); | ||
} | ||
|
||
return astro; | ||
} | ||
|
||
/** | ||
* Normalizes different file names like: | ||
* | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems
frontmatter
needed to be added here where_internal
was removed.