Skip to content

Commit a074a94

Browse files
committed
Use ESM
1 parent 5b4942c commit a074a94

File tree

6 files changed

+81
-93
lines changed

6 files changed

+81
-93
lines changed

Diff for: .gitignore

-3
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-
hast-util-to-nlcst.js
7-
hast-util-to-nlcst.min.js
85
yarn.lock

Diff for: .prettierignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
coverage/
2-
hast-util-to-nlcst.js
3-
hast-util-to-nlcst.min.js
42
*.html
5-
*.json
63
*.md

Diff for: index.js

+26-23
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
'use strict'
2-
3-
var embedded = require('hast-util-embedded')
4-
var convert = require('hast-util-is-element/convert')
5-
var phrasing = require('hast-util-phrasing')
6-
var textContent = require('hast-util-to-string')
7-
var whitespace = require('hast-util-whitespace')
8-
var toString = require('nlcst-to-string')
9-
var position = require('unist-util-position')
10-
var vfileLocation = require('vfile-location')
11-
12-
module.exports = toNlcst
1+
import {embedded} from 'hast-util-embedded'
2+
import {convertElement} from 'hast-util-is-element'
3+
import {phrasing} from 'hast-util-phrasing'
4+
import toString from 'hast-util-to-string'
5+
import {whitespace} from 'hast-util-whitespace'
6+
import {toString as nlcstToString} from 'nlcst-to-string'
7+
import {pointStart} from 'unist-util-position'
8+
import vfileLocation from 'vfile-location'
139

1410
var push = [].push
1511

16-
var source = convert(['code', dataNlcstSourced])
17-
var ignore = convert(['script', 'style', 'svg', 'math', 'del', dataNlcstIgnore])
18-
var explicit = convert(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
12+
var source = convertElement(['code', dataNlcstSourced])
13+
var ignore = convertElement([
14+
'script',
15+
'style',
16+
'svg',
17+
'math',
18+
'del',
19+
dataNlcstIgnore
20+
])
21+
var explicit = convertElement(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
1922

20-
var flowAccepting = convert([
23+
var flowAccepting = convertElement([
2124
'body',
2225
'article',
2326
'section',
@@ -44,10 +47,10 @@ var flowAccepting = convert([
4447
])
4548

4649
// See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>
47-
var unravelInParagraph = convert(['a', 'ins', 'del', 'map'])
50+
var unravelInParagraph = convertElement(['a', 'ins', 'del', 'map'])
4851

4952
// Transform `tree` to nlcst.
50-
function toNlcst(tree, file, Parser) {
53+
export function toNlcst(tree, file, Parser) {
5154
var parser
5255
var location
5356
var results
@@ -67,7 +70,7 @@ function toNlcst(tree, file, Parser) {
6770
throw new Error('hast-util-to-nlcst expected parser')
6871
}
6972

70-
if (!position.start(tree).line || !position.start(tree).column) {
73+
if (!pointStart(tree).line || !pointStart(tree).column) {
7174
throw new Error('hast-util-to-nlcst expected position on nodes')
7275
}
7376

@@ -130,7 +133,7 @@ function toNlcst(tree, file, Parser) {
130133
function add(node) {
131134
var result = ('length' in node ? all : one)(node)
132135

133-
if (result.length) {
136+
if (result.length > 0) {
134137
results.push(parser.tokenizeParagraph(result))
135138
}
136139
}
@@ -182,15 +185,15 @@ function toNlcst(tree, file, Parser) {
182185
replacement = [parser.tokenizeWhiteSpace('\n')]
183186
change = true
184187
} else if (source(node)) {
185-
replacement = [parser.tokenizeSource(textContent(node))]
188+
replacement = [parser.tokenizeSource(toString(node))]
186189
change = true
187190
} else {
188191
replacement = all(node.children)
189192
}
190193
}
191194

192195
return change
193-
? patch(replacement, location, location.toOffset(position.start(node)))
196+
? patch(replacement, location, location.toOffset(pointStart(node)))
194197
: replacement
195198
}
196199

@@ -224,7 +227,7 @@ function toNlcst(tree, file, Parser) {
224227
patch(node.children, location, start)
225228
}
226229

227-
end = start + toString(node).length
230+
end = start + nlcstToString(node).length
228231

229232
node.position = {
230233
start: location.toPoint(start),

Diff for: package.json

+21-35
Original file line numberDiff line numberDiff line change
@@ -26,50 +26,41 @@
2626
"contributors": [
2727
"Titus Wormer <[email protected]> (https://wooorm.com)"
2828
],
29+
"sideEffects": false,
30+
"type": "module",
31+
"main": "index.js",
2932
"files": [
3033
"index.js"
3134
],
3235
"dependencies": {
33-
"hast-util-embedded": "^1.0.0",
34-
"hast-util-is-element": "^1.0.0",
35-
"hast-util-phrasing": "^1.0.0",
36+
"hast-util-embedded": "^2.0.0",
37+
"hast-util-is-element": "^2.0.0",
38+
"hast-util-phrasing": "^2.0.0",
3639
"hast-util-to-string": "^1.0.0",
37-
"hast-util-whitespace": "^1.0.0",
38-
"nlcst-to-string": "^2.0.0",
39-
"unist-util-position": "^3.0.0",
40+
"hast-util-whitespace": "^2.0.0",
41+
"nlcst-to-string": "^3.0.0",
42+
"unist-util-position": "^4.0.0",
4043
"vfile-location": "^3.1.0"
4144
},
4245
"devDependencies": {
43-
"browserify": "^17.0.0",
44-
"is-hidden": "^1.0.0",
45-
"negate": "^1.0.0",
46-
"nyc": "^15.0.0",
47-
"parse-dutch": "^4.0.0",
48-
"parse-english": "^4.0.0",
49-
"parse-latin": "^4.0.0",
46+
"c8": "^7.0.0",
47+
"is-hidden": "^2.0.0",
48+
"parse-dutch": "^5.0.0",
49+
"parse-english": "^5.0.0",
50+
"parse-latin": "^5.0.0",
5051
"prettier": "^2.0.0",
5152
"rehype": "^11.0.0",
5253
"remark-cli": "^9.0.0",
5354
"remark-preset-wooorm": "^8.0.0",
5455
"tape": "^5.0.0",
55-
"tinyify": "^3.0.0",
5656
"vfile": "^4.0.0",
57-
"xo": "^0.38.0"
57+
"xo": "^0.39.0"
5858
},
5959
"scripts": {
6060
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
61-
"build-bundle": "browserify . -s hastUtilToNlcst -o hast-util-to-nlcst.js",
62-
"build-mangle": "browserify . -s hastUtilToNlcst -o hast-util-to-nlcst.min.js -p tinyify",
63-
"build": "npm run build-bundle && npm run build-mangle",
64-
"test-api": "node test",
65-
"test-coverage": "nyc --reporter lcov tape test/index.js",
66-
"test": "npm run format && npm run build && npm run test-coverage"
67-
},
68-
"nyc": {
69-
"check-coverage": true,
70-
"lines": 100,
71-
"functions": 100,
72-
"branches": 100
61+
"test-api": "node test/index.js",
62+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
63+
"test": "npm run format && npm run test-coverage"
7364
},
7465
"prettier": {
7566
"tabWidth": 2,
@@ -81,15 +72,10 @@
8172
},
8273
"xo": {
8374
"prettier": true,
84-
"esnext": false,
8575
"rules": {
86-
"unicorn/explicit-length-check": "off",
87-
"unicorn/no-fn-reference-in-iterator": "off",
88-
"unicorn/prefer-optional-catch-binding": "off"
89-
},
90-
"ignores": [
91-
"hast-util-to-nlcst.js"
92-
]
76+
"no-var": "off",
77+
"prefer-arrow-callback": "off"
78+
}
9379
},
9480
"remarkConfig": {
9581
"plugins": [

Diff for: readme.md

+12-6
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
@@ -35,16 +38,16 @@ Say we have the following `example.html`:
3538
…and next to it, `index.js`:
3639

3740
```js
38-
var rehype = require('rehype')
39-
var vfile = require('to-vfile')
40-
var English = require('parse-english')
41-
var inspect = require('unist-util-inspect')
42-
var toNlcst = require('hast-util-to-nlcst')
41+
import rehype from 'rehype'
42+
import vfile from 'to-vfile'
43+
import {ParseEnglish} from 'parse-english'
44+
import {inspect} from 'unist-util-inspect'
45+
import {toNlcst} from 'hast-util-to-nlcst'
4346

4447
var file = vfile.readSync('example.html')
4548
var tree = rehype().parse(file)
4649

47-
console.log(inspect(toNlcst(tree, file, English)))
50+
console.log(inspect(toNlcst(tree, file, ParseEnglish)))
4851
```
4952

5053
Which, when running, yields:
@@ -73,6 +76,9 @@ RootNode[2] (1:1-6:1, 0-134)
7376

7477
## API
7578

79+
This package exports the following identifiers: `toNlcst`.
80+
There is no default export.
81+
7682
### `toNlcst(tree, file, Parser)`
7783

7884
Transform the given [**hast**][hast] [*tree*][tree] to [**nlcst**][nlcst].

Diff for: test/index.js

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
'use strict'
2-
3-
var fs = require('fs')
4-
var path = require('path')
5-
var test = require('tape')
6-
var rehype = require('rehype')
7-
var vfile = require('vfile')
8-
var Latin = require('parse-latin')
9-
var Dutch = require('parse-dutch')
10-
var English = require('parse-english')
11-
var negate = require('negate')
12-
var hidden = require('is-hidden')
13-
var toNlcst = require('..')
1+
import fs from 'fs'
2+
import path from 'path'
3+
import test from 'tape'
4+
import rehype from 'rehype'
5+
import vfile from 'vfile'
6+
import {ParseLatin} from 'parse-latin'
7+
import {ParseDutch} from 'parse-dutch'
8+
import {ParseEnglish} from 'parse-english'
9+
import {isHidden} from 'is-hidden'
10+
import {toNlcst} from '../index.js'
1411

1512
test('hast-util-to-nlcst', function (t) {
1613
t.throws(
@@ -63,7 +60,7 @@ test('hast-util-to-nlcst', function (t) {
6360

6461
t.throws(
6562
function () {
66-
toNlcst({type: 'text', value: 'foo'}, vfile(), Latin)
63+
toNlcst({type: 'text', value: 'foo'}, vfile(), ParseLatin)
6764
},
6865
/hast-util-to-nlcst expected position on nodes/,
6966
'should fail when not given positional information'
@@ -80,7 +77,7 @@ test('hast-util-to-nlcst', function (t) {
8077
}
8178
},
8279
vfile(),
83-
English
80+
ParseEnglish
8481
)
8582
}, 'should accept a parser constructor')
8683

@@ -95,7 +92,7 @@ test('hast-util-to-nlcst', function (t) {
9592
}
9693
},
9794
vfile(),
98-
new Dutch()
95+
new ParseDutch()
9996
)
10097
}, 'should accept a parser instance')
10198

@@ -108,7 +105,7 @@ test('hast-util-to-nlcst', function (t) {
108105
position: {start: {}, end: {}}
109106
},
110107
vfile(),
111-
Latin
108+
ParseLatin
112109
)
113110
},
114111
/hast-util-to-nlcst expected position on nodes/,
@@ -126,7 +123,7 @@ test('hast-util-to-nlcst', function (t) {
126123
}
127124
},
128125
vfile('foo'),
129-
Latin
126+
ParseLatin
130127
)
131128

132129
st.equal(node.position.start.offset, 0, 'should set starting offset')
@@ -143,7 +140,7 @@ test('hast-util-to-nlcst', function (t) {
143140
position: {start: {line: 1, column: 1}, end: {line: 1, column: 9}}
144141
},
145142
vfile('<!--a-->'),
146-
Latin
143+
ParseLatin
147144
)
148145

149146
st.deepEqual(
@@ -166,8 +163,8 @@ test('hast-util-to-nlcst', function (t) {
166163
})
167164

168165
test('Fixtures', function (t) {
169-
var root = path.join(__dirname, 'fixtures')
170-
var files = fs.readdirSync(root).filter(negate(hidden))
166+
var root = path.join('test', 'fixtures')
167+
var files = fs.readdirSync(root)
171168
var index = -1
172169
var input
173170
var output
@@ -176,14 +173,16 @@ test('Fixtures', function (t) {
176173
var expected
177174

178175
while (++index < files.length) {
176+
if (isHidden(files[index])) continue
177+
179178
input = path.join(root, files[index], 'input.html')
180179
output = path.join(root, files[index], 'output.json')
181180
file = vfile(fs.readFileSync(input))
182-
actual = toNlcst(rehype().parse(file), file, Latin)
181+
actual = toNlcst(rehype().parse(file), file, ParseLatin)
183182

184183
try {
185184
expected = JSON.parse(fs.readFileSync(output))
186-
} catch (_) {
185+
} catch {
187186
fs.writeFileSync(output, JSON.stringify(actual, null, 2) + '\n')
188187
return
189188
}

0 commit comments

Comments
 (0)