-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(mdx): Add support for turning ![]() into <Image> #6824
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
16d3f97
feat(mdx): Add support for turning ![]() into <Image>
Princesseuh 479c941
test: add test
Princesseuh 7ab3aa3
chore: changeset
Princesseuh b84e480
Merge branch 'main' into feat/mdx-images
Princesseuh 22980b1
chore: lockfile
Princesseuh 98e5945
test: extend tests as requested
Princesseuh f11a742
Merge branch 'main' into feat/mdx-images
Princesseuh 50daa43
chore: lockfile
Princesseuh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.webp
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
packages/integrations/mdx/test/fixtures/mdx-images/src/pages/index.mdx
This file contains hidden or 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,3 @@ | ||
 | ||
|
||
Princesseuh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
 |
This file contains hidden or 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,34 @@ | ||
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(2); | ||
expect(imgs.item(0).src.startsWith('/_image')).to.be.true; | ||
expect(imgs.item(1).src.startsWith('/_image')).to.be.true; | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.