Skip to content

Commit

Permalink
Normalize render function return value (#11663)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Aug 9, 2024
1 parent acb6a7a commit d13fb8c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
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>;
}
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>

0 comments on commit d13fb8c

Please sign in to comment.