Skip to content

Commit 20528b4

Browse files
committed
Use ESM
1 parent 10e5333 commit 20528b4

File tree

6 files changed

+50
-68
lines changed

6 files changed

+50
-68
lines changed

.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-
mdast-util-heading-range.js
7-
mdast-util-heading-range.min.js
85
yarn.lock

.prettierignore

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
mdast-util-heading-range.js
3-
mdast-util-heading-range.min.js
4-
*.json
52
*.md

index.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
'use strict'
2-
3-
var toString = require('mdast-util-to-string')
4-
5-
module.exports = headingRange
6-
7-
var splice = [].splice
1+
import {toString} from 'mdast-util-to-string'
82

93
// Search `node` with `options` and invoke `callback`.
10-
function headingRange(node, options, callback) {
4+
// eslint-disable-next-line complexity
5+
export function headingRange(node, options, callback) {
116
var test = options
127
var children = node.children
138
var index = -1
@@ -36,7 +31,7 @@ function headingRange(node, options, callback) {
3631
}
3732

3833
if (typeof test !== 'function') {
39-
throw new Error(
34+
throw new TypeError(
4035
'Expected `string`, `regexp`, or `function` for `test`, not `' +
4136
test +
4237
'`'
@@ -79,7 +74,7 @@ function headingRange(node, options, callback) {
7974
children[end],
8075
{
8176
parent: node,
82-
start: start,
77+
start,
8378
end: children[end] ? end : null
8479
}
8580
)
@@ -94,7 +89,7 @@ function headingRange(node, options, callback) {
9489
if (nodes[index]) result.push(nodes[index])
9590
}
9691

97-
splice.apply(children, [start, end - start + 1].concat(result))
92+
children.splice(start, end - start + 1, ...result)
9893
}
9994
}
10095
}

package.json

+12-24
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,29 @@
2424
"Titus Wormer <[email protected]> (https://wooorm.com)",
2525
"Jake Teton-Landis <[email protected]>"
2626
],
27+
"sideEffects": false,
28+
"type": "module",
29+
"main": "index.js",
2730
"files": [
2831
"index.js"
2932
],
3033
"dependencies": {
31-
"mdast-util-to-string": "^1.0.0"
34+
"mdast-util-to-string": "^3.0.0"
3235
},
3336
"devDependencies": {
34-
"browserify": "^17.0.0",
35-
"nyc": "^15.0.0",
37+
"c8": "^7.0.0",
3638
"prettier": "^2.0.0",
3739
"remark": "^13.0.0",
3840
"remark-cli": "^9.0.0",
3941
"remark-preset-wooorm": "^8.0.0",
4042
"tape": "^5.0.0",
41-
"tinyify": "^3.0.0",
42-
"xo": "^0.38.0"
43+
"xo": "^0.39.0"
4344
},
4445
"scripts": {
4546
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
46-
"build-bundle": "browserify . -s mdastUtilHeadingRange -o mdast-util-heading-range.js",
47-
"build-mangle": "browserify . -s mdastUtilHeadingRange -o mdast-util-heading-range.min.js -p tinyify",
48-
"build": "npm run build-bundle && npm run build-mangle",
49-
"test-api": "node test",
50-
"test-coverage": "nyc --reporter lcov tape test.js",
51-
"test": "npm run format && npm run build && npm run test-coverage"
52-
},
53-
"nyc": {
54-
"check-coverage": true,
55-
"lines": 100,
56-
"functions": 100,
57-
"branches": 100
47+
"test-api": "node test.js",
48+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
49+
"test": "npm run format && npm run test-coverage"
5850
},
5951
"prettier": {
6052
"tabWidth": 2,
@@ -66,14 +58,10 @@
6658
},
6759
"xo": {
6860
"prettier": true,
69-
"esnext": false,
7061
"rules": {
71-
"complexity": "off",
72-
"unicorn/prefer-type-error": "off"
73-
},
74-
"ignores": [
75-
"mdast-util-heading-range.js"
76-
]
62+
"no-var": "off",
63+
"prefer-arrow-callback": "off"
64+
}
7765
},
7866
"remarkConfig": {
7967
"plugins": [

readme.md

+19-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
## Install
1414

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

1720
```sh
@@ -33,9 +36,9 @@ Bar.
3336
And our script, `example.js`, looks as follows:
3437

3538
```js
36-
var vfile = require('to-vfile')
37-
var remark = require('remark')
38-
var heading = require('mdast-util-heading-range')
39+
import toVFile from 'to-vfile'
40+
import remark from 'remark'
41+
import {headingRange} from 'mdast-util-heading-range'
3942

4043
remark()
4144
.use(plugin)
@@ -48,10 +51,10 @@ function plugin() {
4851
return transform
4952

5053
function transform(tree) {
51-
heading(tree, 'foo', mutate)
54+
headingRange(tree, 'foo', handler)
5255
}
5356

54-
function mutate(start, nodes, end) {
57+
function handler(start, nodes, end) {
5558
return [
5659
start,
5760
{type: 'paragraph', children: [{type: 'text', value: 'Qux.'}]},
@@ -73,10 +76,13 @@ Qux.
7376

7477
## API
7578

76-
### `heading(tree, test|options, onrun)`
79+
This package exports the following identifiers: `headingRange`.
80+
There is no default export.
81+
82+
### `headingRange(tree, test|options, handler)`
7783

7884
Search `tree` ([`Node`][node]) and transform a section without affecting other
79-
parts with `onrun` ([`Function`][onrun]).
85+
parts with `handler` ([`Function`][handler]).
8086
A “section” is a heading that passes `test`, until the next heading of the same
8187
or lower depth, or the end of the document.
8288
If `ignoreFinalDefinitions: true`, final definitions “in” the section are
@@ -103,7 +109,7 @@ itself ([`Heading`][heading]) to check if it’s the one to look for.
103109

104110
`Boolean?`, `true` if this is the heading to use.
105111

106-
#### `function onrun(start, nodes, end?, scope)`
112+
#### `function handler(start, nodes, end?, scope)`
107113

108114
Callback invoked when a range is found.
109115

@@ -131,14 +137,14 @@ Extra info (`Object`):
131137

132138
## Security
133139

134-
Improper use of `onrun` can open you up to a [cross-site scripting (XSS)][xss]
140+
Improper use of `handler` can open you up to a [cross-site scripting (XSS)][xss]
135141
attack as the value it returns is injected into the syntax tree.
136142
This can become a problem if the tree is later transformed to [**hast**][hast].
137143
The following example shows how a script is injected that could run when loaded
138144
in a browser.
139145

140146
```js
141-
function onrun(start, nodes, end) {
147+
function handler(start, nodes, end) {
142148
return [start, {type: 'html', value: 'alert(1)'}, end]
143149
}
144150
```
@@ -153,7 +159,8 @@ Yields:
153159
# Baz
154160
```
155161

156-
Either do not use user input in `onrun` or use [`hast-util-santize`][sanitize].
162+
Either do not use user input in `handler` or use
163+
[`hast-util-santize`][sanitize].
157164

158165
## Related
159166

@@ -218,7 +225,7 @@ abide by its terms.
218225

219226
[node]: https://github.com/syntax-tree/unist#node
220227

221-
[onrun]: #function-onrunstart-nodes-end-scope
228+
[handler]: #function-handlerstart-nodes-end-scope
222229

223230
[heading]: https://github.com/syntax-tree/mdast#heading
224231

test.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var remark = require('remark')
5-
var heading = require('.')
1+
import test from 'tape'
2+
import remark from 'remark'
3+
import {headingRange} from './index.js'
64

75
test('mdast-util-heading-range()', function (t) {
86
t.plan(57)
97

10-
t.equal(typeof heading, 'function', 'should be a function')
8+
t.equal(typeof headingRange, 'function', 'should be a function')
119

1210
t.throws(
1311
function () {
14-
heading({type: 'root', chilren: []}, null, function () {})
12+
headingRange({type: 'root', chilren: []}, null, function () {})
1513
},
16-
/^Error: Expected `string`, `regexp`, or `function` for `test`, not `null`$/,
14+
/^TypeError: Expected `string`, `regexp`, or `function` for `test`, not `null`$/,
1715
'should throw when `null` is passed in'
1816
)
1917

2018
t.throws(
2119
function () {
22-
heading({type: 'root', chilren: []}, undefined, function () {})
20+
headingRange({type: 'root', chilren: []}, undefined, function () {})
2321
},
24-
/^Error: Expected `string`, `regexp`, or `function` for `test`, not `undefined`$/,
22+
/^TypeError: Expected `string`, `regexp`, or `function` for `test`, not `undefined`$/,
2523
'should throw when `undefined` is passed in'
2624
)
2725

@@ -121,7 +119,7 @@ test('mdast-util-heading-range()', function (t) {
121119
remark()
122120
.use(function () {
123121
return function (node) {
124-
heading(node, 'foo', function () {
122+
headingRange(node, 'foo', function () {
125123
return null
126124
})
127125
}
@@ -142,7 +140,7 @@ test('mdast-util-heading-range()', function (t) {
142140
remark()
143141
.use(function () {
144142
return function (node) {
145-
heading(node, 'foo', function () {
143+
headingRange(node, 'foo', function () {
146144
return []
147145
})
148146
}
@@ -163,7 +161,7 @@ test('mdast-util-heading-range()', function (t) {
163161
remark()
164162
.use(function () {
165163
return function (node) {
166-
heading(node, 'foo', function (start, nodes, end) {
164+
headingRange(node, 'foo', function (start, nodes, end) {
167165
return [start, {type: 'thematicBreak'}, end]
168166
})
169167
}
@@ -184,7 +182,7 @@ test('mdast-util-heading-range()', function (t) {
184182
remark()
185183
.use(function () {
186184
return function (node) {
187-
heading(node, 'foo', function (start, nodes, end) {
185+
headingRange(node, 'foo', function (start, nodes, end) {
188186
t.equal(nodes.length, 3)
189187

190188
return [start].concat(nodes, end)
@@ -309,7 +307,7 @@ function process(t, value, options) {
309307
return remark()
310308
.use(function () {
311309
return function (node) {
312-
heading(node, options, function (start, nodes, end, scope) {
310+
headingRange(node, options, function (start, nodes, end, scope) {
313311
t.equal(typeof scope.start, 'number')
314312
t.assert(typeof scope.end === 'number' || scope.end === null)
315313
t.equal(scope.parent.type, 'root')

0 commit comments

Comments
 (0)