Skip to content

Commit e9e3685

Browse files
committed
Use ESM
1 parent eb0936b commit e9e3685

File tree

6 files changed

+45
-63
lines changed

6 files changed

+45
-63
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-to-text.js
7-
hast-util-to-text.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-to-text.js
3-
hast-util-to-text.min.js
4-
*.json
52
*.md

index.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
'use strict'
2-
3-
var repeat = require('repeat-string')
4-
var convert = require('hast-util-is-element/convert')
5-
var findAfter = require('unist-util-find-after')
6-
7-
module.exports = toText
1+
import repeat from 'repeat-string'
2+
import {convertElement} from 'hast-util-is-element'
3+
import {findAfter} from 'unist-util-find-after'
84

95
var searchLineFeeds = /\n/g
106
var searchTabOrSpaces = /[\t ]+/g
117

12-
var br = convert('br')
13-
var p = convert('p')
14-
var cell = convert(['th', 'td'])
15-
var row = convert('tr')
8+
var br = convertElement('br')
9+
var p = convertElement('p')
10+
var cell = convertElement(['th', 'td'])
11+
var row = convertElement('tr')
1612

1713
// Note that we don’t need to include void elements here as they don’t have text.
1814
// See: <https://github.com/wooorm/html-void-elements>
19-
var notRendered = convert([
15+
var notRendered = convertElement([
2016
// List from: <https://html.spec.whatwg.org/#hidden-elements>
2117
'datalist',
2218
'head',
@@ -35,7 +31,7 @@ var notRendered = convert([
3531
])
3632

3733
// See: <https://html.spec.whatwg.org/#the-css-user-agent-style-sheet-and-presentational-hints>
38-
var blockOrCaption = convert([
34+
var blockOrCaption = convertElement([
3935
'address', // Flow content
4036
'article', // Sections and headings
4137
'aside', // Sections and headings
@@ -81,10 +77,10 @@ var blockOrCaption = convert([
8177
// <https://html.spec.whatwg.org/#the-innertext-idl-attribute>
8278
// Note that we act as if `node` is being rendered, and as if we’re a
8379
// CSS-supporting user agent.
84-
function toText(node) {
80+
export function toText(node) {
8581
var children = node.children || []
8682
var block = blockOrCaption(node)
87-
var whiteSpace = inferWhiteSpace(node, {})
83+
var whitespace = inferWhitespace(node, {})
8884
var index = -1
8985
var results
9086
var result
@@ -101,7 +97,7 @@ function toText(node) {
10197
// ignored.
10298
if (node.type === 'text' || node.type === 'comment') {
10399
return collectText(node, {
104-
whiteSpace: whiteSpace,
100+
whitespace,
105101
breakBefore: true,
106102
breakAfter: true
107103
})
@@ -129,7 +125,7 @@ function toText(node) {
129125
// 3.2. For each item item in current, append item to results.
130126
results = results.concat(
131127
innerTextCollection(children[index], index, node, {
132-
whiteSpace: whiteSpace,
128+
whitespace,
133129
breakBefore: index ? null : block,
134130
breakAfter:
135131
index < children.length - 1 ? br(children[index + 1]) : block
@@ -171,7 +167,7 @@ function innerTextCollection(node, index, parent, options) {
171167

172168
if (node.type === 'text') {
173169
return [
174-
options.whiteSpace === 'normal'
170+
options.whitespace === 'normal'
175171
? collectText(node, options)
176172
: collectPreText(node, options)
177173
]
@@ -183,7 +179,7 @@ function innerTextCollection(node, index, parent, options) {
183179
// Collect an element.
184180
function collectElement(node, _, parent, options) {
185181
// First we infer the `white-space` property.
186-
var whiteSpace = inferWhiteSpace(node, options)
182+
var whitespace = inferWhitespace(node, options)
187183
var children = node.children || []
188184
var index = -1
189185
var items = []
@@ -249,7 +245,7 @@ function collectElement(node, _, parent, options) {
249245
while (++index < children.length) {
250246
items = items.concat(
251247
innerTextCollection(children[index], index, node, {
252-
whiteSpace: whiteSpace,
248+
whitespace,
253249
breakBefore: index ? null : prefix,
254250
breakAfter:
255251
index < children.length - 1 ? br(children[index + 1]) : suffix
@@ -313,7 +309,7 @@ function collectText(node, options) {
313309
// they were not there.
314310
value
315311
.slice(start, end)
316-
.replace(/[\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/g, ''),
312+
.replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, ''),
317313
options.breakBefore,
318314
options.breakAfter
319315
)
@@ -411,9 +407,9 @@ function trimAndcollapseSpacesAndTabs(value, breakBefore, breakAfter) {
411407
}
412408

413409
// We don’t support void elements here (so `nobr wbr` -> `normal` is ignored).
414-
function inferWhiteSpace(node, options) {
410+
function inferWhitespace(node, options) {
415411
var props = node.properties || {}
416-
var inherit = options.whiteSpace || 'normal'
412+
var inherit = options.whitespace || 'normal'
417413

418414
switch (node.tagName) {
419415
case 'listing':

package.json

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,32 @@
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-
"hast-util-is-element": "^1.0.0",
35+
"hast-util-is-element": "^2.0.0",
3336
"repeat-string": "^1.0.0",
34-
"unist-util-find-after": "^3.0.0"
37+
"unist-util-find-after": "^4.0.0"
3538
},
3639
"devDependencies": {
37-
"browserify": "^17.0.0",
38-
"hastscript": "^6.0.0",
39-
"nyc": "^15.0.0",
40+
"c8": "^7.7.1",
41+
"hastscript": "^7.0.0",
4042
"prettier": "^2.0.0",
4143
"remark-cli": "^9.0.0",
4244
"remark-preset-wooorm": "^8.0.0",
4345
"tape": "^5.0.0",
44-
"tinyify": "^3.0.0",
45-
"unist-builder": "^2.0.0",
46-
"xo": "^0.38.0"
46+
"unist-builder": "^3.0.0",
47+
"xo": "^0.39.0"
4748
},
4849
"scripts": {
4950
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
50-
"build-bundle": "browserify . -s hastUtilToText -o hast-util-to-text.js",
51-
"build-mangle": "browserify . -s hastUtilToText -o hast-util-to-text.min.js -p tinyify",
52-
"build": "npm run build-bundle && npm run build-mangle",
53-
"test-api": "node test",
54-
"test-coverage": "nyc --reporter lcov tape test.js",
55-
"test": "npm run format && npm run build && npm run test-coverage"
56-
},
57-
"nyc": {
58-
"check-coverage": true,
59-
"lines": 100,
60-
"functions": 100,
61-
"branches": 100
51+
"test-api": "node test.js",
52+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
53+
"test": "npm run format && npm run test-coverage"
6254
},
6355
"prettier": {
6456
"tabWidth": 2,
@@ -70,13 +62,9 @@
7062
},
7163
"xo": {
7264
"prettier": true,
73-
"esnext": false,
74-
"ignores": [
75-
"hast-util-to-text.js"
76-
],
7765
"rules": {
78-
"unicorn/escape-case": "off",
79-
"no-constant-condition": "off"
66+
"no-var": "off",
67+
"prefer-arrow-callback": "off"
8068
}
8169
},
8270
"remarkConfig": {

readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ breaks where `<br>` elements are used.
2020

2121
## Install
2222

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

2528
```sh
@@ -29,8 +32,8 @@ npm install hast-util-to-text
2932
## Use
3033

3134
```js
32-
var h = require('hastscript')
33-
var toText = require('hast-util-to-text')
35+
import {h} from 'hastscript'
36+
import {toText} from 'hast-util-to-text'
3437

3538
var tree = h('div', [
3639
h('h1', {hidden: true}, 'Alpha.'),
@@ -54,6 +57,9 @@ Delta echo foxtrot.
5457

5558
## API
5659

60+
This package exports the following identifiers: `toText`.
61+
There is no default export.
62+
5763
### `toText(node)`
5864

5965
Utility to get the plain-text value of a [*node*][node].

test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var u = require('unist-builder')
5-
var h = require('hastscript')
6-
var toText = require('.')
1+
import test from 'tape'
2+
import {u} from 'unist-builder'
3+
import {h} from 'hastscript'
4+
import {toText} from './index.js'
75

86
test('hast-util-to-text', function (t) {
97
t.equal(

0 commit comments

Comments
 (0)