Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

Transform hast properties into mdxJsxAttribute nodes

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

Keywords

FAQs

Package last updated on 22 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc