Documentation
IntroductionGetting StartedSyntaxComponentsThemingPresentExamplesAPI
@mdxp/core
@mdxp/components
@mdxp/rehypex-plugins
Better Media
Auto Import is a Rehype plugin for MDXHAST that transforms HAST img nodes into JSX nodes, performing the following modifications:
- Create an image or video node.
- Parse width, height and/or style properties from the alternate text field.
- Parse other properties (eg. controls, autoplay, loop) from the alternate text field
Arguments
videoTest
regex
/.(mp4|webm|avi|mpe?g|wmv|ogg)$/i
videoMarker
string
null (not being used)
altSeparator
string
&&
Usage
To use this plugin, put it in the rehypePlugins
option of your MDX-JS loader.
In this example we also pass the string '!video!' as videoMarker
option.
// Webpack exampleconst rehypeBetterMedia = require('@mdxp/rehypex-plugins/better-media');module.exports = {module: {rules: [{test: /\.mdx$/,use: ['babel-loader',{loader: '@mdx-js/loader',options: {rehypePlugins: [[rehypeBetterMedia, {videoMarker: '!video!'}]]}}]},...]}}
Examples
Once the plugin is setup, you can write the following code:
// Gets transformed by the plugin to:<img alt="logo image" src="./path/to/logo.png" title="An image of my logo" />
// Note that style should be defined as a JSX property (double braces)// Gets transformed by the plugin to:<img alt="image" src="./path/to/image.jpg" style={{border: '5px solid red', width: '80%'}} />
// Gets transformed by the plugin to:<video alt="video" src="./path/to/video.mp4" autoplay loop />
// Gets transformed by the plugin to:<video alt="online video" src="https://www.thiscoolvideo.com" controls />