Skip to content
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

Injected frontmatter change #2250

Merged
merged 13 commits into from
Jan 3, 2023
Merged
8 changes: 6 additions & 2 deletions src/pages/en/guides/content-collections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ const blogPosts = await getCollection('blog');
})}
```

### Access injected frontmatter from `render()`
### Access modified frontmatter from `render()`

Astro allows you to [inject frontmatter using remark or rehype plugins.](/en/guides/markdown-content/#example-injecting-frontmatter) You can access these values using the `injectedFrontmatter` property from `render()`:
Astro allows you to [modify frontmatter using remark or rehype plugins](/en/guides/markdown-content/#modifying-frontmatter-programmatically). You can access this modified frontmatter using the `injectedFrontmatter` property from `render()`:

```astro "{ injectedFrontmatter }"
---
Expand All @@ -408,6 +408,10 @@ const blogPosts = await getCollection('blog');

Assuming `readingTime` was injected ([see our reading time example](/en/guides/markdown-content/#example-calculate-reading-time)), it will be available on the `injectedFrontmatter` object.

:::caution
Remark and rehype plugins will have access to the _raw_ Markdown or MDX document frontmatter. This means frontmatter will not reflect any changes or defaults applied by your `schema`.
:::

<details>
<summary>**🙋 Why don't `getCollection()` and `getEntry()` contain these values?**</summary>

Expand Down
8 changes: 7 additions & 1 deletion src/pages/en/guides/markdown-content.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ export default {
}
```

#### Example: Injecting frontmatter
### Modifying frontmatter programmatically

You can add frontmatter properties to all of your Markdown and MDX files by using a [remark or rehype plugin](#markdown-plugins).

Expand All @@ -497,6 +497,12 @@ You can add frontmatter properties to all of your Markdown and MDX files by usin
}
```

:::tip
<Since v="2.0.0" />
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved

`data.astro.frontmatter` contains all properties from a given Markdown or MDX document. This allows you to modify existing frontmatter properties, or compute new properties based on other properties.
bholmesdev marked this conversation as resolved.
Show resolved Hide resolved
:::

2. Apply this plugin to your `markdown` or `mdx` integration config:

```js title="astro.config.mjs" "import { exampleRemarkPlugin } from './example-remark-plugin.mjs';" "remarkPlugins: [exampleRemarkPlugin],"
Expand Down
2 changes: 1 addition & 1 deletion src/pages/en/reference/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ A function to compile a given Markdown or MDX document for rendering. This retur

- `<Content />` - A component used to render the document's contents in an Astro file.
- `headings` - A generated list of headings, [mirroring Astro's `getHeadings()` utility](/en/guides/markdown-content/#exported-properties) on Markdown and MDX imports.
- `injectedFrontmatter ` - An object of frontmatter [injected via remark or rehype plugins](/en/guides/markdown-content/#example-injecting-frontmatter). Set to type `any`.
- `injectedFrontmatter ` - The [modified frontmatter object](/en/guides/markdown-content/#modifying-frontmatter-programmatically) applied via remark or rehype plugins. Set to type `any`.

```astro
---
Expand Down