-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mdx): Add support for turning ![]() into <Image> (#6824)
- Loading branch information
1 parent
948a6d7
commit 2511d58
Showing
12 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@astrojs/mdx': minor | ||
'@astrojs/markdown-remark': patch | ||
--- | ||
|
||
Add support for using optimized and relative images in MDX files with `experimental.assets` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/integrations/mdx/src/remark-images-to-component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import type { MarkdownVFile } from '@astrojs/markdown-remark'; | ||
import { type Image, type Parent } from 'mdast'; | ||
import type { MdxJsxFlowElement, MdxjsEsm } from 'mdast-util-mdx'; | ||
import { visit } from 'unist-util-visit'; | ||
import { jsToTreeNode } from './utils.js'; | ||
|
||
export function remarkImageToComponent() { | ||
return function (tree: any, file: MarkdownVFile) { | ||
if (!file.data.imagePaths) return; | ||
|
||
const importsStatements: MdxjsEsm[] = []; | ||
const importedImages = new Map<string, string>(); | ||
|
||
visit(tree, 'image', (node: Image, index: number | null, parent: Parent | null) => { | ||
// Use the imagePaths set from the remark-collect-images so we don't have to duplicate the logic for | ||
// checking if an image should be imported or not | ||
if (file.data.imagePaths?.has(node.url)) { | ||
let importName = importedImages.get(node.url); | ||
|
||
// If we haven't already imported this image, add an import statement | ||
if (!importName) { | ||
importName = `__${importedImages.size}_${node.url.replace(/\W/g, '_')}__`; | ||
|
||
importsStatements.push({ | ||
type: 'mdxjsEsm', | ||
value: '', | ||
data: { | ||
estree: { | ||
type: 'Program', | ||
sourceType: 'module', | ||
body: [ | ||
{ | ||
type: 'ImportDeclaration', | ||
source: { type: 'Literal', value: node.url, raw: JSON.stringify(node.url) }, | ||
specifiers: [ | ||
{ | ||
type: 'ImportDefaultSpecifier', | ||
local: { type: 'Identifier', name: importName }, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
}); | ||
importedImages.set(node.url, importName); | ||
} | ||
|
||
// Build a component that's equivalent to <Image src={importName} alt={node.alt} title={node.title} /> | ||
const componentElement: MdxJsxFlowElement = { | ||
name: '__AstroImage__', | ||
type: 'mdxJsxFlowElement', | ||
attributes: [ | ||
{ | ||
name: 'src', | ||
type: 'mdxJsxAttribute', | ||
value: { | ||
type: 'mdxJsxAttributeValueExpression', | ||
value: importName, | ||
data: { | ||
estree: { | ||
type: 'Program', | ||
sourceType: 'module', | ||
comments: [], | ||
body: [ | ||
{ | ||
type: 'ExpressionStatement', | ||
expression: { type: 'Identifier', name: importName }, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ name: 'alt', type: 'mdxJsxAttribute', value: node.alt || '' }, | ||
], | ||
children: [], | ||
}; | ||
|
||
if (node.title) { | ||
componentElement.attributes.push({ | ||
type: 'mdxJsxAttribute', | ||
name: 'title', | ||
value: node.title, | ||
}); | ||
} | ||
|
||
parent!.children.splice(index!, 1, componentElement); | ||
} | ||
}); | ||
|
||
// Add all the import statements to the top of the file for the images | ||
tree.children.unshift(...importsStatements); | ||
|
||
// Add an import statement for the Astro Image component, we rename it to avoid conflicts | ||
tree.children.unshift(jsToTreeNode(`import { Image as __AstroImage__ } from "astro:assets";`)); | ||
}; | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/integrations/mdx/test/fixtures/mdx-images/astro.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import mdx from '@astrojs/mdx'; | ||
|
||
export default { | ||
integrations: [mdx()], | ||
experimental: { | ||
assets: true | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/integrations/mdx/test/fixtures/mdx-images/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "@test/mdx-page", | ||
"dependencies": { | ||
"@astrojs/mdx": "workspace:*", | ||
"astro": "workspace:*", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
} | ||
} |
Binary file added
BIN
+3.64 KB
packages/integrations/mdx/test/fixtures/mdx-images/src/assets/houston in space.webp
Binary file not shown.
Binary file added
BIN
+3.64 KB
packages/integrations/mdx/test/fixtures/mdx-images/src/assets/houston.webp
Binary file not shown.
11 changes: 11 additions & 0 deletions
11
packages/integrations/mdx/test/fixtures/mdx-images/src/pages/index.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Image using a relative path: | ||
![Houston](../assets/houston.webp) | ||
|
||
Image using an aliased path: | ||
![Houston](~/assets/houston.webp) | ||
|
||
Image with a title: | ||
![Houston](~/assets/houston.webp "Houston title") | ||
|
||
Image with spaces in the path: | ||
![Houston](<~/assets/houston in space.webp>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { expect } from 'chai'; | ||
import { parseHTML } from 'linkedom'; | ||
import { loadFixture } from '../../../astro/test/test-utils.js'; | ||
|
||
describe('MDX Page', () => { | ||
let devServer; | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: new URL('./fixtures/mdx-images/', import.meta.url), | ||
}); | ||
devServer = await fixture.startDevServer(); | ||
}); | ||
|
||
after(async () => { | ||
await devServer.stop(); | ||
}); | ||
|
||
describe('Optimized images in MDX', () => { | ||
it('works', async () => { | ||
const res = await fixture.fetch('/'); | ||
expect(res.status).to.equal(200); | ||
|
||
const html = await res.text(); | ||
const { document } = parseHTML(html); | ||
|
||
const imgs = document.getElementsByTagName('img'); | ||
expect(imgs.length).to.equal(4); | ||
// Image using a relative path | ||
expect(imgs.item(0).src.startsWith('/_image')).to.be.true; | ||
// Image using an aliased path | ||
expect(imgs.item(1).src.startsWith('/_image')).to.be.true; | ||
// Image with title | ||
expect(imgs.item(2).title).to.equal('Houston title'); | ||
// Image with spaces in the path | ||
expect(imgs.item(3).src.startsWith('/_image')).to.be.true; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.