diff --git a/.gitignore b/.gitignore index 460653c..1f8061a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules/ *.d.ts .DS_Store yarn.lock +!/index.d.ts diff --git a/.npmrc b/.npmrc index 9951b11..3757b30 100644 --- a/.npmrc +++ b/.npmrc @@ -1,2 +1,2 @@ -package-lock=false ignore-scripts=true +package-lock=false diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..2efcc3c --- /dev/null +++ b/index.d.ts @@ -0,0 +1,15 @@ +export type { + MdxFlowExpression, + MdxTextExpression +} from 'mdast-util-mdx-expression' +export type { + MdxJsxAttribute, + MdxJsxAttributeValueExpression, + MdxJsxExpressionAttribute, + MdxJsxFlowElement, + MdxJsxTextElement, + ToMarkdownOptions +} from 'mdast-util-mdx-jsx' +export type {MdxjsEsm} from 'mdast-util-mdxjs-esm' + +export {mdxFromMarkdown, mdxToMarkdown} from './lib/index.js' diff --git a/index.js b/index.js index 410c701..9e80b56 100644 --- a/index.js +++ b/index.js @@ -1,56 +1,2 @@ -/** - * @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension - * @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension - * - * @typedef {import('mdast-util-mdx-expression').MdxFlowExpression} MdxFlowExpression - * @typedef {import('mdast-util-mdx-expression').MdxTextExpression} MdxTextExpression - * @typedef {import('mdast-util-mdxjs-esm').MdxjsEsm} MdxjsEsm - * @typedef {import('mdast-util-mdx-jsx').MdxJsxAttributeValueExpression} MdxJsxAttributeValueExpression - * @typedef {import('mdast-util-mdx-jsx').MdxJsxAttribute} MdxJsxAttribute - * @typedef {import('mdast-util-mdx-jsx').MdxJsxExpressionAttribute} MdxJsxExpressionAttribute - * @typedef {import('mdast-util-mdx-jsx').MdxJsxFlowElement} MdxJsxFlowElement - * @typedef {import('mdast-util-mdx-jsx').MdxJsxTextElement} MdxJsxTextElement - * @typedef {import('mdast-util-mdx-jsx').ToMarkdownOptions} ToMarkdownOptions - */ - -import { - mdxExpressionFromMarkdown, - mdxExpressionToMarkdown -} from 'mdast-util-mdx-expression' -import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'mdast-util-mdx-jsx' -import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'mdast-util-mdxjs-esm' - -/** - * Create an extension for `mdast-util-from-markdown` to enable MDX (ESM, JSX, - * expressions). - * - * @returns {Array} - * Extension for `mdast-util-from-markdown` to enable MDX (ESM, JSX, - * expressions). - * - * When using the syntax extensions with `addResult`, ESM and expression - * nodes will have `data.estree` fields set to ESTree `Program` node. - */ -export function mdxFromMarkdown() { - return [mdxExpressionFromMarkdown, mdxJsxFromMarkdown(), mdxjsEsmFromMarkdown] -} - -/** - * Create an extension for `mdast-util-to-markdown` to enable MDX (ESM, JSX, - * expressions). - * - * @param {ToMarkdownOptions | null | undefined} [options] - * Configuration. - * @returns {ToMarkdownExtension} - * Extension for `mdast-util-to-markdown` to enable MDX (ESM, JSX, - * expressions). - */ -export function mdxToMarkdown(options) { - return { - extensions: [ - mdxExpressionToMarkdown, - mdxJsxToMarkdown(options), - mdxjsEsmToMarkdown - ] - } -} +// Note: types exposed from `index.d.ts`. +export {mdxFromMarkdown, mdxToMarkdown} from './lib/index.js' diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..f9eb573 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,51 @@ +/** + * @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension + * @typedef {import('mdast-util-mdx-jsx').ToMarkdownOptions} ToMarkdownOptions + * @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension + */ + +import { + mdxExpressionFromMarkdown, + mdxExpressionToMarkdown +} from 'mdast-util-mdx-expression' +import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'mdast-util-mdx-jsx' +import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'mdast-util-mdxjs-esm' + +/** + * Create an extension for `mdast-util-from-markdown` to enable MDX (ESM, JSX, + * expressions). + * + * @returns {Array} + * Extension for `mdast-util-from-markdown` to enable MDX (ESM, JSX, + * expressions). + * + * When using the syntax extensions with `addResult`, ESM and expression + * nodes will have `data.estree` fields set to ESTree `Program` node. + */ +export function mdxFromMarkdown() { + return [ + mdxExpressionFromMarkdown(), + mdxJsxFromMarkdown(), + mdxjsEsmFromMarkdown() + ] +} + +/** + * Create an extension for `mdast-util-to-markdown` to enable MDX (ESM, JSX, + * expressions). + * + * @param {ToMarkdownOptions | null | undefined} [options] + * Configuration (optional). + * @returns {ToMarkdownExtension} + * Extension for `mdast-util-to-markdown` to enable MDX (ESM, JSX, + * expressions). + */ +export function mdxToMarkdown(options) { + return { + extensions: [ + mdxExpressionToMarkdown(), + mdxJsxToMarkdown(options), + mdxjsEsmToMarkdown() + ] + } +} diff --git a/package.json b/package.json index 79c0c79..caa1a49 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mdast-util-mdx", - "version": "2.0.1", + "version": "3.0.0", "description": "mdast extension to parse and serialize MDX (or MDX.js)", "license": "MIT", "keywords": [ @@ -29,59 +29,59 @@ ], "sideEffects": false, "type": "module", - "main": "index.js", - "types": "index.d.ts", + "exports": "./index.js", "files": [ + "lib/", "index.d.ts", "index.js" ], "dependencies": { - "mdast-util-from-markdown": "^1.0.0", - "mdast-util-mdx-expression": "^1.0.0", - "mdast-util-mdx-jsx": "^2.0.0", - "mdast-util-mdxjs-esm": "^1.0.0", - "mdast-util-to-markdown": "^1.0.0" + "mdast-util-from-markdown": "^2.0.0", + "mdast-util-mdx-expression": "^2.0.0", + "mdast-util-mdx-jsx": "^3.0.0", + "mdast-util-mdxjs-esm": "^2.0.0", + "mdast-util-to-markdown": "^2.0.0" }, "devDependencies": { - "@types/node": "^18.0.0", - "c8": "^7.0.0", - "micromark-extension-mdx": "^1.0.0", - "micromark-extension-mdxjs": "^1.0.0", + "@types/node": "^20.0.0", + "c8": "^8.0.0", + "micromark-extension-mdx": "^2.0.0", + "micromark-extension-mdxjs": "^2.0.0", "prettier": "^2.0.0", "remark-cli": "^11.0.0", "remark-preset-wooorm": "^9.0.0", "type-coverage": "^2.0.0", - "typescript": "^4.0.0", - "xo": "^0.53.0" + "typescript": "^5.0.0", + "xo": "^0.54.0" }, "scripts": { "prepack": "npm run build && npm run format", "build": "tsc --build --clean && tsc --build && type-coverage", "format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", "test-api": "node --conditions development test.js", - "test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", + "test-coverage": "c8 --100 --reporter lcov npm run test-api", "test": "npm run build && npm run format && npm run test-coverage" }, "prettier": { - "tabWidth": 2, - "useTabs": false, - "singleQuote": true, "bracketSpacing": false, "semi": false, - "trailingComma": "none" - }, - "xo": { - "prettier": true + "singleQuote": true, + "tabWidth": 2, + "trailingComma": "none", + "useTabs": false }, "remarkConfig": { "plugins": [ - "preset-wooorm" + "remark-preset-wooorm" ] }, "typeCoverage": { "atLeast": 100, "detail": true, - "strict": true, - "ignoreCatch": true + "ignoreCatch": true, + "strict": true + }, + "xo": { + "prettier": true } } diff --git a/readme.md b/readme.md index 6aded2c..b54df5a 100644 --- a/readme.md +++ b/readme.md @@ -64,7 +64,7 @@ internals away. ## Install This package is [ESM only][esm]. -In Node.js (version 14.14+ and 16.0+), install with [npm][]: +In Node.js (version 16+), install with [npm][]: ```sh npm install mdast-util-mdx @@ -73,14 +73,14 @@ npm install mdast-util-mdx In Deno with [`esm.sh`][esmsh]: ```js -import {mdxFromMarkdown, mdxToMarkdown} from 'https://esm.sh/mdast-util-mdx@2' +import {mdxFromMarkdown, mdxToMarkdown} from 'https://esm.sh/mdast-util-mdx@3' ``` In browsers with [`esm.sh`][esmsh]: ```html ``` @@ -88,7 +88,7 @@ In browsers with [`esm.sh`][esmsh]: Say our document `example.mdx` contains: -```markdown +```mdx import Box from "place" Here’s an expression: @@ -110,10 +110,10 @@ Which you can also put inline: {1+1}. ```js import fs from 'node:fs/promises' -import {fromMarkdown} from 'mdast-util-from-markdown' -import {toMarkdown} from 'mdast-util-to-markdown' import {mdxjs} from 'micromark-extension-mdxjs' +import {fromMarkdown} from 'mdast-util-from-markdown' import {mdxFromMarkdown, mdxToMarkdown} from 'mdast-util-mdx' +import {toMarkdown} from 'mdast-util-to-markdown' const doc = await fs.readFile('example.mdx') @@ -249,7 +249,7 @@ console.log(out) } ``` -```markdown +```mdx import Box from "place" Here’s an expression: @@ -350,10 +350,17 @@ See their readmes for the node types supported in the tree: ## Types This package is fully typed with [TypeScript][]. -It exports the additional types `MdxFlowExpression`, `MdxTextExpression`, -`MdxjsEsm`, `MdxJsxAttributeValueExpression`, `MdxJsxAttribute`, -`MdxJsxExpressionAttribute`, `MdxJsxFlowElement`, -`MdxJsxTextElement`, and [`ToMarkdownOptions`][api-to-markdown-options]. +It exports the additional types +`MdxFlowExpression` and `MdxTextExpression` +from `mdast-util-mdx-expression`; +`MdxJsxAttribute`, +`MdxJsxAttributeValueExpression`, +`MdxJsxExpressionAttribute`, +`MdxJsxFlowElement`, +`MdxJsxTextElement`, +and [`ToMarkdownOptions`][api-to-markdown-options] +from `mdast-util-mdx-jsx`; +and `MdxjsEsm` from `mdast-util-mdxjs-esm`. It also registers the node types with `@types/mdast` and `@types/hast`. If you’re working with the syntax tree, make sure to import this utility @@ -369,20 +376,23 @@ import {visit} from 'unist-util-visit' /** @type {import('mdast').Root} */ const tree = getMdastNodeSomeHow() -visit(tree, (node) => { +visit(tree, function (node) { // `node` can now be an expression, JSX, or ESM node. }) ``` ## Compatibility -Projects maintained by the unified collective are compatible with all maintained +Projects maintained by the unified collective are compatible with maintained versions of Node.js. -As of now, that is Node.js 14.14+ and 16.0+. -Our projects sometimes work with older versions, but this is not guaranteed. -This utility works with `mdast-util-from-markdown` version 1+ and -`mdast-util-to-markdown` version 1+. +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, `mdast-util-mdx@^3`, +compatible with Node.js 16. + +This utility works with `mdast-util-from-markdown` version 2+ and +`mdast-util-to-markdown` version 2+. ## Related @@ -421,9 +431,9 @@ abide by its terms. [downloads]: https://www.npmjs.com/package/mdast-util-mdx -[size-badge]: https://img.shields.io/bundlephobia/minzip/mdast-util-mdx.svg +[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=mdast-util-mdx -[size]: https://bundlephobia.com/result?p=mdast-util-mdx +[size]: https://bundlejs.com/?q=mdast-util-mdx [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg diff --git a/test.js b/test.js index db55e9b..13fb034 100644 --- a/test.js +++ b/test.js @@ -1,581 +1,593 @@ import assert from 'node:assert/strict' import test from 'node:test' +import {mdxjs} from 'micromark-extension-mdxjs' import {fromMarkdown} from 'mdast-util-from-markdown' +import {mdxFromMarkdown, mdxToMarkdown} from 'mdast-util-mdx' import {toMarkdown} from 'mdast-util-to-markdown' -import {mdxjs} from 'micromark-extension-mdxjs' -import {mdxFromMarkdown, mdxToMarkdown} from './index.js' -import * as mod from './index.js' -test('core', () => { - assert.deepEqual( - Object.keys(mod).sort(), - ['mdxFromMarkdown', 'mdxToMarkdown'], - 'should expose the public api' - ) +test('core', async function (t) { + await t.test('should expose the public api', async function () { + assert.deepEqual(Object.keys(await import('mdast-util-mdx')).sort(), [ + 'mdxFromMarkdown', + 'mdxToMarkdown' + ]) + }) }) -test('mdxFromMarkdown', () => { - assert.deepEqual( - JSON.parse( - JSON.stringify( - fromMarkdown('import a from "b"', { - extensions: [mdxjs()], - mdastExtensions: [mdxFromMarkdown()] - }) - ) - ), - { - type: 'root', - children: [ - { - type: 'mdxjsEsm', - value: 'import a from "b"', - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 1, column: 18, offset: 17} - }, - data: { - estree: { - type: 'Program', - start: 0, - end: 17, - body: [ - { - type: 'ImportDeclaration', - start: 0, - end: 17, - specifiers: [ - { - type: 'ImportDefaultSpecifier', - start: 7, - end: 8, - local: { - type: 'Identifier', +test('mdxFromMarkdown()', async function (t) { + await t.test('should support esm', async function () { + assert.deepEqual( + JSON.parse( + JSON.stringify( + fromMarkdown('import a from "b"', { + extensions: [mdxjs()], + mdastExtensions: [mdxFromMarkdown()] + }) + ) + ), + { + type: 'root', + children: [ + { + type: 'mdxjsEsm', + value: 'import a from "b"', + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 1, column: 18, offset: 17} + }, + data: { + estree: { + type: 'Program', + start: 0, + end: 17, + body: [ + { + type: 'ImportDeclaration', + start: 0, + end: 17, + specifiers: [ + { + type: 'ImportDefaultSpecifier', start: 7, end: 8, - name: 'a', + local: { + type: 'Identifier', + start: 7, + end: 8, + name: 'a', + loc: { + start: {line: 1, column: 7, offset: 7}, + end: {line: 1, column: 8, offset: 8} + }, + range: [7, 8] + }, loc: { start: {line: 1, column: 7, offset: 7}, end: {line: 1, column: 8, offset: 8} }, range: [7, 8] - }, + } + ], + source: { + type: 'Literal', + start: 14, + end: 17, + value: 'b', + raw: '"b"', loc: { - start: {line: 1, column: 7, offset: 7}, - end: {line: 1, column: 8, offset: 8} + start: {line: 1, column: 14, offset: 14}, + end: {line: 1, column: 17, offset: 17} }, - range: [7, 8] - } - ], - source: { - type: 'Literal', - start: 14, - end: 17, - value: 'b', - raw: '"b"', + range: [14, 17] + }, loc: { - start: {line: 1, column: 14, offset: 14}, + start: {line: 1, column: 0, offset: 0}, end: {line: 1, column: 17, offset: 17} }, - range: [14, 17] - }, - loc: { - start: {line: 1, column: 0, offset: 0}, - end: {line: 1, column: 17, offset: 17} - }, - range: [0, 17] - } - ], - sourceType: 'module', - comments: [], - loc: { - start: {line: 1, column: 0, offset: 0}, - end: {line: 1, column: 17, offset: 17} - }, - range: [0, 17] + range: [0, 17] + } + ], + sourceType: 'module', + comments: [], + loc: { + start: {line: 1, column: 0, offset: 0}, + end: {line: 1, column: 17, offset: 17} + }, + range: [0, 17] + } } } + ], + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 1, column: 18, offset: 17} } - ], - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 1, column: 18, offset: 17} } - }, - 'should support esm' - ) + ) + }) - assert.deepEqual( - fromMarkdown('', { - extensions: [mdxjs()], - mdastExtensions: [mdxFromMarkdown()] - }), - { - type: 'root', - children: [ - { - type: 'mdxJsxFlowElement', - name: 'x', - attributes: [], - children: [], - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 1, column: 5, offset: 4} + await t.test('should support jsx', async function () { + assert.deepEqual( + fromMarkdown('', { + extensions: [mdxjs()], + mdastExtensions: [mdxFromMarkdown()] + }), + { + type: 'root', + children: [ + { + type: 'mdxJsxFlowElement', + name: 'x', + attributes: [], + children: [], + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 1, column: 5, offset: 4} + } } + ], + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 1, column: 5, offset: 4} } - ], - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 1, column: 5, offset: 4} } - }, - 'should support jsx' - ) + ) + }) - assert.deepEqual( - JSON.parse( - JSON.stringify( - fromMarkdown('{1 + 1}', { - extensions: [mdxjs()], - mdastExtensions: [mdxFromMarkdown()] - }) - ) - ), - { - type: 'root', - children: [ - { - type: 'mdxFlowExpression', - value: '1 + 1', - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 1, column: 8, offset: 7} - }, - data: { - estree: { - type: 'Program', - start: 1, - end: 6, - body: [ - { - type: 'ExpressionStatement', - expression: { - type: 'BinaryExpression', - start: 1, - end: 6, - left: { - type: 'Literal', + await t.test('should support expressions', async function () { + assert.deepEqual( + JSON.parse( + JSON.stringify( + fromMarkdown('{1 + 1}', { + extensions: [mdxjs()], + mdastExtensions: [mdxFromMarkdown()] + }) + ) + ), + { + type: 'root', + children: [ + { + type: 'mdxFlowExpression', + value: '1 + 1', + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 1, column: 8, offset: 7} + }, + data: { + estree: { + type: 'Program', + start: 1, + end: 6, + body: [ + { + type: 'ExpressionStatement', + expression: { + type: 'BinaryExpression', start: 1, - end: 2, - value: 1, - raw: '1', - loc: { - start: {line: 1, column: 1, offset: 1}, - end: {line: 1, column: 2, offset: 2} - }, - range: [1, 2] - }, - operator: '+', - right: { - type: 'Literal', - start: 5, end: 6, - value: 1, - raw: '1', + left: { + type: 'Literal', + start: 1, + end: 2, + value: 1, + raw: '1', + loc: { + start: {line: 1, column: 1, offset: 1}, + end: {line: 1, column: 2, offset: 2} + }, + range: [1, 2] + }, + operator: '+', + right: { + type: 'Literal', + start: 5, + end: 6, + value: 1, + raw: '1', + loc: { + start: {line: 1, column: 5, offset: 5}, + end: {line: 1, column: 6, offset: 6} + }, + range: [5, 6] + }, loc: { - start: {line: 1, column: 5, offset: 5}, + start: {line: 1, column: 1, offset: 1}, end: {line: 1, column: 6, offset: 6} }, - range: [5, 6] + range: [1, 6] }, + start: 1, + end: 6, loc: { start: {line: 1, column: 1, offset: 1}, end: {line: 1, column: 6, offset: 6} }, range: [1, 6] - }, - start: 1, - end: 6, - loc: { - start: {line: 1, column: 1, offset: 1}, - end: {line: 1, column: 6, offset: 6} - }, - range: [1, 6] - } - ], - sourceType: 'module', - comments: [], - loc: { - start: {line: 1, column: 1, offset: 1}, - end: {line: 1, column: 6, offset: 6} - }, - range: [1, 6] + } + ], + sourceType: 'module', + comments: [], + loc: { + start: {line: 1, column: 1, offset: 1}, + end: {line: 1, column: 6, offset: 6} + }, + range: [1, 6] + } } } + ], + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 1, column: 8, offset: 7} } - ], - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 1, column: 8, offset: 7} } - }, - 'should support expressions' - ) + ) + }) - assert.deepEqual( - JSON.parse( - JSON.stringify( - fromMarkdown( - "\n {{\n template: /* Comment */ '',\n }}\n", - { - extensions: [mdxjs()], - mdastExtensions: [mdxFromMarkdown()] - } - ) - ) - ), - { - type: 'root', - children: [ - { - type: 'mdxJsxFlowElement', - name: 'Stuff', - attributes: [], - children: [ + await t.test('should add proper positions on estree (1)', async function () { + assert.deepEqual( + JSON.parse( + JSON.stringify( + fromMarkdown( + "\n {{\n template: /* Comment */ '',\n }}\n", { - type: 'mdxFlowExpression', - value: "{\n template: /* Comment */ '',\n}", - position: { - start: {line: 2, column: 3, offset: 10}, - end: {line: 4, column: 5, offset: 49} - }, - data: { - estree: { - type: 'Program', - start: 11, - end: 48, - body: [ - { - type: 'ExpressionStatement', - expression: { - type: 'ObjectExpression', - start: 11, - end: 48, - properties: [ - { - type: 'Property', - start: 17, - end: 43, - method: false, - shorthand: false, - computed: false, - key: { - type: 'Identifier', + extensions: [mdxjs()], + mdastExtensions: [mdxFromMarkdown()] + } + ) + ) + ), + { + type: 'root', + children: [ + { + type: 'mdxJsxFlowElement', + name: 'Stuff', + attributes: [], + children: [ + { + type: 'mdxFlowExpression', + value: "{\n template: /* Comment */ '',\n }", + position: { + start: {line: 2, column: 3, offset: 10}, + end: {line: 4, column: 5, offset: 49} + }, + data: { + estree: { + type: 'Program', + start: 11, + end: 48, + body: [ + { + type: 'ExpressionStatement', + expression: { + type: 'ObjectExpression', + start: 11, + end: 48, + properties: [ + { + type: 'Property', start: 17, - end: 25, - name: 'template', - loc: { - start: {line: 3, column: 4, offset: 17}, - end: {line: 3, column: 12, offset: 25} - }, - range: [17, 25] - }, - value: { - type: 'Literal', - start: 41, end: 43, - value: '', - raw: "''", + method: false, + shorthand: false, + computed: false, + key: { + type: 'Identifier', + start: 17, + end: 25, + name: 'template', + loc: { + start: {line: 3, column: 4, offset: 17}, + end: {line: 3, column: 12, offset: 25} + }, + range: [17, 25] + }, + value: { + type: 'Literal', + start: 41, + end: 43, + value: '', + raw: "''", + loc: { + start: {line: 3, column: 28, offset: 41}, + end: {line: 3, column: 30, offset: 43} + }, + range: [41, 43] + }, + kind: 'init', loc: { - start: {line: 3, column: 28, offset: 41}, + start: {line: 3, column: 4, offset: 17}, end: {line: 3, column: 30, offset: 43} }, - range: [41, 43] - }, - kind: 'init', - loc: { - start: {line: 3, column: 4, offset: 17}, - end: {line: 3, column: 30, offset: 43} - }, - range: [17, 43] - } - ], + range: [17, 43] + } + ], + loc: { + start: {line: 2, column: 3, offset: 11}, + end: {line: 4, column: 3, offset: 48} + }, + range: [11, 48] + }, + start: 11, + end: 48, loc: { start: {line: 2, column: 3, offset: 11}, end: {line: 4, column: 3, offset: 48} }, range: [11, 48] - }, - start: 11, - end: 48, - loc: { - start: {line: 2, column: 3, offset: 11}, - end: {line: 4, column: 3, offset: 48} - }, - range: [11, 48] - } - ], - sourceType: 'module', - comments: [ - { - type: 'Block', - value: ' Comment ', - start: 27, - end: 40, - loc: { - start: {line: 3, column: 14, offset: 27}, - end: {line: 3, column: 27, offset: 40} - }, - range: [27, 40] - } - ], - loc: { - start: {line: 2, column: 3, offset: 11}, - end: {line: 4, column: 3, offset: 48} - }, - range: [11, 48] + } + ], + sourceType: 'module', + comments: [ + { + type: 'Block', + value: ' Comment ', + start: 27, + end: 40, + loc: { + start: {line: 3, column: 14, offset: 27}, + end: {line: 3, column: 27, offset: 40} + }, + range: [27, 40] + } + ], + loc: { + start: {line: 2, column: 3, offset: 11}, + end: {line: 4, column: 3, offset: 48} + }, + range: [11, 48] + } } } + ], + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 5, column: 9, offset: 58} } - ], - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 5, column: 9, offset: 58} } + ], + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 5, column: 9, offset: 58} } - ], - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 5, column: 9, offset: 58} } - }, - 'should add proper positions on estree (1)' - ) + ) + }) - assert.deepEqual( - JSON.parse( - JSON.stringify( - fromMarkdown("export let a = 'a'\n\nexport let b = 'b'", { - extensions: [mdxjs()], - mdastExtensions: [mdxFromMarkdown()] - }) - ) - ), - { - type: 'root', - children: [ - { - type: 'mdxjsEsm', - value: "export let a = 'a'", - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 1, column: 19, offset: 18} - }, - data: { - estree: { - type: 'Program', - start: 0, - end: 18, - body: [ - { - type: 'ExportNamedDeclaration', - start: 0, - end: 18, - declaration: { - type: 'VariableDeclaration', - start: 7, + await t.test('should add proper positions on estree (2)', async function () { + assert.deepEqual( + JSON.parse( + JSON.stringify( + fromMarkdown("export let a = 'a'\n\nexport let b = 'b'", { + extensions: [mdxjs()], + mdastExtensions: [mdxFromMarkdown()] + }) + ) + ), + { + type: 'root', + children: [ + { + type: 'mdxjsEsm', + value: "export let a = 'a'", + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 1, column: 19, offset: 18} + }, + data: { + estree: { + type: 'Program', + start: 0, + end: 18, + body: [ + { + type: 'ExportNamedDeclaration', + start: 0, end: 18, - declarations: [ - { - type: 'VariableDeclarator', - start: 11, - end: 18, - id: { - type: 'Identifier', + declaration: { + type: 'VariableDeclaration', + start: 7, + end: 18, + declarations: [ + { + type: 'VariableDeclarator', start: 11, - end: 12, - name: 'a', - loc: { - start: {line: 1, column: 11, offset: 11}, - end: {line: 1, column: 12, offset: 12} - }, - range: [11, 12] - }, - init: { - type: 'Literal', - start: 15, end: 18, - value: 'a', - raw: "'a'", + id: { + type: 'Identifier', + start: 11, + end: 12, + name: 'a', + loc: { + start: {line: 1, column: 11, offset: 11}, + end: {line: 1, column: 12, offset: 12} + }, + range: [11, 12] + }, + init: { + type: 'Literal', + start: 15, + end: 18, + value: 'a', + raw: "'a'", + loc: { + start: {line: 1, column: 15, offset: 15}, + end: {line: 1, column: 18, offset: 18} + }, + range: [15, 18] + }, loc: { - start: {line: 1, column: 15, offset: 15}, + start: {line: 1, column: 11, offset: 11}, end: {line: 1, column: 18, offset: 18} }, - range: [15, 18] - }, - loc: { - start: {line: 1, column: 11, offset: 11}, - end: {line: 1, column: 18, offset: 18} - }, - range: [11, 18] - } - ], - kind: 'let', + range: [11, 18] + } + ], + kind: 'let', + loc: { + start: {line: 1, column: 7, offset: 7}, + end: {line: 1, column: 18, offset: 18} + }, + range: [7, 18] + }, + specifiers: [], + source: null, loc: { - start: {line: 1, column: 7, offset: 7}, + start: {line: 1, column: 0, offset: 0}, end: {line: 1, column: 18, offset: 18} }, - range: [7, 18] - }, - specifiers: [], - source: null, - loc: { - start: {line: 1, column: 0, offset: 0}, - end: {line: 1, column: 18, offset: 18} - }, - range: [0, 18] - } - ], - sourceType: 'module', - comments: [], - loc: { - start: {line: 1, column: 0, offset: 0}, - end: {line: 1, column: 18, offset: 18} - }, - range: [0, 18] + range: [0, 18] + } + ], + sourceType: 'module', + comments: [], + loc: { + start: {line: 1, column: 0, offset: 0}, + end: {line: 1, column: 18, offset: 18} + }, + range: [0, 18] + } } - } - }, - { - type: 'mdxjsEsm', - value: "export let b = 'b'", - position: { - start: {line: 3, column: 1, offset: 20}, - end: {line: 3, column: 19, offset: 38} }, - data: { - estree: { - type: 'Program', - start: 20, - end: 38, - body: [ - { - type: 'ExportNamedDeclaration', - start: 20, - end: 38, - declaration: { - type: 'VariableDeclaration', - start: 27, + { + type: 'mdxjsEsm', + value: "export let b = 'b'", + position: { + start: {line: 3, column: 1, offset: 20}, + end: {line: 3, column: 19, offset: 38} + }, + data: { + estree: { + type: 'Program', + start: 20, + end: 38, + body: [ + { + type: 'ExportNamedDeclaration', + start: 20, end: 38, - declarations: [ - { - type: 'VariableDeclarator', - start: 31, - end: 38, - id: { - type: 'Identifier', + declaration: { + type: 'VariableDeclaration', + start: 27, + end: 38, + declarations: [ + { + type: 'VariableDeclarator', start: 31, - end: 32, - name: 'b', - loc: { - start: {line: 3, column: 11, offset: 31}, - end: {line: 3, column: 12, offset: 32} - }, - range: [31, 32] - }, - init: { - type: 'Literal', - start: 35, end: 38, - value: 'b', - raw: "'b'", + id: { + type: 'Identifier', + start: 31, + end: 32, + name: 'b', + loc: { + start: {line: 3, column: 11, offset: 31}, + end: {line: 3, column: 12, offset: 32} + }, + range: [31, 32] + }, + init: { + type: 'Literal', + start: 35, + end: 38, + value: 'b', + raw: "'b'", + loc: { + start: {line: 3, column: 15, offset: 35}, + end: {line: 3, column: 18, offset: 38} + }, + range: [35, 38] + }, loc: { - start: {line: 3, column: 15, offset: 35}, + start: {line: 3, column: 11, offset: 31}, end: {line: 3, column: 18, offset: 38} }, - range: [35, 38] - }, - loc: { - start: {line: 3, column: 11, offset: 31}, - end: {line: 3, column: 18, offset: 38} - }, - range: [31, 38] - } - ], - kind: 'let', + range: [31, 38] + } + ], + kind: 'let', + loc: { + start: {line: 3, column: 7, offset: 27}, + end: {line: 3, column: 18, offset: 38} + }, + range: [27, 38] + }, + specifiers: [], + source: null, loc: { - start: {line: 3, column: 7, offset: 27}, + start: {line: 3, column: 0, offset: 20}, end: {line: 3, column: 18, offset: 38} }, - range: [27, 38] - }, - specifiers: [], - source: null, - loc: { - start: {line: 3, column: 0, offset: 20}, - end: {line: 3, column: 18, offset: 38} - }, - range: [20, 38] - } - ], - sourceType: 'module', - comments: [], - loc: { - start: {line: 3, column: 0, offset: 20}, - end: {line: 3, column: 18, offset: 38} - }, - range: [20, 38] + range: [20, 38] + } + ], + sourceType: 'module', + comments: [], + loc: { + start: {line: 3, column: 0, offset: 20}, + end: {line: 3, column: 18, offset: 38} + }, + range: [20, 38] + } } } + ], + position: { + start: {line: 1, column: 1, offset: 0}, + end: {line: 3, column: 19, offset: 38} } - ], - position: { - start: {line: 1, column: 1, offset: 0}, - end: {line: 3, column: 19, offset: 38} } - }, - 'should add proper positions on estree (2)' - ) + ) + }) }) -test('mdxToMarkdown', () => { - assert.equal( - toMarkdown( - {type: 'mdxjsEsm', value: 'import a from "b"'}, - {extensions: [mdxToMarkdown()]} - ), - 'import a from "b"\n', - 'should support esm' - ) +test('mdxToMarkdown()', async function (t) { + await t.test('should support esm', async function () { + assert.equal( + toMarkdown( + {type: 'mdxjsEsm', value: 'import a from "b"'}, + {extensions: [mdxToMarkdown()]} + ), + 'import a from "b"\n' + ) + }) - assert.equal( - toMarkdown( - {type: 'mdxJsxFlowElement', name: 'x', attributes: [], children: []}, - {extensions: [mdxToMarkdown()]} - ), - '\n', - 'should support jsx' - ) + await t.test('should support jsx', async function () { + assert.equal( + toMarkdown( + {type: 'mdxJsxFlowElement', name: 'x', attributes: [], children: []}, + {extensions: [mdxToMarkdown()]} + ), + '\n' + ) + }) - assert.deepEqual( - toMarkdown( - {type: 'mdxFlowExpression', value: '1 + 1'}, - {extensions: [mdxToMarkdown()]} - ), - '{1 + 1}\n', - 'should support expressions' - ) + await t.test('should support expressions', async function () { + assert.deepEqual( + toMarkdown( + {type: 'mdxFlowExpression', value: '1 + 1'}, + {extensions: [mdxToMarkdown()]} + ), + '{1 + 1}\n' + ) + }) - assert.deepEqual( - toMarkdown( - { - type: 'link', - url: 'tel:123', - children: [{type: 'text', value: 'tel:123'}] - }, - {extensions: [mdxToMarkdown()]} - ), - '[tel:123](tel:123)\n', - 'should use link (resource) instead of link (auto)' + await t.test( + 'should use link (resource) instead of link (auto)', + async function () { + assert.deepEqual( + toMarkdown( + { + type: 'link', + url: 'tel:123', + children: [{type: 'text', value: 'tel:123'}] + }, + {extensions: [mdxToMarkdown()]} + ), + '[tel:123](tel:123)\n' + ) + } ) }) diff --git a/tsconfig.json b/tsconfig.json index ebe8889..bed2bb4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,17 +1,15 @@ { - "include": ["**/*.js"], - "exclude": ["coverage/", "node_modules/"], "compilerOptions": { "checkJs": true, + "customConditions": ["development"], "declaration": true, "emitDeclarationOnly": true, "exactOptionalPropertyTypes": true, - "forceConsistentCasingInFileNames": true, - "lib": ["es2020"], + "lib": ["es2022"], "module": "node16", - "newLine": "lf", - "skipLibCheck": true, "strict": true, - "target": "es2020" - } + "target": "es2022" + }, + "exclude": ["coverage/", "node_modules/"], + "include": ["**/*.js", "index.d.ts"] }