diff --git a/src/content/docs/en/reference/content-loader-reference.mdx b/src/content/docs/en/reference/content-loader-reference.mdx index 28591b78d6084..f4f9d3023cabd 100644 --- a/src/content/docs/en/reference/content-loader-reference.mdx +++ b/src/content/docs/en/reference/content-loader-reference.mdx @@ -351,6 +351,44 @@ export function feedLoader({ url }): Loader { } ``` +#### `renderMarkdown` + +

+**Type**: `(markdown: string) => Promise` +

+ + +Renders a Markdown string to HTML, returning a `RenderedContent` object. + +This allows allows you to render Markdown content directly within your loaders using the same Markdown processing as Astro's built-in `glob` loader and provides access to the `render()` function and `` component for [rendering body content](/en/guides/content-collections/#rendering-body-content). + +Assign this object to the [rendered](https://github.com/withastro/docs/pull/11801#rendered) field of the [DataEntry](https://github.com/withastro/docs/pull/11801#dataentry) object to allow users to [render the content in a page](https://github.com/en/guides/content-collections/#rendering-body-content). + +```ts title=loader.ts {16-17} +import type { Loader } from 'astro/loaders'; +import { loadFromCMS } from './cms.js'; + +export function myLoader(settings): Loader { + return { + name: 'cms-loader', + async load({ renderMarkdown, store }) { + const entries = await loadFromCMS(); + + store.clear(); + + for (const entry of entries) { + store.set(entry.id, { + id: entry.id, + data: entry, + // Assume each entry has a 'content' field with markdown content + rendered: await renderMarkdown(entry.content), + }); + } + }, + }; +} +``` + #### `generateDigest`

@@ -618,7 +656,7 @@ The format of the `RenderedContent` object is: /** Rendered HTML string. If present then `render(entry)` will return a component that renders this HTML. */ html: string; metadata?: { - /** Any images that are present in this entry. Relative to the {@link DataEntry} filePath. */ + /** Any images that are present in this entry. Relative to the DataEntry filePath. */ imagePaths?: Array; /** Any headings that are present in this file. Returned as `headings` from `render()` */ headings?: MarkdownHeading[]; @@ -629,3 +667,5 @@ The format of the `RenderedContent` object is: }; } ``` + +If the entry has Markdown content then you can use the [`renderMarkdown()`](#rendermarkdown) function to generate this object from the Markdown string.