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

Normalize content layer render function return value #11663

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export type TransitionAnimationValue =
| TransitionDirectionalAnimations;

// Allow users to extend this for astro-jsx.d.ts

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
export interface AstroClientDirectives {}

Expand Down
13 changes: 11 additions & 2 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,12 @@ export async function renderEntry(
// @ts-expect-error virtual module
const { default: contentModules } = await import('astro:content-module-imports');
const module = contentModules.get(entry.filePath);
return await module();
const deferredMod = await module();
return {
Content: deferredMod.Content,
headings: deferredMod.getHeadings?.() ?? [],
remarkPluginFrontmatter: deferredMod.frontmatter ?? {},
};
} catch (e) {
// eslint-disable-next-line
console.error(e);
Expand All @@ -462,7 +467,11 @@ export async function renderEntry(
: entry?.rendered?.html;

const Content = createComponent(() => serverRender`${unescapeHTML(html)}`);
return { Content };
return {
Content,
headings: entry?.rendered?.metadata?.headings ?? [],
remarkPluginFrontmatter: entry?.rendered?.metadata?.frontmatter ?? {},
};
}

async function render({
Expand Down
18 changes: 9 additions & 9 deletions packages/astro/templates/content/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
declare module 'astro:content' {
interface Render {
'.md': Promise<{
Content: import('astro').MarkdownInstance<{}>['Content'];
headings: import('astro').MarkdownHeading[];
remarkPluginFrontmatter: Record<string, any>;
}>;
}
interface ContentLayerRenderResult {

interface RenderResult {
Content: import('astro/runtime/server/index.js').AstroComponentFactory;
headings: import('astro').MarkdownHeading[];
remarkPluginFrontmatter: Record<string, any>;
Comment on lines +5 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these

}
interface Render {
'.md': Promise<RenderResult>;
}


export interface RenderedContent {
html: string;
Expand Down Expand Up @@ -113,7 +113,7 @@ declare module 'astro:content' {

export function render<C extends keyof AnyEntryMap>(
entry: AnyEntryMap[C][string],
): Promise<ContentLayerRenderResult>;
): Promise<RenderResult>;

export function reference<C extends keyof AnyEntryMap>(
collection: C,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import H2 from "../src/components/H2.astro";

<H2>Iguana</H2>

### Iguana

This is a rendered entry

![file](./shuttle.jpg)
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export const getStaticPaths = (async () => {
const { craft } = Astro.props as any

let cat = craft.data.cat ? await getEntry(craft.data.cat) : undefined
const { Content } = await render(craft)
const { Content, headings } = await render(craft)

---
<meta charset="utf-8">
<h1>{craft.data.title}</h1>
<ul>
{headings.map((heading) => <li><a href={`#${heading.slug}`}>{heading.text}</a></li>)}
</ul>
{cat? <p>🐈: {cat.data.breed}</p> : undefined}
{craft.data.heroImage ? <Image src={craft.data.heroImage} alt={craft.data.title} width="100" height="100"/> : undefined}
<Content />
</ul>
Loading