You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
8
+
9
+
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:
10
+
11
+
```ts
12
+
exportfunction remarkInjectSocialImagePlugin() {
13
+
returnfunction (tree, file) {
14
+
const { frontmatter } =file.data.astro;
15
+
frontmatter.socialImageSrc=newURL(
16
+
frontmatter.imageSrc,
17
+
'https://my-blog.com/',
18
+
).pathname;
19
+
}
20
+
}
21
+
```
22
+
23
+
#### Content Collections - new `remarkPluginFrontmatter` property
24
+
25
+
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.
26
+
27
+
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.
28
+
29
+
30
+
#### Migration instructions
31
+
32
+
Plugin authors should now **check for user frontmatter when applying defaults.**
33
+
34
+
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:
35
+
36
+
```diff
37
+
export function remarkInjectTitlePlugin() {
38
+
return function (tree, file) {
39
+
const { frontmatter } = file.data.astro;
40
+
+ if (!frontmatter.title) {
41
+
frontmatter.title = 'Default title';
42
+
+ }
43
+
}
44
+
}
45
+
```
46
+
47
+
This differs from previous behavior, where a Markdown file's frontmatter would _always_ override frontmatter injected via remark or reype.
* 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.
529
+
*/
530
+
InvalidFrontmatterInjectionError: {
531
+
title: 'Invalid frontmatter injection.',
532
+
code: 6003,
533
+
message:
534
+
'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`.',
535
+
hint: 'See the frontmatter injection docs https://docs.astro.build/en/guides/markdown-content/#example-injecting-frontmatter for more information.',
0 commit comments