npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rehype-mdx-import-media

v1.2.0

Published

An MDX rehype plugin for turning media paths into imports.

Downloads

11,783

Readme

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