Skip to content

Commit 43d6a90

Browse files
committed
Use ESM
1 parent cf83552 commit 43d6a90

File tree

8 files changed

+66
-66
lines changed

8 files changed

+66
-66
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.nyc_output/
21
coverage/
32
node_modules/
43
*.log

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

from-markdown.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
1-
exports.fromMarkdown = require('./from-markdown.js')
2-
exports.toMarkdown = require('./to-markdown.js')
1+
export const mdxjsEsmFromMarkdown = {
2+
enter: {mdxjsEsm: enterMdxjsEsm},
3+
exit: {mdxjsEsm: exitMdxjsEsm, mdxjsEsmData: exitMdxjsEsmData}
4+
}
5+
6+
export const mdxjsEsmToMarkdown = {handlers: {mdxjsEsm: handleMdxjsEsm}}
7+
8+
function enterMdxjsEsm(token) {
9+
this.enter({type: 'mdxjsEsm', value: ''}, token)
10+
this.buffer() // Capture EOLs
11+
}
12+
13+
function exitMdxjsEsm(token) {
14+
var value = this.resume()
15+
var node = this.exit(token)
16+
17+
node.value = value
18+
19+
if (token.estree) {
20+
node.data = {estree: token.estree}
21+
}
22+
}
23+
24+
function exitMdxjsEsmData(token) {
25+
this.config.enter.data.call(this, token)
26+
this.config.exit.data.call(this, token)
27+
}
28+
29+
function handleMdxjsEsm(node) {
30+
return node.value || ''
31+
}

package.json

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,31 @@
2828
"contributors": [
2929
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
3030
],
31+
"sideEffects": false,
32+
"type": "module",
33+
"main": "index.js",
3134
"files": [
32-
"from-markdown.js",
33-
"index.js",
34-
"to-markdown.js"
35+
"index.js"
3536
],
36-
"dependencies": {},
3737
"devDependencies": {
3838
"acorn": "^8.0.0",
39+
"c8": "^7.0.0",
3940
"mdast-util-from-markdown": "^0.8.0",
4041
"mdast-util-to-markdown": "^0.6.0",
4142
"micromark-extension-mdxjs-esm": "^0.3.0",
42-
"nyc": "^15.0.0",
4343
"prettier": "^2.0.0",
4444
"remark-cli": "^9.0.0",
4545
"remark-preset-wooorm": "^8.0.0",
4646
"tape": "^5.0.0",
47-
"unist-util-remove-position": "^3.0.0",
48-
"xo": "^0.38.0"
47+
"unist-util-remove-position": "^4.0.0",
48+
"xo": "^0.39.0"
4949
},
5050
"scripts": {
5151
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
52-
"test-api": "node test",
53-
"test-coverage": "nyc --reporter lcov tape test.js",
52+
"test-api": "node --conditions development test.js",
53+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
5454
"test": "npm run format && npm run test-coverage"
5555
},
56-
"nyc": {
57-
"check-coverage": true,
58-
"lines": 100,
59-
"functions": 100,
60-
"branches": 100
61-
},
6256
"prettier": {
6357
"tabWidth": 2,
6458
"useTabs": false,
@@ -69,7 +63,10 @@
6963
},
7064
"xo": {
7165
"prettier": true,
72-
"esnext": false
66+
"rules": {
67+
"no-var": "off",
68+
"prefer-arrow-callback": "off"
69+
}
7370
},
7471
"remarkConfig": {
7572
"plugins": [

readme.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Or use it all through [`remark-mdx`][remark-mdx] (**[remark][]**).
2323

2424
## Install
2525

26+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
27+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
28+
2629
[npm][]:
2730

2831
```sh
@@ -122,12 +125,13 @@ d
122125

123126
## API
124127

125-
### `mdxjsEsm.fromMarkdown`
128+
This package exports the following identifier: `mdxjsEsmFromMarkdown`,
129+
`mdxjsEsmToMarkdown`.
130+
There is no default export.
126131

127-
### `mdxjsEsm.toMarkdown`
132+
### `mdxjsEsmFromMarkdown`
128133

129-
> Note: the separate extensions are also available at
130-
> `mdast-util-mdxjs-esm/from-markdown` and `mdast-util-mdxjs-esm/to-markdown`.
134+
### `mdxjsEsmToMarkdown`
131135

132136
Support MDX.js ESM import/exports.
133137
The exports are extensions, respectively for

test.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
var test = require('tape')
2-
var acorn = require('acorn')
3-
var fromMarkdown = require('mdast-util-from-markdown')
4-
var toMarkdown = require('mdast-util-to-markdown')
5-
var removePosition = require('unist-util-remove-position')
6-
var syntax = require('micromark-extension-mdxjs-esm')
7-
var mdxjsEsm = require('.')
1+
import test from 'tape'
2+
import * as acorn from 'acorn'
3+
import fromMarkdown from 'mdast-util-from-markdown'
4+
import toMarkdown from 'mdast-util-to-markdown'
5+
import {removePosition} from 'unist-util-remove-position'
6+
import mdxjsEsm from 'micromark-extension-mdxjs-esm'
7+
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from './index.js'
88

99
test('markdown -> mdast', function (t) {
1010
t.deepEqual(
1111
fromMarkdown('import a from "b"\nexport var c = ""\n\nd', {
12-
extensions: [syntax({acorn: acorn})],
13-
mdastExtensions: [mdxjsEsm.fromMarkdown]
12+
extensions: [mdxjsEsm({acorn})],
13+
mdastExtensions: [mdxjsEsmFromMarkdown]
1414
}),
1515
{
1616
type: 'root',
@@ -55,8 +55,8 @@ test('markdown -> mdast', function (t) {
5555
JSON.stringify(
5656
removePosition(
5757
fromMarkdown('import a from "b"\nexport var c = ""\n\nd', {
58-
extensions: [syntax({acorn: acorn, addResult: true})],
59-
mdastExtensions: [mdxjsEsm.fromMarkdown]
58+
extensions: [mdxjsEsm({acorn, addResult: true})],
59+
mdastExtensions: [mdxjsEsmFromMarkdown]
6060
}),
6161
true
6262
)
@@ -188,7 +188,7 @@ test('markdown -> mdast', function (t) {
188188
{type: 'paragraph', children: [{type: 'text', value: 'd'}]}
189189
]
190190
},
191-
'should add a `data.estree` if `addResult` was used in the syntax plugin'
191+
'should add a `data.estree` if `addResult` was used in the syntax extension'
192192
)
193193

194194
t.end()
@@ -204,7 +204,7 @@ test('mdast -> markdown', function (t) {
204204
{type: 'paragraph', children: [{type: 'text', value: 'd'}]}
205205
]
206206
},
207-
{extensions: [mdxjsEsm.toMarkdown]}
207+
{extensions: [mdxjsEsmToMarkdown]}
208208
),
209209
'import a from "b"\nexport var c = ""\n\nd\n',
210210
'should serialize ESM'
@@ -219,7 +219,7 @@ test('mdast -> markdown', function (t) {
219219
{type: 'paragraph', children: [{type: 'text', value: 'd'}]}
220220
]
221221
},
222-
{extensions: [mdxjsEsm.toMarkdown]}
222+
{extensions: [mdxjsEsmToMarkdown]}
223223
),
224224
'\n\nd\n',
225225
'should not crash on ESM missing `value`'

to-markdown.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)