Socket
Socket
Sign inDemoInstall

micromark-extension-mdx-expression

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

micromark-extension-mdx-expression - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

1

dev/index.js
/**
* @typedef {import('./lib/syntax.js').Options} Options
*/
export {mdxExpression} from './lib/syntax.js'

22

dev/lib/syntax.d.ts
/**
* Add support for MDX expressions.
* Create an extension for `micromark` to enable MDX expression syntax.
*
* Function called optionally with options to get a syntax extension for
* micromark.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Extension}
* Syntax extension for micromark (passed in `extensions`).
* Extension for `micromark` that can be passed in `extensions` to enable MDX
* expression syntax.
*/
export function mdxExpression(options?: Options | null | undefined): Extension
export type Acorn = import('micromark-util-events-to-acorn').Acorn
export type AcornOptions = import('micromark-util-events-to-acorn').AcornOptions
export type Extension = import('micromark-util-types').Extension
export type Tokenizer = import('micromark-util-types').Tokenizer
export type State = import('micromark-util-types').State
export type TokenizeContext = import('micromark-util-types').TokenizeContext
export type Acorn = import('micromark-util-events-to-acorn').Acorn
export type AcornOptions = import('micromark-util-events-to-acorn').AcornOptions
export type Tokenizer = import('micromark-util-types').Tokenizer
/**

@@ -28,10 +26,10 @@ * Configuration (optional).

/**
* Options to pass to acorn (default: `{ecmaVersion: 2020, locations: true,
* Configuration for acorn (default: `{ecmaVersion: 2020, locations: true,
* sourceType: 'module'}`).
* All fields (except for `locations`) can be set.
*
* All fields except `locations` can be set.
*/
acornOptions?: AcornOptions | null | undefined
/**
* Whether to add an `estree` field to `mdxFlowExpression` and
* `mdxTextExpression` tokens with results from acorn.
* Whether to add `estree` fields to tokens with results from acorn.
*/

@@ -38,0 +36,0 @@ addResult?: boolean | null | undefined

/**
* @typedef {import('micromark-util-events-to-acorn').Acorn} Acorn
* @typedef {import('micromark-util-events-to-acorn').AcornOptions} AcornOptions
* @typedef {import('micromark-util-types').Extension} Extension
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-events-to-acorn').Acorn} Acorn
* @typedef {import('micromark-util-events-to-acorn').AcornOptions} AcornOptions
*
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*/
/**
* @typedef Options

@@ -14,8 +16,8 @@ * Configuration (optional).

* @property {AcornOptions | null | undefined} [acornOptions]
* Options to pass to acorn (default: `{ecmaVersion: 2020, locations: true,
* Configuration for acorn (default: `{ecmaVersion: 2020, locations: true,
* sourceType: 'module'}`).
* All fields (except for `locations`) can be set.
*
* All fields except `locations` can be set.
* @property {boolean | null | undefined} [addResult=false]
* Whether to add an `estree` field to `mdxFlowExpression` and
* `mdxTextExpression` tokens with results from acorn.
* Whether to add `estree` fields to tokens with results from acorn.
* @property {boolean | null | undefined} [spread=false]

@@ -29,19 +31,17 @@ * Undocumented option to parse only a spread (used by

import {ok as assert} from 'uvu/assert'
import {factoryMdxExpression} from 'micromark-factory-mdx-expression'
import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding} from 'micromark-util-character'
import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
import {codes} from 'micromark-util-symbol/codes.js'
import {types} from 'micromark-util-symbol/types.js'
import {ok as assert} from 'uvu/assert'
/**
* Add support for MDX expressions.
* Create an extension for `micromark` to enable MDX expression syntax.
*
* Function called optionally with options to get a syntax extension for
* micromark.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Extension}
* Syntax extension for micromark (passed in `extensions`).
* Extension for `micromark` that can be passed in `extensions` to enable MDX
* expression syntax.
*/

@@ -89,2 +89,9 @@ export function mdxExpression(options) {

/**
* MDX expression (flow).
*
* ```markdown
* > | {Math.PI}
* ^^^^^^^^^
* ```
*
* @this {TokenizeContext}

@@ -98,9 +105,34 @@ * @type {Tokenizer}

/** @type {State} */
/**
* Start of an MDX expression (flow).
*
* ```markdown
* > | {Math.PI}
* ^
* ```
*
* @type {State}
*/
function start(code) {
// To do: in `markdown-rs`, constructs need to parse the indent themselves.
// This should also be introduced in `micromark-js`.
assert(code === codes.leftCurlyBrace, 'expected `{`')
return before(code)
}
/**
* After optional whitespace, before expression.
*
* ```markdown
* > | {Math.PI}
* ^
* ```
*
* @type {State}
*/
function before(code) {
return factoryMdxExpression.call(
self,
effects,
factorySpace(effects, after, types.whitespace),
after,
'mdxFlowExpression',

@@ -117,4 +149,29 @@ 'mdxFlowExpressionMarker',

/** @type {State} */
/**
* After expression.
*
* ```markdown
* > | {Math.PI}
* ^
* ```
*
* @type {State}
*/
function after(code) {
return markdownSpace(code)
? factorySpace(effects, end, types.whitespace)(code)
: end(code)
}
/**
* After expression, after optional whitespace.
*
* ```markdown
* > | {Math.PI}␠␊
* ^
* ```
*
* @type {State}
*/
function end(code) {
return code === codes.eof || markdownLineEnding(code)

@@ -127,2 +184,9 @@ ? ok(code)

/**
* MDX expression (text).
*
* ```markdown
* > | a {Math.PI} c.
* ^^^^^^^^^
* ```
*
* @this {TokenizeContext}

@@ -136,3 +200,13 @@ * @type {Tokenizer}

/** @type {State} */
/**
* Start of an MDX expression (text).
*
* ```markdown
* > | a {Math.PI} c.
* ^
* ```
*
*
* @type {State}
*/
function start(code) {

@@ -139,0 +213,0 @@ assert(code === codes.leftCurlyBrace, 'expected `{`')

/**
* @typedef {import('./lib/syntax.js').Options} Options
*/
export {mdxExpression} from './lib/syntax.js'
/**
* Add support for MDX expressions.
* Create an extension for `micromark` to enable MDX expression syntax.
*
* Function called optionally with options to get a syntax extension for
* micromark.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Extension}
* Syntax extension for micromark (passed in `extensions`).
* Extension for `micromark` that can be passed in `extensions` to enable MDX
* expression syntax.
*/
export function mdxExpression(options?: Options | null | undefined): Extension
export type Acorn = import('micromark-util-events-to-acorn').Acorn
export type AcornOptions = import('micromark-util-events-to-acorn').AcornOptions
export type Extension = import('micromark-util-types').Extension
export type Tokenizer = import('micromark-util-types').Tokenizer
export type State = import('micromark-util-types').State
export type TokenizeContext = import('micromark-util-types').TokenizeContext
export type Acorn = import('micromark-util-events-to-acorn').Acorn
export type AcornOptions = import('micromark-util-events-to-acorn').AcornOptions
export type Tokenizer = import('micromark-util-types').Tokenizer
/**

@@ -28,10 +26,10 @@ * Configuration (optional).

/**
* Options to pass to acorn (default: `{ecmaVersion: 2020, locations: true,
* Configuration for acorn (default: `{ecmaVersion: 2020, locations: true,
* sourceType: 'module'}`).
* All fields (except for `locations`) can be set.
*
* All fields except `locations` can be set.
*/
acornOptions?: AcornOptions | null | undefined
/**
* Whether to add an `estree` field to `mdxFlowExpression` and
* `mdxTextExpression` tokens with results from acorn.
* Whether to add `estree` fields to tokens with results from acorn.
*/

@@ -38,0 +36,0 @@ addResult?: boolean | null | undefined

/**
* @typedef {import('micromark-util-events-to-acorn').Acorn} Acorn
* @typedef {import('micromark-util-events-to-acorn').AcornOptions} AcornOptions
* @typedef {import('micromark-util-types').Extension} Extension
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
* @typedef {import('micromark-util-types').State} State
* @typedef {import('micromark-util-types').TokenizeContext} TokenizeContext
* @typedef {import('micromark-util-events-to-acorn').Acorn} Acorn
* @typedef {import('micromark-util-events-to-acorn').AcornOptions} AcornOptions
*
* @typedef {import('micromark-util-types').Tokenizer} Tokenizer
*/
/**
* @typedef Options

@@ -14,8 +16,8 @@ * Configuration (optional).

* @property {AcornOptions | null | undefined} [acornOptions]
* Options to pass to acorn (default: `{ecmaVersion: 2020, locations: true,
* Configuration for acorn (default: `{ecmaVersion: 2020, locations: true,
* sourceType: 'module'}`).
* All fields (except for `locations`) can be set.
*
* All fields except `locations` can be set.
* @property {boolean | null | undefined} [addResult=false]
* Whether to add an `estree` field to `mdxFlowExpression` and
* `mdxTextExpression` tokens with results from acorn.
* Whether to add `estree` fields to tokens with results from acorn.
* @property {boolean | null | undefined} [spread=false]

@@ -31,13 +33,11 @@ * Undocumented option to parse only a spread (used by

import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding} from 'micromark-util-character'
import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
/**
* Add support for MDX expressions.
* Create an extension for `micromark` to enable MDX expression syntax.
*
* Function called optionally with options to get a syntax extension for
* micromark.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Extension}
* Syntax extension for micromark (passed in `extensions`).
* Extension for `micromark` that can be passed in `extensions` to enable MDX
* expression syntax.
*/

@@ -91,2 +91,9 @@ export function mdxExpression(options) {

/**
* MDX expression (flow).
*
* ```markdown
* > | {Math.PI}
* ^^^^^^^^^
* ```
*
* @this {TokenizeContext}

@@ -99,8 +106,34 @@ * @type {Tokenizer}

/** @type {State} */
/**
* Start of an MDX expression (flow).
*
* ```markdown
* > | {Math.PI}
* ^
* ```
*
* @type {State}
*/
function start(code) {
// To do: in `markdown-rs`, constructs need to parse the indent themselves.
// This should also be introduced in `micromark-js`.
return before(code)
}
/**
* After optional whitespace, before expression.
*
* ```markdown
* > | {Math.PI}
* ^
* ```
*
* @type {State}
*/
function before(code) {
return factoryMdxExpression.call(
self,
effects,
factorySpace(effects, after, 'whitespace'),
after,
'mdxFlowExpression',

@@ -117,4 +150,29 @@ 'mdxFlowExpressionMarker',

/** @type {State} */
/**
* After expression.
*
* ```markdown
* > | {Math.PI}
* ^
* ```
*
* @type {State}
*/
function after(code) {
return markdownSpace(code)
? factorySpace(effects, end, 'whitespace')(code)
: end(code)
}
/**
* After expression, after optional whitespace.
*
* ```markdown
* > | {Math.PI}␠␊
* ^
* ```
*
* @type {State}
*/
function end(code) {
return code === null || markdownLineEnding(code) ? ok(code) : nok(code)

@@ -125,2 +183,9 @@ }

/**
* MDX expression (text).
*
* ```markdown
* > | a {Math.PI} c.
* ^^^^^^^^^
* ```
*
* @this {TokenizeContext}

@@ -133,3 +198,13 @@ * @type {Tokenizer}

/** @type {State} */
/**
* Start of an MDX expression (text).
*
* ```markdown
* > | a {Math.PI} c.
* ^
* ```
*
*
* @type {State}
*/
function start(code) {

@@ -136,0 +211,0 @@ return factoryMdxExpression.call(

{
"name": "micromark-extension-mdx-expression",
"version": "1.0.4",
"version": "1.0.5",
"description": "micromark extension to support MDX or MDX JS expressions",

@@ -59,3 +59,3 @@ "license": "MIT",

"scripts": {
"build": "tsc --build --clean && tsc && type-coverage && micromark-build"
"build": "micromark-build"
},

@@ -62,0 +62,0 @@ "xo": false,

@@ -11,3 +11,3 @@ # micromark-extension-mdx-expression

[micromark][] extension to support MDX expressions (`{2 * Math.PI}`).
[micromark][] extension to support [MDX][mdxjs] expressions (`{Math.PI}`).

@@ -22,2 +22,3 @@ ## Contents

* [`mdxExpression(options?)`](#mdxexpressionoptions)
* [Options](#options)
* [Authoring](#authoring)

@@ -39,33 +40,29 @@ * [Syntax](#syntax)

This project contains three packages (it’s a monorepo).
This package contains an extension that adds support for the expression syntax
enabled by [MDX][mdxjs] to [`micromark`][micromark].
These extensions are used inside MDX.
* `micromark-extension-mdx-expression`
— extension that adds support for expressions enabled by MDX to
[`micromark`][micromark]
* `micromark-factory-mdx-expression`
— subroutine to parse the expressions
* `micromark-util-events-to-acorn`
— subroutine to turn parse micromark events with acorn
This package can be made aware or unaware of JavaScript syntax.
When unaware, expressions could include Rust or variables or whatnot.
This readme will focus on `micromark-extension-mdx-expression`.
## When to use this
These tools are all low-level.
In many cases, you want to use [`remark-mdx`][remark-mdx] with remark instead.
When you are using [`mdx-js/mdx`][mdxjs], that is already included.
This project is useful when you want to support expressions in markdown.
Even when you want to use `micromark`, you likely want to use
[`micromark-extension-mdxjs`][micromark-extension-mdxjs] to support all MDX
features.
That extension includes this extension.
You can use this extension when you are working with [`micromark`][micromark].
To support all MDX features, use
[`micromark-extension-mdxjs`][micromark-extension-mdxjs] instead.
When working with [`mdast-util-from-markdown`][mdast-util-from-markdown], you
must combine this package with
When you need a syntax tree, combine this package with
[`mdast-util-mdx-expression`][mdast-util-mdx-expression].
All these packages are used in [`remark-mdx`][remark-mdx], which focusses on
making it easier to transform content by abstracting these internals away.
When you are using [`mdx-js/mdx`][mdxjs], all of this is already included.
## Install
This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with [npm][]:
In Node.js (version 16+), install with [npm][]:

@@ -93,7 +90,8 @@ ```sh

```js
import * as acorn from 'acorn'
import {Parser} from 'acorn'
import acornJsx from 'acorn-jsx'
import {micromark} from 'micromark'
import {mdxExpression} from 'micromark-extension-mdx-expression'
// Agnostic (balanced braces):
// Unaware of JavaScript (“agnostic”) (balanced braces):
const output = micromark('a {1 + 1} b', {extensions: [mdxExpression()]})

@@ -103,4 +101,4 @@

// Gnostic (JavaScript):
micromark('a {!} b', {extensions: [mdxExpression({acorn})]})
// Aware of JavaScript:
micromark('a {!} b', {extensions: [mdxExpression({acorn: Parser.extend(acornJsx())})]})
```

@@ -134,6 +132,6 @@

This package exports the identifier `mdxExpression`.
This package exports the identifier [`mdxExpression`][api-mdx-expression].
There is no default export.
The export map supports the endorsed [`development` condition][condition].
The export map supports the [`development` condition][development].
Run `node --conditions development module.js` to get instrumented dev code.

@@ -144,28 +142,29 @@ Without this condition, production code is loaded.

Add support for MDX expressions.
Create an extension for `micromark` to enable MDX expression syntax.
Function called optionally with options to get a syntax extension for micromark
(passed in `extensions`).
###### Parameters
##### `options`
* `options` ([`Options`][api-options], optional)
— configuration
Configuration (optional).
###### Returns
###### `options.acorn`
Extension for `micromark` that can be passed in `extensions` to enable MDX
expression syntax ([`Extension`][micromark-extension]).
Acorn parser to use ([`Acorn`][acorn], optional).
### Options
###### `options.acornOptions`
Configuration (TypeScript type).
Options to pass to acorn (`Object`, default: `{ecmaVersion: 2020, locations:
true, sourceType: 'module'}`).
All fields can be set.
Positional info (`loc`, `range`) is set on ES nodes regardless of acorn options.
###### Fields
###### `options.addResult`
* `acorn` ([`Acorn`][acorn], optional)
— acorn parser to use
* `acornOptions` ([`AcornOptions`][acorn-options], default:
`{ecmaVersion: 2020, 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
Whether to add an `estree` field to `mdxFlowExpression` and `mdxTextExpression`
tokens with the results from acorn (`boolean`, default: `false`).
Note that expressions can be empty or be just comments, in which case `estree`
will be undefined.
<!-- Note: `spread` and `allowEmpty` are intentionally not documented. -->

@@ -178,10 +177,11 @@ ## Authoring

This affects how markdown and JavaScript interleave with eachother in MDX.
For more info on how it works, see [§ Interleaving][interleaving] on the MDX
site.
For more info on how it works, see [§ Interleaving][mdxjs-interleaving] on the
MDX site.
## Syntax
This extension supports MDX both agnostic and gnostic to JavaScript.
This extension supports MDX 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 could (such as Rust or so) can be used.
expressions, or arbitrary text (such as Rust code or so) can be used.

@@ -292,23 +292,25 @@ There are two types of expressions: in text (inline, span) or in flow (block).

This package is fully typed with [TypeScript][].
It exports the additional type `Options`.
It exports the additional type [`Options`][api-options].
## Compatibility
This package is at least compatible with all maintained versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
It also works in Deno and modern browsers.
Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 16+.
Our projects sometimes work with older versions, but this is not guaranteed.
These extensions work with `micromark` version 3+.
## Security
This package deals with compiling JavaScript.
If you do not trust the JavaScript, this package does nothing to change that.
This package is safe.
## Related
* [`micromark/micromark-extension-mdxjs`][micromark-extension-mdxjs]
— micromark extension to support MDX
* [`syntax-tree/mdast-util-mdx-expression`][mdast-util-mdx-expression]
— mdast utility to support MDX expressions
* [`micromark-extension-mdxjs`][micromark-extension-mdxjs]
— support all MDX syntax
* [`mdast-util-mdx-expression`][mdast-util-mdx-expression]
— support MDX expressions in mdast
* [`remark-mdx`][remark-mdx]
— remark plugin to support MDX syntax
— support all MDX syntax in remark

@@ -365,7 +367,7 @@ ## Contribute

[contributing]: https://github.com/micromark/.github/blob/HEAD/contributing.md
[contributing]: https://github.com/micromark/.github/blob/main/contributing.md
[support]: https://github.com/micromark/.github/blob/HEAD/support.md
[support]: https://github.com/micromark/.github/blob/main/support.md
[coc]: https://github.com/micromark/.github/blob/HEAD/code-of-conduct.md
[coc]: https://github.com/micromark/.github/blob/main/code-of-conduct.md

@@ -376,6 +378,8 @@ [esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

[condition]: https://nodejs.org/api/packages.html#packages_resolving_user_conditions
[development]: https://nodejs.org/api/packages.html#packages_resolving_user_conditions
[micromark]: https://github.com/micromark/micromark
[micromark-extension]: https://github.com/micromark/micromark#syntaxextension
[micromark-extension-mdxjs]: https://github.com/micromark/micromark-extension-mdxjs

@@ -391,4 +395,10 @@

[interleaving]: https://mdxjs.com/docs/what-is-mdx/#interleaving
[mdxjs-interleaving]: https://mdxjs.com/docs/what-is-mdx/#interleaving
[acorn]: https://github.com/acornjs/acorn
[acorn-options]: https://github.com/acornjs/acorn/blob/96c721dbf89d0ccc3a8c7f39e69ef2a6a3c04dfa/acorn/dist/acorn.d.ts#L16
[api-mdx-expression]: #mdxexpressionoptions
[api-options]: #options
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