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

hast-util-properties-to-mdx-jsx-attributes

v1.0.0

Published

Transform hast properties into mdxJsxAttribute nodes

Downloads

42,454

Readme

hast-util-properties-to-mdx-jsx-attributes

github actions codecov npm version npm downloads

Transform hast properties to a list of mdxJsxAttribute nodes.

Table of Contents

Installation

npm install hast-util-properties-to-mdx-jsx-attributes

Usage

propertiesToMdxJsxAttributes takes hast properties, and transform them to a list of mdxJsxAttribute nodes. This is useful when creating an MDX plugin where you want to enhance a hast element with JSX specific features, but you don’t want to transform all child nodes.

For example, this plugin prefixes all id attributes on hast elements with the id prop passed to the MDX document.

import { type Root } from 'hast'
import { propertiesToMdxJsxAttributes } from 'hast-util-properties-to-mdx-jsx-attributes'
import { type Plugin } from 'unified'
import { visit } from 'unist-util-visit'

const rehypeMdxPrefixId: Plugin<[], Root> = () => (ast) => {
  visit(ast, 'element', (element, index, parent) => {
    let hasId = false
    const replacement = {
      type: 'mdxJsxFlowElement',
      name: element.tagName,
      attributes: propertiesToMdxJsxAttributes(element.properties, {
        transform(name, value) {
          hasId = true

          return {
            type: 'BinaryExpression',
            operator: '+',
            left: { type: 'Identifier', name: 'id' },
            right: {
              type: 'MemberExpression',
              computed: false,
              optional: false,
              object: { type: 'Identifier', name: 'props' },
              property: { type: 'Literal', value }
            }
          }
        }
      }),
      children: element.children
    }

    if (hasId) {
      parent!.children[index!] = replacement
    }
  })
}

export default rehypeMdxPrefixId

API

propertiesToMdxJsxAttributes(properties, options?)

Transform hast properties to a list of mdxJsxAttribute nodes.

Arguments

  • properties (Properties) — The hast properties to transform.
  • options (object) — Additional options to pass. The following options are supported:
    • elementAttributeNameCase ('html' | 'react') — The casing to use for attribute names. This should match the elementAttributeNameCase option specified in the MDX options. (Default: 'react')
    • space ('html' | 'svg') — The space the hast properties are in. (Default: 'html')
    • transform (function) — A function to transform an attribute value. The function gets called with the name of the MDX JSX attribute that’s being generated, the value that would be used if no transform function is passed, and the value as it was in the hast properties. It should return an ESTree expression or string to represent the value, or null or undefined to generate an attribute with no value.

Returns

The hast properties as a list of mdxJsxAttribute nodes.

Compatibility

This project is compatible with Node.js 16 or greater.

License

MIT © Remco Haszing