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

[MD] Add rawContent and compiledContent to MD layout props #4222

Merged
merged 4 commits into from Aug 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-socks-exercise.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add rawContent and compiledContent to MD layout props
20 changes: 19 additions & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Expand Up @@ -92,10 +92,28 @@ export default function markdown({ config, logging }: AstroPluginOptions): Plugi
throw new Error('The "astro" property is no longer supported! To access "headings" from your layout, try using "Astro.props.headings."')
}
});
Object.defineProperty(content.astro, 'html', {
get() {
throw new Error('The "astro" property is no longer supported! To access "html" from your layout, try using "Astro.props.compiledContent()."')
}
});
Object.defineProperty(content.astro, 'source', {
get() {
throw new Error('The "astro" property is no longer supported! To access "source" from your layout, try using "Astro.props.rawContent()."')
}
});
const contentFragment = h(Fragment, { 'set:html': html });
return ${
layout
? `h(Layout, { content, frontmatter: content, headings: getHeadings(), 'server:root': true, children: contentFragment })`
? `h(Layout, {
content,
frontmatter: content,
headings: getHeadings(),
rawContent,
compiledContent,
'server:root': true,
children: contentFragment
})`
: `contentFragment`
};
}
Expand Down
22 changes: 22 additions & 0 deletions packages/astro/test/astro-markdown.test.js
Expand Up @@ -89,6 +89,28 @@ describe('Astro Markdown', () => {
expect(headingSlugs).to.contain('section-2');
});

it('Passes compiled content to layout via "compiledContent" prop', async () => {
const html = await fixture.readFile('/with-layout/index.html');
const $ = cheerio.load(html);

const compiledContent = $('[data-compiled-content]');

expect(fixLineEndings(compiledContent.text()).trim()).to.equal(
`<h2 id="section-1">Section 1</h2>\n<h2 id="section-2">Section 2</h2>`
);
});

it('Passes raw content to layout via "rawContent" prop', async () => {
const html = await fixture.readFile('/with-layout/index.html');
const $ = cheerio.load(html);

const rawContent = $('[data-raw-content]');

expect(fixLineEndings(rawContent.text()).trim()).to.equal(
`## Section 1\n\n## Section 2`
);
});

it('Exposes getHeadings() on glob imports', async () => {
const { headings } = JSON.parse(await fixture.readFile('/headings-glob.json'));

Expand Down
Expand Up @@ -3,6 +3,8 @@ const {
content = { title: "content didn't work" },
frontmatter = { title: "frontmatter didn't work" },
headings = [],
compiledContent = () => '',
rawContent = () => '',
} = Astro.props;
---

Expand All @@ -18,6 +20,8 @@ const {
<body>
<p data-content-title>{content.title}</p>
<p data-frontmatter-title>{frontmatter.title}</p>
<p data-compiled-content>{compiledContent()}</p>
<p data-raw-content>{rawContent()}</p>
<p data-layout-rendered>Layout rendered!</p>
<ul data-headings>
{headings.map(heading => <li>{heading.slug}</li>)}
Expand Down