Skip to content

Commit 1377fe8

Browse files
committed
Use ESM
1 parent 43b7843 commit 1377fe8

File tree

6 files changed

+73
-91
lines changed

6 files changed

+73
-91
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-
hast-util-raw.js
7-
hast-util-raw.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-
hast-util-raw.js
3-
hast-util-raw.min.js
4-
*.json
52
*.md

index.js

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
'use strict'
2-
3-
var Parser = require('parse5/lib/parser')
4-
var pos = require('unist-util-position')
5-
var visit = require('unist-util-visit')
6-
var fromParse5 = require('hast-util-from-parse5')
7-
var toParse5 = require('hast-util-to-parse5')
8-
var voids = require('html-void-elements')
9-
var ns = require('web-namespaces')
10-
var zwitch = require('zwitch')
11-
var xtend = require('xtend')
12-
13-
module.exports = wrap
1+
import Parser from 'parse5/lib/parser/index.js'
2+
import {pointStart, pointEnd} from 'unist-util-position'
3+
import {visit} from 'unist-util-visit'
4+
import {fromParse5} from 'hast-util-from-parse5'
5+
import {toParse5} from 'hast-util-to-parse5'
6+
import {htmlVoidElements} from 'html-void-elements'
7+
import {webNamespaces} from 'web-namespaces'
8+
import {zwitch} from 'zwitch'
149

1510
var inTemplateMode = 'IN_TEMPLATE_MODE'
1611
var dataState = 'DATA_STATE'
@@ -22,18 +17,18 @@ var doctypeToken = 'DOCTYPE_TOKEN'
2217

2318
var parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}
2419

25-
function wrap(tree, file, options) {
20+
export function raw(tree, file, options) {
2621
var parser = new Parser(parseOptions)
2722
var one = zwitch('type', {
2823
handlers: {
29-
root: root,
30-
element: element,
31-
text: text,
32-
comment: comment,
33-
doctype: doctype,
34-
raw: raw
24+
root,
25+
element,
26+
text,
27+
comment,
28+
doctype,
29+
raw: handleRaw
3530
},
36-
unknown: unknown
31+
unknown
3732
})
3833
var stitches
3934
var tokenizer
@@ -81,14 +76,14 @@ function wrap(tree, file, options) {
8176
nodeName: 'template',
8277
tagName: 'template',
8378
attrs: [],
84-
namespaceURI: ns.html,
79+
namespaceURI: webNamespaces.html,
8580
childNodes: []
8681
}
8782
var mock = {
8883
nodeName: 'documentmock',
8984
tagName: 'documentmock',
9085
attrs: [],
91-
namespaceURI: ns.html,
86+
namespaceURI: webNamespaces.html,
9287
childNodes: []
9388
}
9489
var doc = {nodeName: '#document-fragment', childNodes: []}
@@ -143,11 +138,11 @@ function wrap(tree, file, options) {
143138

144139
function element(node) {
145140
resetTokenizer()
146-
parser._processToken(startTag(node), ns.html)
141+
parser._processToken(startTag(node), webNamespaces.html)
147142

148143
all(node.children)
149144

150-
if (voids.indexOf(node.tagName) < 0) {
145+
if (!htmlVoidElements.includes(node.tagName)) {
151146
resetTokenizer()
152147
parser._processToken(endTag(node))
153148
}
@@ -184,8 +179,8 @@ function wrap(tree, file, options) {
184179
})
185180
}
186181

187-
function raw(node) {
188-
var start = pos.start(node)
182+
function handleRaw(node) {
183+
var start = pointStart(node)
189184
var line = start.line || 1
190185
var column = start.column || 1
191186
var offset = start.offset || 0
@@ -242,7 +237,7 @@ function wrap(tree, file, options) {
242237
// Recurse, because to somewhat handle `[<x>]</x>` (where `[]` denotes the
243238
// passed through node).
244239
if (node.children) {
245-
clone.children = wrap(
240+
clone.children = raw(
246241
{type: 'root', children: node.children},
247242
file,
248243
options
@@ -280,14 +275,14 @@ function wrap(tree, file, options) {
280275
function startTag(node) {
281276
var location = createParse5Location(node)
282277

283-
location.startTag = xtend(location)
278+
location.startTag = Object.assign({}, location)
284279

285280
return {
286281
type: startTagToken,
287282
tagName: node.tagName,
288283
selfClosing: false,
289284
attrs: attributes(node),
290-
location: location
285+
location
291286
}
292287
}
293288

@@ -302,13 +297,13 @@ function attributes(node) {
302297
function endTag(node) {
303298
var location = createParse5Location(node)
304299

305-
location.endTag = xtend(location)
300+
location.endTag = Object.assign({}, location)
306301

307302
return {
308303
type: endTagToken,
309304
tagName: node.tagName,
310305
attrs: [],
311-
location: location
306+
location
312307
}
313308
}
314309

@@ -323,8 +318,8 @@ function documentMode(node) {
323318
}
324319

325320
function createParse5Location(node) {
326-
var start = pos.start(node)
327-
var end = pos.end(node)
321+
var start = pointStart(node)
322+
var end = pointEnd(node)
328323

329324
return {
330325
startLine: start.line,

package.json

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,45 @@
2424
"Titus Wormer <[email protected]> (https://wooorm.com)",
2525
"Christian Murphy <[email protected]>"
2626
],
27+
"sideEffects": false,
28+
"type": "module",
29+
"main": "index.js",
30+
"types": "types/index.d.ts",
2731
"files": [
28-
"index.js",
29-
"types/index.d.ts"
32+
"types/index.d.ts",
33+
"index.js"
3034
],
31-
"types": "types/index.d.ts",
3235
"dependencies": {
3336
"@types/hast": "^2.0.0",
34-
"hast-util-from-parse5": "^6.0.0",
35-
"hast-util-to-parse5": "^6.0.0",
36-
"html-void-elements": "^1.0.0",
37+
"hast-util-from-parse5": "^7.0.0",
38+
"hast-util-to-parse5": "^7.0.0",
39+
"html-void-elements": "^2.0.0",
3740
"parse5": "^6.0.0",
38-
"unist-util-position": "^3.0.0",
39-
"unist-util-visit": "^2.0.0",
41+
"unist-util-position": "^4.0.0",
42+
"unist-util-visit": "^3.0.0",
4043
"vfile": "^4.0.0",
41-
"web-namespaces": "^1.0.0",
42-
"xtend": "^4.0.0",
43-
"zwitch": "^1.0.0"
44+
"web-namespaces": "^2.0.0",
45+
"zwitch": "^2.0.0"
4446
},
4547
"devDependencies": {
46-
"browserify": "^17.0.0",
47-
"dtslint": "^4.0.0",
48-
"hastscript": "^6.0.0",
49-
"nyc": "^15.0.0",
48+
"c8": "^7.0.0",
49+
"hastscript": "^7.0.0",
5050
"prettier": "^2.0.0",
5151
"rehype-stringify": "^8.0.0",
5252
"remark-cli": "^9.0.0",
5353
"remark-parse": "^9.0.0",
5454
"remark-preset-wooorm": "^8.0.0",
5555
"remark-rehype": "^8.0.0",
5656
"tape": "^5.0.0",
57-
"tinyify": "^3.0.0",
5857
"unified": "^9.0.0",
59-
"unist-builder": "^2.0.0",
60-
"xo": "^0.38.0"
58+
"unist-builder": "^3.0.0",
59+
"xo": "^0.39.0"
6160
},
6261
"scripts": {
6362
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
64-
"build-bundle": "browserify index.js -s hastUtilRaw -o hast-util-raw.js",
65-
"build-mangle": "browserify index.js -s hastUtilRaw -o hast-util-raw.min.js -p tinyify",
66-
"build": "npm run build-bundle && npm run build-mangle",
67-
"test-api": "node test",
68-
"test-coverage": "nyc --reporter lcov tape test.js",
69-
"test-types": "dtslint types",
70-
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
71-
},
72-
"nyc": {
73-
"check-coverage": true,
74-
"lines": 100,
75-
"functions": 100,
76-
"branches": 100
63+
"test-api": "node test.js",
64+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
65+
"test": "npm run format && npm run test-coverage"
7766
},
7867
"prettier": {
7968
"tabWidth": 2,
@@ -85,12 +74,12 @@
8574
},
8675
"xo": {
8776
"prettier": true,
88-
"esnext": false,
8977
"rules": {
90-
"unicorn/prefer-includes": "off"
78+
"no-var": "off",
79+
"prefer-arrow-callback": "off"
9180
},
92-
"ignores": [
93-
"hast-util-raw.js"
81+
"ignore": [
82+
"types/"
9483
]
9584
},
9685
"remarkConfig": {

readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ If you’re working with [**remark**][remark] and/or
2222

2323
## Install
2424

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

2730
```sh
@@ -31,8 +34,8 @@ npm install hast-util-raw
3134
## Use
3235

3336
```js
34-
var h = require('hastscript')
35-
var raw = require('hast-util-raw')
37+
import {h} from 'hastscript'
38+
import {raw} from 'hast-util-raw'
3639

3740
var tree = h('div', [h('h1', ['Foo ', h('h2', 'Bar'), ' Baz'])])
3841

@@ -61,6 +64,9 @@ Yields:
6164

6265
## API
6366

67+
This package exports the following identifiers: `raw`.
68+
There is no default export.
69+
6470
### `raw(tree[, file][, options])`
6571

6672
Given a [**hast**][hast] [*tree*][tree] and an optional [vfile][] (for

test.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var u = require('unist-builder')
5-
var h = require('hastscript')
6-
var unified = require('unified')
7-
var parse = require('remark-parse')
8-
var remark2rehype = require('remark-rehype')
9-
var stringify = require('rehype-stringify')
10-
var raw = require('.')
1+
import test from 'tape'
2+
import {u} from 'unist-builder'
3+
import {h} from 'hastscript'
4+
import unified from 'unified'
5+
import remarkParse from 'remark-parse'
6+
import remarkRehype from 'remark-rehype'
7+
import rehypeStringify from 'rehype-stringify'
8+
import {raw} from './index.js'
119

1210
test('raw', function (t) {
1311
t.throws(
@@ -63,7 +61,7 @@ test('raw', function (t) {
6361
t.deepEqual(
6462
raw(u('root', [u('doctype', {name: 'html'}), h('html', {lang: 'en'}, [])])),
6563
u('root', {data: {quirksMode: false}}, [
66-
u('doctype', {name: 'html', public: null, system: null}),
64+
u('doctype'),
6765
h('html', {lang: 'en'}, [h('head'), h('body')])
6866
]),
6967
'should pass documents through (#2)'
@@ -268,8 +266,8 @@ test('raw', function (t) {
268266

269267
test('integration', function (t) {
270268
unified()
271-
.use(parse)
272-
.use(remark2rehype, {allowDangerousHtml: true})
269+
.use(remarkParse)
270+
.use(remarkRehype, {allowDangerousHtml: true})
273271
.use(function () {
274272
return raw
275273
})
@@ -628,7 +626,7 @@ test('integration', function (t) {
628626
)
629627
}
630628
})
631-
.use(stringify)
629+
.use(rehypeStringify)
632630
.process(
633631
[
634632
'<i>Some title</i>',

0 commit comments

Comments
 (0)