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

Fix MDX style imports when layout is not applied #4443

Merged
merged 3 commits into from
Aug 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-pens-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/mdx': patch
---

Fix MDX style imports when layout is not applied
5 changes: 5 additions & 0 deletions packages/integrations/mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
// These transforms must happen *after* JSX runtime transformations
transform(code, id) {
if (!id.endsWith('.mdx')) return;

// Ensures styles and scripts are injected into a `<head>`
// When a layout is not applied
code += `\nMDXContent[Symbol.for('astro.needsHeadRendering')] = !Boolean(frontmatter.layout);`;

const [, moduleExports] = parseESM(code);

const { fileUrl, fileId } = getFileInfo(id, config);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import '../styles.css'

# Hello page!
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
color: red;
}
9 changes: 9 additions & 0 deletions packages/integrations/mdx/test/mdx-page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ describe('MDX Page', () => {

expect(h1.textContent).to.equal('Hello page!');
});

it('injects style imports when layout is not applied', async () => {
const html = await fixture.readFile('/index.html');
const { document } = parseHTML(html);

const stylesheet = document.querySelector('link[rel="stylesheet"]');

expect(stylesheet).to.not.be.null;
})
});

describe('dev', () => {
Expand Down