Giter Site home page Giter Site logo

rehype-mdx-import-media's Introduction

rehype-mdx-import-media

github actions codecov npm version npm downloads

An MDX rehype plugin for turning media paths into imports.

Table of Contents

Installation

npm install rehype-mdx-import-media

When should I use this?

You may want to author images in MDX using the markdown format, like so:

![alt](./image.png 'title')

You may use MDX with a bundler such as Webpack or Vite. By default bundlers don’t understand how to resolve those images. They only understand how to resolve imports. This plugin solves that problem.

Also you may use MDX to load markdown files. If you reference other media in those markdown files using HTML tags, that media can be resolved by this plugin too.

Usage

This plugin takes HTML elements that refer to media content, and turns them into MDX expressions that use imports. This allows bundlers to resolve media you referenced from your code. Note that JSX elements are not HTML elements, so they are not processed. HTML elements can come from:

  • Markdown syntax in MDX files, such as images.
  • HTML in files parsed using the md format when using rehype-raw
  • Custom remark / rehype plugins.

If this plugin finds an attribute to process, it transforms the hast element nodes into an mdxJsxTextElement node. This may prevent other rehype plugins from further processing. To avoid this, put rehype-mdx-import-media after any other rehype plugins

Example

Let’s say we have a file named example.mdx with the following contents:

![](./image.png)

The following script:

import { compile } from '@mdx-js/mdx'
import rehypeMdxImportMedia from 'rehype-mdx-import-media'
import { read } from 'to-vfile'

const { value } = await compile(await read('example.mdx'), {
  jsx: true,
  rehypePlugins: [rehypeMdxImportMedia]
})
console.log(value)

Roughly yields:

import _rehypeMdxImportMedia0 from './image.png'

export default function MDXContent() {
  return (
    <p>
      <img alt="" src={_rehypeMdxImportMedia0} />
    </p>
  )
}

API

The default export is a rehype plugin.

Options

  • attributes (object): HTML element attributes that should be processed. The key is the HTML element tag name. The value is a list of attribute names to process. The default attributes are:
  • elementAttributeNameCase ('html' | 'react'): The casing to use for attribute names. This should match the elementAttributeNameCase value passed to MDX. (Default: 'react')
  • preserveHash ('both' | 'import' | 'jsx' | 'none'): Where to keep URL hash. (Default: 'import')
    • both: Keep the URL hash on both the import source and the JSX prop.
    • import: Only keep the URL hash on the import source.
    • jsx: Only keep the URL hash on the JSX prop.
    • none: Remove the URL hash.
  • preserveQuery ('both' | 'import' | 'jsx' | 'none'): Where to keep query parameters. (Default: 'import')
    • both: Keep the query parameters on both the import source and the JSX prop.
    • import: Only keep the query parameters on the import source.
    • jsx: Only keep the query parameters on the JSX prop.
    • none: Remove the query parameters.
  • resolve (boolean): By default imports are resolved relative to the markdown file. This matches behaviour of places that render the markdown, such as GitHub. If this is set to false, this behaviour is removed and URLs are no longer processed. This allows to import images from node_modules. If this is disabled, local images can still be imported by prepending the path with ./.. (Default: true).

Compatibility

This project is compatible with MDX 3 and Node.js 18 or greater.

License

MIT © Remco Haszing

rehype-mdx-import-media's People

Contributors

remcohaszing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rehype-mdx-import-media's Issues

import losing search params

I am trying to use this in NextJS app router with mdx files. I have the img tags being replaced by a custom component that reads the search param from the path and uses it to size the image. However, when I add this plugin, while the local image load works nicely, I lose the query param. Can it be preserved in the path, or moved to a different attribute?

Example MDX:

![alt text](images/IMG_1641-2.jpg?w=full)
![alt text](images/IMG_1643.jpg?w=400&h=300)

If there is a way to add more attributes, that is not clear from the documentation. Any help appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.