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

@hellomouse/micromark-extension-mdx-jsx

v0.1.1

Published

micromark extension to support MDX or MDX.js JSX

Downloads

5

Readme

Modified MDX

This is modified version of micromark-extension-mdx-jsx with a few changes.

import { mdxJsx, mdastExtraJsxFlow } from 'micromark-extension-mdx-jsx';
import { mdxExpressionFromMarkdown } from 'mdast-util-mdx-expression';

import { fromMarkdown } from 'mdast-util-from-markdown';
import { mdxJsxFromMarkdown } from 'mdast-util-mdx-jsx';
import { mdxExpressionFromMarkdown } from 'mdast-util-mdx-expression';

export let options: any = {
  acorn: Parser.extend(acornJsx()),
  acornOptions: { ecmaVersion: 2024, sourceType: 'module' },
  addResult: true,
};

export default function parse(input: string): MarkdownRoot {
  return fromMarkdown(input, {
    extensions: [
      mdxJsx(options),
      mdxExpression(options),
    ],
    mdastExtensions: [
      mdxJsxFromMarkdown(),
      // note: this extension is required or newlines may not render properly
      mdastExtraJsxFlow,
      mdxExpressionFromMarkdown(),
    ],
  });
}

What is changed?

Flow-level JSX no longer wraps its children in paragraphs. Inline-level markdown is still allowed within flow JSX. To use paragraph wrapping, simply leave a blank line after a flow JSX element, as in CommonMark.

<span class="test">
  **Test:** A potato.
</span>

... becomes ...

<span class="test">
  <strong>Test:</strong> A potato.
</span>
This does not work:

<div>
  - item 1
    test
  - item 2
</div>

Add a blank line instead:

<div>

- item 1
  test
- item 2

</div>

... becomes ...

<div>
  <ul>
    <li>
      item 1
      test
    </li>
    <li>item 2</li>
  </ul>
</div>

The original readme is reproduced below.

micromark-extension-mdx-jsx

Build Coverage Downloads Size Sponsors Backers Chat

micromark extension to support MDX JSX (<Component />).

Contents

What is this?

This package contains an extension that adds support for the JSX syntax enabled by MDX to micromark. These extensions are used inside MDX. It mostly matches how JSX works in most places that support it (TypeScript, Babel, esbuild, SWC, etc).

This package can be made aware or unaware of JavaScript syntax. When unaware, expressions could include Rust or variables or whatnot.

When to use this

This project is useful when you want to support JSX in markdown.

You can use this extension when you are working with micromark. To support all MDX features, use micromark-extension-mdxjs instead.

When you need a syntax tree, combine this package with mdast-util-mdx-jsx.

All these packages are used in remark-mdx, which focusses on making it easier to transform content by abstracting these internals away.

When you are using mdx-js/mdx, all of this is already included.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install micromark-extension-mdx-jsx

In Deno with esm.sh:

import {mdxJsx} from 'https://esm.sh/micromark-extension-mdx-jsx@2'

In browsers with esm.sh:

<script type="module">
  import {mdxJsx} from 'https://esm.sh/micromark-extension-mdx-jsx@2?bundle'
</script>

Use

import {micromark} from 'micromark'
import {mdxJsx} from 'micromark-extension-mdx-jsx'

const output = micromark('a <b c d="e" /> f', {extensions: [mdxJsx()]})

console.log(output)

Yields:

<p>a  f</p>

…which is useless: go to a syntax tree with mdast-util-from-markdown and mdast-util-mdx-jsx instead.

API

This package exports the identifier mdxJsx. There is no default export.

The export map supports the development condition. Run node --conditions development module.js to get instrumented dev code. Without this condition, production code is loaded.

mdxJsx(options?)

Create an extension for micromark to enable MDX JSX syntax.

Parameters
  • options (Options, optional) — configuration
Returns

Extension for micromark that can be passed in extensions to enable MDX JSX syntax (Extension).

Options

Configuration (TypeScript type).

Fields
  • acorn (Acorn, optional) — acorn parser to use
  • acornOptions (AcornOptions, default: {ecmaVersion: 2024, locations: true, sourceType: 'module'}) — configuration for acorn; all fields except locations can be set
  • addResult (boolean, default: false) — whether to add estree fields to tokens with results from acorn

Authoring

When authoring markdown with JSX, keep in mind that MDX is a whitespace sensitive and line-based language, while JavaScript is insensitive to whitespace. This affects how markdown and JSX interleave with eachother in MDX. For more info on how it works, see § Interleaving on the MDX site.

Comments inside tags

JavaScript comments in JSX are not supported.

Incorrect:

<hi/*comment!*//>
<hello// comment!
/>

Correct:

<hi/>
<hello
/>

A PR that adds support for them would be accepted.

Element or fragment attribute values

JSX elements or JSX fragments as attribute values are not supported. The reason for this change is that it would be confusing whether markdown would work.

Incorrect:

<welcome name=<>Venus</> />
<welcome name=<span>Pluto</span> />

Correct:

<welcome name='Mars' />
<welcome name={<span>Jupiter</span>} />
Greater than (>) and right curly brace (})

JSX does not allow U+003E GREATER THAN (>) or U+007D RIGHT CURLY BRACE (}) literally in text, they need to be encoded as character references (or expressions). There is no good reason for this (some JSX parsers agree with us and don’t crash either). Therefore, in MDX, U+003E GREATER THAN (>) and U+007D RIGHT CURLY BRACE (}) are fine literally and don’t need to be encoded.

Syntax

JSX forms with the following BNF:

As the flow construct occurs in flow, like all flow constructs, it must be followed by an eol (line ending) or eof (end of file).

The grammar for JSX in markdown is much stricter than that of HTML in markdown. The primary benefit of this is that tags are parsed into tokens, and thus can be processed. Another, arguable, benefit of this is that it comes with syntax errors: if an author types something that is nonsensical, an error is thrown with information about where it happened, what occurred, and what was expected instead.

This extension supports expressions both aware and unaware to JavaScript (respectively gnostic and agnostic). Depending on whether acorn is passed, either valid JavaScript must be used in expressions, or arbitrary text (such as Rust code or so) can be used.

More on this can be found in § Syntax of micromark-extension-mdx-expression.

Errors

In aware (gnostic) mode, expressions are parsed with micromark-extension-mdx-expression, which throws some more errors.

Unexpected end of file $at, expected $expect

This error occurs for many different reasons if something was opened but not closed (source: micromark-extension-mdx-jsx, rule id: unexpected-eof).

Some examples are:

<
</
<a
<a:
<a.
<a b
<a b:
<a b=
<a b="
<a b='
<a b={
<a/

Unexpected character $at, expected $expect

This error occurs for many different reasons if an unexpected character is seen (source: micromark-extension-mdx-jsx, rule id: unexpected-character).

Some examples are:

<.>
</.>
<a?>
<a:+>
<a./>
<a b!>
<a b:1>
<a b=>
<a/->

Unexpected lazy line in container, expected line to be…

This error occurs if a < was seen in a container which then has lazy content (source: micromark-extension-mdx-jsx, rule id: unexpected-lazy). For example:

> <a
b>

Tokens

Many tokens are used:

  • mdxJsxFlowTag for the whole JSX tag (<a>)
  • mdxJsxTextTag ^
  • mdxJsxFlowTagMarker for the tag markers (<, >)
  • mdxJsxTextTagMarker ^
  • mdxJsxFlowTagClosingMarker for the / marking a closing tag (</a>)
  • mdxJsxTextTagClosingMarker ^
  • mdxJsxFlowTagSelfClosingMarker for the / marking a self-closing tag (<a/>)
  • mdxJsxTextTagSelfClosingMarker ^
  • mdxJsxFlowTagName for the whole tag name (a:b in <a:b>)
  • mdxJsxTextTagName ^
  • mdxJsxFlowTagNamePrimary for the first name (a in <a:b>)
  • mdxJsxTextTagNamePrimary ^
  • mdxJsxFlowTagNameMemberMarker for the . marking in members (<a.b>)
  • mdxJsxTextTagNameMemberMarker ^
  • mdxJsxFlowTagNameMember for member names (b in <a:b>)
  • mdxJsxTextTagNameMember ^
  • mdxJsxFlowTagNamePrefixMarker for the : between primary and local (<a:b>)
  • mdxJsxTextTagNamePrefixMarker ^
  • mdxJsxFlowTagNameLocal for the local name (b in <a:b>)
  • mdxJsxTextTagNameLocal ^
  • mdxJsxFlowTagExpressionAttribute for whole expression attributes (<a {...b}>)
  • mdxJsxTextTagExpressionAttribute ^
  • mdxJsxFlowTagExpressionAttributeMarker for {, } in expression attributes
  • mdxJsxTextTagExpressionAttributeMarker ^
  • mdxJsxFlowTagExpressionAttributeValue for chunks of what’s inside expression attributes
  • mdxJsxTextTagExpressionAttributeValue ^
  • mdxJsxFlowTagAttribute for a whole normal attribute (<a b>)
  • mdxJsxTextTagAttribute ^
  • mdxJsxFlowTagAttributeName for the whole name of an attribute (b:c in <a b:c>)
  • mdxJsxTextTagAttributeName ^
  • mdxJsxFlowTagAttributeNamePrimary for the first name of an attribute (b in <a b:c>)
  • mdxJsxTextTagAttributeNamePrimary ^
  • mdxJsxFlowTagAttributeNamePrefixMarker for the : between primary and local (<a b:c>)
  • mdxJsxTextTagAttributeNamePrefixMarker ^
  • mdxJsxFlowTagAttributeNameLocal for the local name of an attribute (c in <a b:c>)
  • mdxJsxTextTagAttributeNameLocal ^
  • mdxJsxFlowTagAttributeInitializerMarker for the = between an attribute name and value
  • mdxJsxTextTagAttributeInitializerMarker ^
  • mdxJsxFlowTagAttributeValueLiteral for a string attribute value (<a b="">)
  • mdxJsxTextTagAttributeValueLiteral ^
  • mdxJsxFlowTagAttributeValueLiteralMarker for the quotes around a string attribute value (" or ')
  • mdxJsxTextTagAttributeValueLiteralMarker ^
  • mdxJsxFlowTagAttributeValueLiteralValue for chunks of what’s inside string attribute values
  • mdxJsxTextTagAttributeValueLiteralValue ^
  • mdxJsxFlowTagAttributeValueExpression for an expression attribute value (<a b={1}>)
  • mdxJsxTextTagAttributeValueExpression ^
  • mdxJsxFlowTagAttributeValueExpressionMarker for the { and } of expression attribute values
  • mdxJsxTextTagAttributeValueExpressionMarker ^
  • mdxJsxFlowTagAttributeValueExpressionValue for chunks of what’s inside expression attribute values
  • mdxJsxTextTagAttributeValueExpressionValue ^

Types

This package is fully typed with TypeScript. It exports the additional type Options.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, micromark-extension-mdx-jsx@^2, compatible with Node.js 16.

This package works with micromark version 3 and later.

Security

This package is safe.

Related

Contribute

See contributing.md in micromark/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer