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

Content Layer #982

Merged
merged 14 commits into from
Sep 17, 2024
31 changes: 24 additions & 7 deletions proposals/0047-content-layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ export interface ScopedDataStore {
digest?: number | string;
/** The rendered content, if applicable. */
rendered?: RenderedContent;
/** If an entry is a deferred, its rendering phase is delegated to a virtual module during the runtime phase when calling `renderEntry`. */
deferredRender: boolean
}) => boolean;
values: () => Array<DataEntry>;
keys: () => Array<string>;
Expand Down Expand Up @@ -304,7 +302,26 @@ const { craft } = Astro.props;

### Rendered content

Some entry types may have HTML content that can be rendered as a component. While this can be accessed like any other property, a loader can also store the rendered HTML in the `rendered.html` property. This allows users to use the `<Content />` component to render the HTML. The `rendered` property can also include metadata such as frontmatter or headings, which can be accessed as properties on the `rendered.metadata` object.
Some entry types may have HTML content that can be rendered as a component. While this can be accessed like any other property, a loader can also store the rendered HTML in the `rendered.html` property. This allows users to use the `<Content />` component to render the HTML. The `rendered` property can also include metadata such as frontmatter or headings, which can be accessed as properties on the `rendered.metadata` object:

```ts
// src/content/config.ts
store.set({
id,
data,
rendered: {
// A raw HTML string
html: data.description ?? "",
metadata: {
// Optionally, metadata such as headings can be stored here
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
headings: data.headings ?? [],
},
},
digest,
});
```

This can then be accessed in the page like this:

```astro
---
Expand All @@ -328,7 +345,7 @@ export const getStaticPaths: GetStaticPaths = async () => {

const { craft } = Astro.props;
// The `render()` helper can be used to render the HTML content of an entry. If an entry doesn't have rendered content, it will return an empty component.
const { Content } = await render(craft);
const { Content, headings } = await render(craft);
---

<h1>{craft.data.title}</h1>
Expand Down Expand Up @@ -393,7 +410,7 @@ const dogs = defineCollection({
// astro.config.mjs
export default defineConfig({
experimental: {
contentLayer: true
}
})
contentLayer: true,
},
});
```