Skip to content

Commit 7cb41ac

Browse files
committed
Use ESM
1 parent 79fca5d commit 7cb41ac

File tree

8 files changed

+147
-149
lines changed

8 files changed

+147
-149
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 & 51 deletions
This file was deleted.

index.js

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,93 @@
1-
exports.fromMarkdown = require('./from-markdown.js')
2-
exports.toMarkdown = require('./to-markdown.js')
1+
import stripIndent from 'strip-indent'
2+
3+
var eol = /\r?\n|\r/g
4+
5+
export const mdxExpressionFromMarkdown = {
6+
enter: {
7+
mdxFlowExpression: enterMdxFlowExpression,
8+
mdxTextExpression: enterMdxTextExpression
9+
},
10+
exit: {
11+
mdxFlowExpression: exitMdxExpression,
12+
mdxFlowExpressionChunk: exitMdxExpressionData,
13+
mdxTextExpression: exitMdxExpression,
14+
mdxTextExpressionChunk: exitMdxExpressionData
15+
}
16+
}
17+
18+
export const mdxExpressionToMarkdown = {
19+
handlers: {
20+
mdxFlowExpression: handleMdxExpression,
21+
mdxTextExpression: handleMdxExpression
22+
},
23+
unsafe: [
24+
{character: '{', inConstruct: ['phrasing']},
25+
{atBreak: true, character: '{'}
26+
]
27+
}
28+
29+
function enterMdxFlowExpression(token) {
30+
this.enter({type: 'mdxFlowExpression', value: ''}, token)
31+
this.buffer()
32+
}
33+
34+
function enterMdxTextExpression(token) {
35+
this.enter({type: 'mdxTextExpression', value: ''}, token)
36+
this.buffer()
37+
}
38+
39+
function exitMdxExpression(token) {
40+
var value = this.resume()
41+
var node = this.exit(token)
42+
43+
node.value = token.type === 'mdxFlowExpression' ? dedent(value) : value
44+
45+
if (token.estree) {
46+
node.data = {estree: token.estree}
47+
}
48+
}
49+
50+
function exitMdxExpressionData(token) {
51+
this.config.enter.data.call(this, token)
52+
this.config.exit.data.call(this, token)
53+
}
54+
55+
function dedent(value) {
56+
var firstLineEnding = /\r?\n|\r/.exec(value)
57+
var position = firstLineEnding
58+
? firstLineEnding.index + firstLineEnding[0].length
59+
: -1
60+
61+
if (position > -1) {
62+
return value.slice(0, position) + stripIndent(value.slice(position))
63+
}
64+
65+
return value
66+
}
67+
68+
function handleMdxExpression(node) {
69+
var value = node.value || ''
70+
return '{' + (node.type === 'mdxFlowExpression' ? indent(value) : value) + '}'
71+
}
72+
73+
function indent(value) {
74+
var result = []
75+
var start = 0
76+
var line = 0
77+
var match
78+
79+
while ((match = eol.exec(value))) {
80+
one(value.slice(start, match.index))
81+
result.push(match[0])
82+
start = match.index + match[0].length
83+
line++
84+
}
85+
86+
one(value.slice(start))
87+
88+
return result.join('')
89+
90+
function one(slice) {
91+
result.push((line && slice ? ' ' : '') + slice)
92+
}
93+
}

package.json

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,34 @@
2626
"contributors": [
2727
"Titus Wormer <[email protected]> (https://wooorm.com)"
2828
],
29+
"sideEffects": false,
30+
"type": "module",
31+
"main": "index.js",
2932
"files": [
30-
"from-markdown.js",
31-
"index.js",
32-
"to-markdown.js"
33+
"index.js"
3334
],
3435
"dependencies": {
35-
"strip-indent": "^3.0.0"
36+
"strip-indent": "^4.0.0"
3637
},
3738
"devDependencies": {
3839
"acorn": "^8.0.0",
40+
"c8": "^7.0.0",
3941
"mdast-util-from-markdown": "^0.8.0",
4042
"mdast-util-to-markdown": "^0.6.0",
4143
"micromark-extension-mdx-expression": "^0.3.0",
42-
"nyc": "^15.0.0",
4344
"prettier": "^2.0.0",
4445
"remark-cli": "^9.0.0",
4546
"remark-preset-wooorm": "^8.0.0",
4647
"tape": "^5.0.0",
47-
"unist-util-remove-position": "^3.0.0",
48-
"xo": "^0.38.0"
48+
"unist-util-remove-position": "^4.0.0",
49+
"xo": "^0.39.0"
4950
},
5051
"scripts": {
5152
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
52-
"test-api": "node test",
53-
"test-coverage": "nyc --reporter lcov tape test.js",
53+
"test-api": "node --conditions development test.js",
54+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
5455
"test": "npm run format && npm run test-coverage"
5556
},
56-
"nyc": {
57-
"check-coverage": true,
58-
"lines": 100,
59-
"functions": 100,
60-
"branches": 100
61-
},
6257
"prettier": {
6358
"tabWidth": 2,
6459
"useTabs": false,
@@ -69,7 +64,11 @@
6964
},
7065
"xo": {
7166
"prettier": true,
72-
"esnext": false
67+
"prettier": true,
68+
"rules": {
69+
"no-var": "off",
70+
"prefer-arrow-callback": "off"
71+
}
7372
},
7473
"remarkConfig": {
7574
"plugins": [

readme.md

Lines changed: 5 additions & 6 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
@@ -129,13 +132,9 @@ b {true}.
129132

130133
## API
131134

132-
### `mdxExpression.fromMarkdown`
133-
134-
### `mdxExpression.toMarkdown`
135+
### `mdxExpressionFromMarkdown`
135136

136-
> Note: the separate extensions are also available at
137-
> `mdast-util-mdx-expression/from-markdown` and
138-
> `mdast-util-mdx-expression/to-markdown`.
137+
### `mdxExpressionToMarkdown`
139138

140139
Support MDX (or MDX.js) expressions.
141140
The exports are extensions, respectively for

0 commit comments

Comments
 (0)