Skip to content

Commit

Permalink
Handle edge case in jsx-runtime (#4158)
Browse files Browse the repository at this point in the history
* fix(#4135): handle edge case in jsx-runtime

* test: add mdx test case

* chore: fix utils reference

* test: fix mdx escape test

Co-authored-by: Nate Moore <nate@astro.build>
  • Loading branch information
natemoo-re and natemoo-re committed Aug 5, 2022
1 parent e7bee22 commit e569f0a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-vans-deliver.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix edge case where MDX components would be escaped
4 changes: 2 additions & 2 deletions packages/astro/src/runtime/server/jsx.ts
Expand Up @@ -51,12 +51,12 @@ export async function renderJSX(result: SSRResult, vnode: any): Promise<any> {
props[key] = value;
}
}
return await renderToString(result, vnode.type as any, props, slots);
return markHTMLString(await renderToString(result, vnode.type as any, props, slots));
}
case !vnode.type && (vnode.type as any) !== 0:
return '';
case typeof vnode.type === 'string' && vnode.type !== ClientOnlyPlaceholder:
return await renderElement(result, vnode.type as string, vnode.props ?? {});
return markHTMLString(await renderElement(result, vnode.type as string, vnode.props ?? {}));
}

if (vnode.type) {
Expand Down
@@ -0,0 +1,7 @@
<em><slot/></em>

<style>
em {
color: red;
}
</style>
@@ -0,0 +1 @@
<p><slot /></p>
@@ -0,0 +1 @@
<h1><slot/></h1>
@@ -0,0 +1,5 @@
import P from '../components/P.astro';
import Em from '../components/Em.astro';

<P>Render <Em>Me</Em></P>
<P><Em>Me</Em></P>
@@ -0,0 +1,13 @@
import P from '../components/P.astro';
import Em from '../components/Em.astro';
import Title from '../components/Title.astro';

export const components = { p: P, em: Em, h1: Title };

# Hello _there_

# _there_

Hello _there_

_there_
32 changes: 32 additions & 0 deletions packages/integrations/mdx/test/mdx-escape.test.js
@@ -0,0 +1,32 @@
import mdx from '@astrojs/mdx';

import { expect } from 'chai';
import { parseHTML } from 'linkedom';
import { loadFixture } from '../../../astro/test/test-utils.js';

const FIXTURE_ROOT = new URL('./fixtures/mdx-escape/', import.meta.url);

describe('MDX frontmatter', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: FIXTURE_ROOT,
integrations: [mdx()],
});
await fixture.build();
});

it('does not have unescaped HTML at top-level', async () => {
const html = await fixture.readFile('/index.html');
const { document } = parseHTML(html);

expect(document.body.textContent).to.not.include('<em');
});

it('does not have unescaped HTML inside html tags', async () => {
const html = await fixture.readFile('/html-tag/index.html');
const { document } = parseHTML(html);

expect(document.body.textContent).to.not.include('<em');
});
});

0 comments on commit e569f0a

Please sign in to comment.