Skip to content

Commit efad048

Browse files
committed
Use ESM
1 parent 3fd7db9 commit efad048

File tree

8 files changed

+65
-89
lines changed

8 files changed

+65
-89
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
mdast-util-to-nlcst.js
7-
mdast-util-to-nlcst.min.js
85
yarn.lock

.prettierignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
mdast-util-to-nlcst.js
3-
mdast-util-to-nlcst.min.js
4-
*.json
52
*.md

index.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
'use strict'
2-
3-
var toString = require('nlcst-to-string')
4-
var repeat = require('repeat-string')
5-
var position = require('unist-util-position')
6-
var vfileLocation = require('vfile-location')
7-
8-
module.exports = toNlcst
1+
import repeat from 'repeat-string'
2+
import {toString} from 'nlcst-to-string'
3+
import {pointStart, pointEnd} from 'unist-util-position'
4+
import vfileLocation from 'vfile-location'
95

106
// Transform a `tree` in mdast to nlcst.
11-
function toNlcst(tree, file, Parser, options) {
7+
export function toNlcst(tree, file, Parser, options) {
128
var settings = options || {}
139
var parser
1410

@@ -44,7 +40,7 @@ function toNlcst(tree, file, Parser, options) {
4440
{
4541
doc: String(file),
4642
location: vfileLocation(file),
47-
parser: parser,
43+
parser,
4844
ignore: [].concat(
4945
'table',
5046
'tableRow',
@@ -63,13 +59,13 @@ function one(config, node) {
6359
var start
6460
var end
6561

66-
if (config.ignore.indexOf(node.type) < 0) {
62+
if (!config.ignore.includes(node.type)) {
6763
// To do: next major — nodes should have offsets, so
6864
// `config.location.toOffset` should be superfluous.
69-
start = config.location.toOffset(position.start(node))
70-
end = config.location.toOffset(position.end(node))
65+
start = config.location.toOffset(pointStart(node))
66+
end = config.location.toOffset(pointEnd(node))
7167

72-
if (config.source.indexOf(node.type) > -1) {
68+
if (config.source.includes(node.type)) {
7369
return patch(
7470
config,
7571
[config.parser.tokenizeSource(config.doc.slice(start, end))],
@@ -107,7 +103,7 @@ function all(config, parent) {
107103

108104
while (++index < parent.children.length) {
109105
child = parent.children[index]
110-
start = position.start(child)
106+
start = pointStart(child)
111107

112108
if (end && start.line !== end.line) {
113109
lineEnding = config.parser.tokenizeWhiteSpace(
@@ -123,7 +119,7 @@ function all(config, parent) {
123119
}
124120

125121
result = result.concat(one(config, child) || [])
126-
end = position.end(child)
122+
end = pointEnd(child)
127123
}
128124

129125
return result

package.json

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,39 @@
2525
"contributors": [
2626
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2727
],
28+
"sideEffects": false,
29+
"type": "module",
30+
"main": "index.js",
2831
"files": [
2932
"index.js"
3033
],
3134
"dependencies": {
32-
"nlcst-to-string": "^2.0.0",
35+
"nlcst-to-string": "^3.0.0",
3336
"repeat-string": "^1.0.0",
34-
"unist-util-position": "^3.0.0",
37+
"unist-util-position": "^4.0.0",
3538
"vfile-location": "^3.1.0"
3639
},
3740
"devDependencies": {
38-
"browserify": "^17.0.0",
39-
"is-hidden": "^1.0.0",
40-
"nyc": "^15.0.0",
41-
"parse-dutch": "^4.0.0",
42-
"parse-english": "^4.0.0",
43-
"parse-latin": "^4.0.0",
41+
"c8": "^7.0.0",
42+
"is-hidden": "^2.0.0",
43+
"parse-dutch": "^5.0.0",
44+
"parse-english": "^5.0.0",
45+
"parse-latin": "^5.0.0",
4446
"prettier": "^2.0.0",
4547
"remark": "^13.0.0",
4648
"remark-cli": "^9.0.0",
4749
"remark-frontmatter": "^3.0.0",
4850
"remark-gfm": "^1.0.0",
4951
"remark-preset-wooorm": "^8.0.0",
5052
"tape": "^5.0.0",
51-
"tinyify": "^3.0.0",
5253
"to-vfile": "^6.1.0",
53-
"xo": "^0.38.0"
54+
"xo": "^0.39.0"
5455
},
5556
"scripts": {
56-
"format": "remark *.md -qfo && prettier . --write && xo --fix",
57-
"build-bundle": "browserify . -s mdastUtilToNlcst -o mdast-util-to-nlcst.js",
58-
"build-mangle": "browserify . -s mdastUtilToNlcst -o mdast-util-to-nlcst.min.js -p tinyify",
59-
"build": "npm run build-bundle && npm run build-mangle",
60-
"test-api": "node test",
61-
"test-coverage": "nyc --reporter lcov tape test/index.js",
62-
"test": "npm run format && npm run build && npm run test-coverage"
63-
},
64-
"nyc": {
65-
"check-coverage": true,
66-
"lines": 100,
67-
"functions": 100,
68-
"branches": 100
57+
"format": "remark . -qfo --ignore-pattern test/ && prettier . -w --loglevel warn && xo --fix",
58+
"test-api": "node test/index.js",
59+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
60+
"test": "npm run format && npm run test-coverage"
6961
},
7062
"prettier": {
7163
"tabWidth": 2,
@@ -77,15 +69,10 @@
7769
},
7870
"xo": {
7971
"prettier": true,
80-
"esnext": false,
8172
"rules": {
82-
"unicorn/no-fn-reference-in-iterator": "off",
83-
"unicorn/prefer-includes": "off",
84-
"unicorn/prefer-optional-catch-binding": "off"
85-
},
86-
"ignores": [
87-
"mdast-util-to-nlcst.js"
88-
]
73+
"no-var": "off",
74+
"prefer-arrow-callback": "off"
75+
}
8976
},
9077
"remarkConfig": {
9178
"plugins": [

readme.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
1515
## Install
1616

17+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
18+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
19+
1720
[npm][]:
1821

1922
```sh
@@ -23,16 +26,15 @@ npm install mdast-util-to-nlcst
2326
## Use
2427

2528
```js
26-
var toNlcst = require('mdast-util-to-nlcst')
27-
var inspect = require('unist-util-inspect')
28-
var English = require('parse-english')
29-
var remark = require('remark')
30-
var vfile = require('vfile')
29+
import vfile from 'vfile'
30+
import {ParseEnglish} from 'parse-english'
31+
import {inspect} from 'unist-util-inspect'
32+
import fromMarkdown from 'mdast-util-from-markdown'
33+
import {toNlcst} from 'mdast-util-to-nlcst'
3134

3235
var file = vfile('Some *foo*sball.')
33-
var mdast = remark().parse(file)
34-
35-
var nlcst = toNlcst(mdast, file, English)
36+
var mdast = fromMarkdown(file)
37+
var nlcst = toNlcst(mdast, file, ParseEnglish)
3638

3739
console.log(inspect(nlcst))
3840
```
@@ -54,6 +56,9 @@ RootNode[1] (1:1-1:17, 0-16)
5456

5557
## API
5658

59+
This package exports the following identifiers: `toNlcst`.
60+
There is no default export.
61+
5762
### `toNlcst(tree, file, Parser[, options])`
5863

5964
Transform a [tree][] in [mdast][], with a corresponding [virtual file][vfile],
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"ignore": [
3-
"blockquote"
4-
]
2+
"ignore": ["blockquote"]
53
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"source": [
3-
"blockquote"
4-
]
2+
"source": ["blockquote"]
53
}

test/index.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict'
2-
3-
var fs = require('fs')
4-
var path = require('path')
5-
var test = require('tape')
6-
var remark = require('remark')
7-
var gfm = require('remark-gfm')
8-
var frontmatter = require('remark-frontmatter')
9-
var vfile = require('to-vfile')
10-
var Latin = require('parse-latin')
11-
var Dutch = require('parse-dutch')
12-
var English = require('parse-english')
13-
var isHidden = require('is-hidden')
14-
var toNlcst = require('..')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import test from 'tape'
4+
import remark from 'remark'
5+
import gfm from 'remark-gfm'
6+
import frontmatter from 'remark-frontmatter'
7+
import vfile from 'to-vfile'
8+
import {ParseLatin} from 'parse-latin'
9+
import {ParseDutch} from 'parse-dutch'
10+
import {ParseEnglish} from 'parse-english'
11+
import {isHidden} from 'is-hidden'
12+
import {toNlcst} from '../index.js'
1513

1614
test('mdast-util-to-nlcst', function (t) {
1715
t.throws(
@@ -64,7 +62,7 @@ test('mdast-util-to-nlcst', function (t) {
6462

6563
t.throws(
6664
function () {
67-
toNlcst({type: 'text', value: 'foo'}, vfile(), Latin)
65+
toNlcst({type: 'text', value: 'foo'}, vfile(), ParseLatin)
6866
},
6967
/mdast-util-to-nlcst expected position on nodes/,
7068
'should fail when not given positional information'
@@ -78,7 +76,7 @@ test('mdast-util-to-nlcst', function (t) {
7876
position: {start: {line: 1, column: 1}, end: {line: 1, column: 4}}
7977
},
8078
vfile(),
81-
English
79+
ParseEnglish
8280
)
8381
}, 'should accept a parser constructor')
8482

@@ -90,7 +88,7 @@ test('mdast-util-to-nlcst', function (t) {
9088
position: {start: {line: 1, column: 1}, end: {line: 1, column: 4}}
9189
},
9290
vfile(),
93-
new Dutch()
91+
new ParseDutch()
9492
)
9593
}, 'should accept a parser instance')
9694

@@ -103,7 +101,7 @@ test('mdast-util-to-nlcst', function (t) {
103101
position: {start: {}, end: {}}
104102
},
105103
vfile(),
106-
Latin
104+
ParseLatin
107105
)
108106
},
109107
/mdast-util-to-nlcst expected position on nodes/,
@@ -118,7 +116,7 @@ test('mdast-util-to-nlcst', function (t) {
118116
position: {start: {line: 1, column: 1}, end: {line: 1, column: 4}}
119117
},
120118
vfile({contents: 'foo'}),
121-
Latin
119+
ParseLatin
122120
)
123121

124122
st.equal(node.position.start.offset, 0, 'should set starting offset')
@@ -131,7 +129,7 @@ test('mdast-util-to-nlcst', function (t) {
131129
})
132130

133131
test('Fixtures', function (t) {
134-
var base = path.join(__dirname, 'fixtures')
132+
var base = path.join('test', 'fixtures')
135133
var files = fs.readdirSync(base)
136134
var index = -1
137135
var name
@@ -152,7 +150,7 @@ test('Fixtures', function (t) {
152150
options = JSON.parse(
153151
vfile.readSync(path.join(base, name, 'options.json'))
154152
)
155-
} catch (_) {
153+
} catch {
156154
options = null
157155
}
158156

@@ -162,7 +160,7 @@ test('Fixtures', function (t) {
162160
.parse(input)
163161

164162
t.deepEqual(
165-
toNlcst(mdast, input, Latin, options),
163+
toNlcst(mdast, input, ParseLatin, options),
166164
expected,
167165
'should work on `' + name + '`'
168166
)

0 commit comments

Comments
 (0)