0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-04-07 23:41:43 -05:00

fix(mdx): don't import image component when no images are used (#12921)

* fix(mdx): don't import image component when no images are used

* Fix test

* Fix test
This commit is contained in:
Matt Kane 2025-01-07 17:07:33 +00:00 committed by GitHub
parent 8b9d530378
commit aeb7e1ac11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 19 additions and 15 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/mdx': patch
---
Fixes a bug that caused Image component to be imported on MDX pages that did not include images

View file

@ -202,7 +202,7 @@ describe('Content Collections - render()', () => {
assert.equal($('ul li').length, 3);
// Includes styles
assert.equal($('head > style').length, 2);
assert.equal($('head > style').length, 1);
assert.ok($('head > style').text().includes("font-family: 'Comic Sans MS'"));
});

View file

@ -1,3 +1,3 @@
export async function GET({ request }) {
return fetch("https://http.im/status/500", request)
return fetch("https://httpstat.us/500", request)
}

View file

@ -141,7 +141,7 @@ describe('API routes in SSR', () => {
const response = await fixture.fetch('/fail');
const text = await response.text();
assert.equal(response.status, 500);
assert.equal(text, '');
assert.equal(text, '500 Internal Server Error');
});
it('Has valid api context', async () => {

View file

@ -101,7 +101,7 @@ describe('Content Collections - render()', () => {
assert.equal($('ul li').length, 3);
// Rendered the styles
assert.equal($('style').length, 2);
assert.equal($('style').length, 1);
},
);
});
@ -158,7 +158,7 @@ describe('Content Collections - render()', () => {
assert.equal($('ul li').length, 3);
// Rendered the styles
assert.equal($('style').length, 2);
assert.equal($('style').length, 1);
},
);
});
@ -225,7 +225,7 @@ describe('Content Collections - render()', () => {
assert.equal($('ul li').length, 3);
// Rendered the styles
assert.equal($('style').length, 2);
assert.equal($('style').length, 1);
},
);
});
@ -291,7 +291,7 @@ describe('Content Collections - render()', () => {
assert.equal($('ul li').length, 3);
// Rendered the styles
assert.equal($('style').length, 2);
assert.equal($('style').length, 1);
},
);
});

View file

@ -73,13 +73,12 @@ function getImageComponentAttributes(props: Properties): MdxJsxAttribute[] {
export function rehypeImageToComponent() {
return function (tree: Root, file: VFile) {
if (!file.data.astro?.imagePaths) return;
if (!file.data.astro?.imagePaths?.length) return;
const importsStatements: MdxjsEsm[] = [];
const importedImages = new Map<string, string>();
visit(tree, 'element', (node, index, parent) => {
if (!file.data.astro?.imagePaths || node.tagName !== 'img' || !node.properties.src) return;
if (!file.data.astro?.imagePaths?.length || node.tagName !== 'img' || !node.properties.src) return;
const src = decodeURI(String(node.properties.src));

View file

@ -28,7 +28,7 @@ describe('Head injection w/ MDX', () => {
const { document } = parseHTML(html);
const links = document.querySelectorAll('head link[rel=stylesheet]');
assert.equal(links.length, 2);
assert.equal(links.length, 1);
const scripts = document.querySelectorAll('script[type=module]');
assert.equal(scripts.length, 1);
@ -67,7 +67,7 @@ describe('Head injection w/ MDX', () => {
const $ = cheerio.load(html);
const headLinks = $('head link[rel=stylesheet]');
assert.equal(headLinks.length, 2);
assert.equal(headLinks.length, 1);
const bodyLinks = $('body link[rel=stylesheet]');
assert.equal(bodyLinks.length, 0);
@ -92,7 +92,7 @@ describe('Head injection w/ MDX', () => {
const $ = cheerio.load(html);
const headLinks = $('head link[rel=stylesheet]');
assert.equal(headLinks.length, 2);
assert.equal(headLinks.length, 1);
const bodyLinks = $('body link[rel=stylesheet]');
assert.equal(bodyLinks.length, 0);

View file

@ -28,7 +28,7 @@ describe('MDX math', () => {
const mjxContainer = document.querySelector('mjx-container[jax="SVG"]');
assert.notEqual(mjxContainer, null);
const mjxStyle = document.querySelectorAll('style')[1].innerHTML;
const mjxStyle = document.querySelectorAll('style')[0].innerHTML;
assert.equal(
mjxStyle.includes('mjx-container[jax="SVG"]'),
true,
@ -62,7 +62,7 @@ describe('MDX math', () => {
const mjxContainer = document.querySelector('mjx-container[jax="CHTML"]');
assert.notEqual(mjxContainer, null);
const mjxStyle = document.querySelectorAll('style')[1].innerHTML;
const mjxStyle = document.querySelectorAll('style')[0].innerHTML;
assert.equal(
mjxStyle.includes('mjx-container[jax="CHTML"]'),
true,