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 cannot find relative image path without leading ./ #10754

Merged
merged 11 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions .changeset/curly-badgers-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@astrojs/mdx": patch
---

Fixes an issue where images in MDX required a relative specifier (e.g. `./`)

Now, you can use the standard `![](img.png)` syntax in MDX files for images colocated in the same folder: no relative specifier required!

There is no need to update your project; your existing images will still continue to work. However, you may wish to remove any relative specifiers from these MDX images as they are no longer necessary:

```diff
- ![A cute dog](./dog.jpg)
+ ![A cute dog](dog.jpg)
<!-- This dog lives in the same folder as my article! -->
rishi-raj-jain marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 7 additions & 3 deletions packages/integrations/mdx/src/remark-images-to-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function remarkImageToComponent() {
// If we haven't already imported this image, add an import statement
if (!importName) {
importName = `__${importedImages.size}_${node.url.replace(/\W/g, '_')}__`;

const prefixedNodeUrl = (node.url.includes('/') ? '' : './') + node.url;
rishi-raj-jain marked this conversation as resolved.
Show resolved Hide resolved
importsStatements.push({
type: 'mdxjsEsm',
value: '',
Expand All @@ -35,7 +35,11 @@ export function remarkImageToComponent() {
body: [
{
type: 'ImportDeclaration',
source: { type: 'Literal', value: node.url, raw: JSON.stringify(node.url) },
source: {
type: 'Literal',
value: prefixedNodeUrl,
raw: JSON.stringify(prefixedNodeUrl),
},
specifiers: [
{
type: 'ImportDefaultSpecifier',
Expand All @@ -47,7 +51,7 @@ export function remarkImageToComponent() {
},
},
});
importedImages.set(node.url, importName);
importedImages.set(prefixedNodeUrl, importName);
}

// Build a component that's equivalent to <Image src={importName} alt={node.alt} title={node.title} />
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ Image with a title:

Image with spaces in the path:
![Houston](<~/assets/houston in space.webp>)

Image using a relative path with no slashes:
![Houston](houston.png)
4 changes: 3 additions & 1 deletion packages/integrations/mdx/test/mdx-images.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('MDX Page', () => {
const { document } = parseHTML(html);

const imgs = document.getElementsByTagName('img');
assert.equal(imgs.length, 4);
assert.equal(imgs.length, 5);
// Image using a relative path
assert.equal(imgs.item(0).src.startsWith('/_image'), true);
// Image using an aliased path
Expand All @@ -38,6 +38,8 @@ describe('MDX Page', () => {
assert.equal(imgs.item(2).title, 'Houston title');
// Image with spaces in the path
assert.equal(imgs.item(3).src.startsWith('/_image'), true);
// Image using a relative path with no slashes
assert.equal(imgs.item(4).src.startsWith('/_image'), true);
});

for (const route of imageTestRoutes) {
Expand Down
Loading