mdast-util-mdx-expression
Advanced tools
Comparing version 0.1.1 to 1.0.0
135
index.js
@@ -1,2 +0,133 @@ | ||
exports.fromMarkdown = require('./from-markdown') | ||
exports.toMarkdown = require('./to-markdown') | ||
/** | ||
* @typedef {import('mdast').Literal} Literal | ||
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension | ||
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle | ||
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension | ||
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle | ||
* @typedef {import('estree-jsx').Program} Estree | ||
* | ||
* @typedef {Literal & {type: 'mdxFlowExpression', data: {estree?: Estree}}} MDXFlowExpression | ||
* @typedef {Literal & {type: 'mdxSpanExpression', data: {estree?: Estree}}} MDXSpanExpression | ||
*/ | ||
import stripIndent from 'strip-indent' | ||
const eol = /\r?\n|\r/g | ||
/** @type {FromMarkdownExtension} */ | ||
export const mdxExpressionFromMarkdown = { | ||
enter: { | ||
mdxFlowExpression: enterMdxFlowExpression, | ||
mdxTextExpression: enterMdxTextExpression | ||
}, | ||
exit: { | ||
mdxFlowExpression: exitMdxExpression, | ||
mdxFlowExpressionChunk: exitMdxExpressionData, | ||
mdxTextExpression: exitMdxExpression, | ||
mdxTextExpressionChunk: exitMdxExpressionData | ||
} | ||
} | ||
/** @type {ToMarkdownExtension} */ | ||
export const mdxExpressionToMarkdown = { | ||
handlers: { | ||
mdxFlowExpression: handleMdxExpression, | ||
mdxTextExpression: handleMdxExpression | ||
}, | ||
unsafe: [ | ||
{character: '{', inConstruct: ['phrasing']}, | ||
{atBreak: true, character: '{'} | ||
] | ||
} | ||
/** @type {FromMarkdownHandle} */ | ||
function enterMdxFlowExpression(token) { | ||
// @ts-expect-error: fine. | ||
this.enter({type: 'mdxFlowExpression', value: ''}, token) | ||
this.buffer() | ||
} | ||
/** @type {FromMarkdownHandle} */ | ||
function enterMdxTextExpression(token) { | ||
// @ts-expect-error: fine. | ||
this.enter({type: 'mdxTextExpression', value: ''}, token) | ||
this.buffer() | ||
} | ||
/** @type {FromMarkdownHandle} */ | ||
function exitMdxExpression(token) { | ||
const value = this.resume() | ||
const node = this.exit(token) | ||
node.value = token.type === 'mdxFlowExpression' ? dedent(value) : value | ||
// @ts-expect-error: estree. | ||
if (token.estree) { | ||
// @ts-expect-error: estree. | ||
node.data = {estree: token.estree} | ||
} | ||
} | ||
/** @type {FromMarkdownHandle} */ | ||
function exitMdxExpressionData(token) { | ||
this.config.enter.data.call(this, token) | ||
this.config.exit.data.call(this, token) | ||
} | ||
/** | ||
* @type {ToMarkdownHandle} | ||
* @param {MDXFlowExpression|MDXSpanExpression} node | ||
*/ | ||
function handleMdxExpression(node) { | ||
const value = node.value || '' | ||
return '{' + (node.type === 'mdxFlowExpression' ? indent(value) : value) + '}' | ||
} | ||
/** | ||
* @param {string} value | ||
* @returns {string} | ||
*/ | ||
function dedent(value) { | ||
const firstLineEnding = /\r?\n|\r/.exec(value) | ||
const position = firstLineEnding | ||
? firstLineEnding.index + firstLineEnding[0].length | ||
: -1 | ||
if (position > -1) { | ||
return value.slice(0, position) + stripIndent(value.slice(position)) | ||
} | ||
return value | ||
} | ||
/** | ||
* @param {string} value | ||
* @returns {string} | ||
*/ | ||
function indent(value) { | ||
/** @type {Array.<string>} */ | ||
const result = [] | ||
let start = 0 | ||
let line = 0 | ||
/** @type {RegExpExecArray|null} */ | ||
let match | ||
while ((match = eol.exec(value))) { | ||
one(value.slice(start, match.index)) | ||
result.push(match[0]) | ||
start = match.index + match[0].length | ||
line++ | ||
} | ||
one(value.slice(start)) | ||
return result.join('') | ||
/** | ||
* @param {string} slice | ||
* @returns {void} | ||
*/ | ||
function one(slice) { | ||
result.push((line && slice ? ' ' : '') + slice) | ||
} | ||
} |
{ | ||
"name": "mdast-util-mdx-expression", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"description": "mdast extension to parse and serialize MDX (or MDX.js) expressions", | ||
@@ -29,35 +29,39 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"from-markdown.js", | ||
"index.js", | ||
"to-markdown.js" | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": { | ||
"strip-indent": "^3.0.0" | ||
"@types/estree-jsx": "^0.0.1", | ||
"strip-indent": "^4.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/acorn": "^4.0.5", | ||
"@types/tape": "^4.0.0", | ||
"acorn": "^8.0.0", | ||
"mdast-util-from-markdown": "^0.8.0", | ||
"mdast-util-to-markdown": "^0.5.0", | ||
"micromark-extension-mdx-expression": "^0.1.0", | ||
"nyc": "^15.0.0", | ||
"c8": "^7.0.0", | ||
"mdast-util-from-markdown": "^1.0.0", | ||
"mdast-util-to-markdown": "^1.0.0", | ||
"micromark-extension-mdx-expression": "^1.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"unist-util-remove-position": "^3.0.0", | ||
"xo": "^0.36.0" | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"unist-util-remove-position": "^4.0.0", | ||
"xo": "^0.39.0" | ||
}, | ||
"scripts": { | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run format && npm run test-coverage" | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"prettier": { | ||
@@ -72,4 +76,3 @@ "tabWidth": 2, | ||
"xo": { | ||
"prettier": true, | ||
"esnext": false | ||
"prettier": true | ||
}, | ||
@@ -80,3 +83,9 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true, | ||
"ignoreCatch": true | ||
} | ||
} |
@@ -19,9 +19,12 @@ # mdast-util-mdx-expression | ||
You probably should use either [`micromark-extension-mdx`][mdx] or | ||
[`micromark-extension-mdxjs`][mdxjs] with [`mdast-util-mdx`][mdast-util-mdx] | ||
(which both include this package) to support all of MDX (or MDX.js). | ||
Or use it all through `remark-mdx` or `remark-mdxjs` (**[remark][]**). | ||
## When to use this | ||
Use [`mdast-util-mdx`][mdast-util-mdx] if you want all of MDX / MDX.js. | ||
Use this otherwise. | ||
## Install | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
[npm][]: | ||
@@ -45,11 +48,11 @@ | ||
And our script, `example.js`, looks as follows: | ||
And our module, `example.js`, looks as follows: | ||
```js | ||
var fs = require('fs') | ||
var acorn = require('acorn') | ||
var syntax = require('micromark-extension-mdx-expression') | ||
var fromMarkdown = require('mdast-util-from-markdown') | ||
var toMarkdown = require('mdast-util-to-markdown') | ||
var mdxExpression = require('mdast-util-mdx-expression') | ||
import fs from 'node:fs' | ||
import * as acorn from 'acorn' | ||
import {fromMarkdown} from 'mdast-util-from-markdown' | ||
import {toMarkdown} from 'mdast-util-to-markdown' | ||
import {mdxExpression} from 'micromark-extension-mdx-expression' | ||
import {mdxExpressionFromMarkdown, mdxExpressionToMarkdown} from 'mdast-util-mdx-expression' | ||
@@ -59,4 +62,4 @@ var doc = fs.readFileSync('example.mdx') | ||
var tree = fromMarkdown(doc, { | ||
extensions: [syntax({acorn: acorn, addResult: true})], | ||
mdastExtensions: [mdxExpression.fromMarkdown] | ||
extensions: [mdxExpression({acorn, addResult: true})], | ||
mdastExtensions: [mdxExpressionFromMarkdown] | ||
}) | ||
@@ -66,3 +69,3 @@ | ||
var out = toMarkdown(tree, {extensions: [mdxExpression.toMarkdown]}) | ||
var out = toMarkdown(tree, {extensions: [mdxExpressionToMarkdown]}) | ||
@@ -83,6 +86,15 @@ console.log(out) | ||
estree: { | ||
type: 'BinaryExpression', | ||
left: {type: 'Identifier', name: 'a'}, | ||
operator: '+', | ||
right: {type: 'Literal', value: 1} | ||
type: 'Program', | ||
body: [ | ||
{ | ||
type: 'ExpressionStatement', | ||
expression: { | ||
type: 'BinaryExpression', | ||
left: {type: 'Identifier', name: 'a'}, | ||
operator: '+', | ||
right: {type: 'Literal', value: 1, raw: '1'} | ||
} | ||
} | ||
], | ||
sourceType: 'module' | ||
} | ||
@@ -99,3 +111,12 @@ } | ||
data: { | ||
estree: {type: 'Literal', value: true} | ||
estree: { | ||
type: 'Program', | ||
body: [ | ||
{ | ||
type: 'ExpressionStatement', | ||
expression: {type: 'Literal', value: true, raw: 'true'} | ||
} | ||
], | ||
sourceType: 'module' | ||
} | ||
} | ||
@@ -120,10 +141,6 @@ }, | ||
### `mdxExpression.fromMarkdown` | ||
### `mdxExpressionFromMarkdown` | ||
### `mdxExpression.toMarkdown` | ||
### `mdxExpressionToMarkdown` | ||
> Note: the separate extensions are also available at | ||
> `mdast-util-mdx-expression/from-markdown` and | ||
> `mdast-util-mdx-expression/to-markdown`. | ||
Support MDX (or MDX.js) expressions. | ||
@@ -294,8 +311,4 @@ The exports are extensions, respectively for | ||
[extension]: https://github.com/micromark/micromark-extension-mdxjs-esm | ||
[extension]: https://github.com/micromark/micromark-extension-mdx-expression | ||
[mdx]: https://github.com/micromark/micromark-extension-mdx | ||
[mdxjs]: https://github.com/micromark/micromark-extension-mdxjs | ||
[mdast-util-mdx]: https://github.com/syntax-tree/mdast-util-mdx | ||
@@ -302,0 +315,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15443
136
1
315
Yes
2
16
5
1
+ Added@types/estree-jsx@^0.0.1
+ Added@types/estree@1.0.6(transitive)
+ Added@types/estree-jsx@0.0.1(transitive)
+ Addedstrip-indent@4.0.0(transitive)
- Removedstrip-indent@3.0.0(transitive)
Updatedstrip-indent@^4.0.0