@@ -195,6 +195,9 @@ should be installed as one of your project's `dependencies`:
195195npm install --save mdx-bundler
196196```
197197
198+ One of mdx-bundler's dependancies requires a working [ node-gyp] [ node-gyp ] setup
199+ to be able to install correctly.
200+
198201## Usage
199202
200203``` typescript
@@ -481,6 +484,68 @@ export const exampleImage = 'https://example.com/image.jpg'
481484<img src = { exampleImage } alt = " Image alt text" />
482485```
483486
487+ ### Image Bundling
488+
489+ With the [ cwd] ( #cwd ) and the remark plugin
490+ [ remark-mdx-images] ( https://www.npmjs.com/package/remark-mdx-images ) you can
491+ bundle images in your mdx!
492+
493+ There are two loaders in esbuild that can be used here. The easiest is ` dataurl `
494+ which outputs the images as inline data urls in the returned code.
495+
496+ ``` js
497+ import {remarkMdxImages } from ' remark-mdx-images'
498+
499+ const {code } = await bundleMDX (mdxSource, {
500+ cwd: ' /users/you/site/_content/pages' ,
501+ xdmOptions : (vFile , options ) => {
502+ options .remarkPlugins = [remarkMdxImages]
503+
504+ return options
505+ },
506+ esbuildOptions : options => {
507+ options .loader = {
508+ ... options .loader ,
509+ ' .png' : ' dataurl' ,
510+ }
511+
512+ return options
513+ },
514+ })
515+ ```
516+
517+ The ` file ` loader requires a little more configuration to get working. With the
518+ ` file ` loader your images are copied to the output directory so esbuild needs to
519+ be set to write files and needs to know where to put them plus the url of the
520+ folder to be used in image sources.
521+
522+ ``` js
523+ const {code } = await bundleMDX (mdxSource, {
524+ cwd: ' /users/you/site/_content/pages' ,
525+ xdmOptions : (vFile , options ) => {
526+ options .remarkPlugins = [remarkMdxImages]
527+
528+ return options
529+ },
530+ esbuildOptions : options => {
531+ // Set the `outdir` to your public directory.
532+ options .outdir = ' /users/you/site/public/img'
533+ options .loader = {
534+ ... options .loader ,
535+ // Tell esbuild to use the `file` loader for pngs
536+ ' .png' : ' file' ,
537+ }
538+ // Set the public path to /img/ so image sources start /img/
539+ options .publicPath = ' /img/'
540+
541+ // Set write to true so that esbuild will output the files.
542+ options .write = true
543+
544+ return options
545+ },
546+ })
547+ ```
548+
484549### Known Issues
485550
486551#### Cloudflare Workers
624689[ bugs ] : https://github.com/kentcdodds/mdx-bundler/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug
625690[ requests ] : https://github.com/kentcdodds/mdx-bundler/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
626691[ good-first-issue ] : https://github.com/kentcdodds/mdx-bundler/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement+label%3A%22good+first+issue%22
692+ [ node-gyp ] : https://github.com/nodejs/node-gyp#installation
627693<!-- prettier-ignore-end -->
0 commit comments