Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 2, 2021
1 parent 5b4942c commit a074a94
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 93 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
hast-util-to-nlcst.js
hast-util-to-nlcst.min.js
yarn.lock
3 changes: 0 additions & 3 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
coverage/
hast-util-to-nlcst.js
hast-util-to-nlcst.min.js
*.html
*.json
*.md
49 changes: 26 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
'use strict'

var embedded = require('hast-util-embedded')
var convert = require('hast-util-is-element/convert')
var phrasing = require('hast-util-phrasing')
var textContent = require('hast-util-to-string')
var whitespace = require('hast-util-whitespace')
var toString = require('nlcst-to-string')
var position = require('unist-util-position')
var vfileLocation = require('vfile-location')

module.exports = toNlcst
import {embedded} from 'hast-util-embedded'
import {convertElement} from 'hast-util-is-element'
import {phrasing} from 'hast-util-phrasing'
import toString from 'hast-util-to-string'
import {whitespace} from 'hast-util-whitespace'
import {toString as nlcstToString} from 'nlcst-to-string'
import {pointStart} from 'unist-util-position'
import vfileLocation from 'vfile-location'

var push = [].push

var source = convert(['code', dataNlcstSourced])
var ignore = convert(['script', 'style', 'svg', 'math', 'del', dataNlcstIgnore])
var explicit = convert(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
var source = convertElement(['code', dataNlcstSourced])
var ignore = convertElement([
'script',
'style',
'svg',
'math',
'del',
dataNlcstIgnore
])
var explicit = convertElement(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])

var flowAccepting = convert([
var flowAccepting = convertElement([
'body',
'article',
'section',
Expand All @@ -44,10 +47,10 @@ var flowAccepting = convert([
])

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

// Transform `tree` to nlcst.
function toNlcst(tree, file, Parser) {
export function toNlcst(tree, file, Parser) {
var parser
var location
var results
Expand All @@ -67,7 +70,7 @@ function toNlcst(tree, file, Parser) {
throw new Error('hast-util-to-nlcst expected parser')
}

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

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

if (result.length) {
if (result.length > 0) {
results.push(parser.tokenizeParagraph(result))
}
}
Expand Down Expand Up @@ -182,15 +185,15 @@ function toNlcst(tree, file, Parser) {
replacement = [parser.tokenizeWhiteSpace('\n')]
change = true
} else if (source(node)) {
replacement = [parser.tokenizeSource(textContent(node))]
replacement = [parser.tokenizeSource(toString(node))]
change = true
} else {
replacement = all(node.children)
}
}

return change
? patch(replacement, location, location.toOffset(position.start(node)))
? patch(replacement, location, location.toOffset(pointStart(node)))
: replacement
}

Expand Down Expand Up @@ -224,7 +227,7 @@ function toNlcst(tree, file, Parser) {
patch(node.children, location, start)
}

end = start + toString(node).length
end = start + nlcstToString(node).length

node.position = {
start: location.toPoint(start),
Expand Down
56 changes: 21 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,50 +26,41 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
"dependencies": {
"hast-util-embedded": "^1.0.0",
"hast-util-is-element": "^1.0.0",
"hast-util-phrasing": "^1.0.0",
"hast-util-embedded": "^2.0.0",
"hast-util-is-element": "^2.0.0",
"hast-util-phrasing": "^2.0.0",
"hast-util-to-string": "^1.0.0",
"hast-util-whitespace": "^1.0.0",
"nlcst-to-string": "^2.0.0",
"unist-util-position": "^3.0.0",
"hast-util-whitespace": "^2.0.0",
"nlcst-to-string": "^3.0.0",
"unist-util-position": "^4.0.0",
"vfile-location": "^3.1.0"
},
"devDependencies": {
"browserify": "^17.0.0",
"is-hidden": "^1.0.0",
"negate": "^1.0.0",
"nyc": "^15.0.0",
"parse-dutch": "^4.0.0",
"parse-english": "^4.0.0",
"parse-latin": "^4.0.0",
"c8": "^7.0.0",
"is-hidden": "^2.0.0",
"parse-dutch": "^5.0.0",
"parse-english": "^5.0.0",
"parse-latin": "^5.0.0",
"prettier": "^2.0.0",
"rehype": "^11.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"vfile": "^4.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s hastUtilToNlcst -o hast-util-to-nlcst.js",
"build-mangle": "browserify . -s hastUtilToNlcst -o hast-util-to-nlcst.min.js -p tinyify",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test": "npm run format && npm run build && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
"test": "npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -81,15 +72,10 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/explicit-length-check": "off",
"unicorn/no-fn-reference-in-iterator": "off",
"unicorn/prefer-optional-catch-binding": "off"
},
"ignores": [
"hast-util-to-nlcst.js"
]
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
"plugins": [
Expand Down
18 changes: 12 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
## Install

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

[npm][]:

```sh
Expand All @@ -35,16 +38,16 @@ Say we have the following `example.html`:
…and next to it, `index.js`:

```js
var rehype = require('rehype')
var vfile = require('to-vfile')
var English = require('parse-english')
var inspect = require('unist-util-inspect')
var toNlcst = require('hast-util-to-nlcst')
import rehype from 'rehype'
import vfile from 'to-vfile'
import {ParseEnglish} from 'parse-english'
import {inspect} from 'unist-util-inspect'
import {toNlcst} from 'hast-util-to-nlcst'

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

console.log(inspect(toNlcst(tree, file, English)))
console.log(inspect(toNlcst(tree, file, ParseEnglish)))
```

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

## API

This package exports the following identifiers: `toNlcst`.
There is no default export.

### `toNlcst(tree, file, Parser)`

Transform the given [**hast**][hast] [*tree*][tree] to [**nlcst**][nlcst].
Expand Down
45 changes: 22 additions & 23 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
'use strict'

var fs = require('fs')
var path = require('path')
var test = require('tape')
var rehype = require('rehype')
var vfile = require('vfile')
var Latin = require('parse-latin')
var Dutch = require('parse-dutch')
var English = require('parse-english')
var negate = require('negate')
var hidden = require('is-hidden')
var toNlcst = require('..')
import fs from 'fs'
import path from 'path'
import test from 'tape'
import rehype from 'rehype'
import vfile from 'vfile'
import {ParseLatin} from 'parse-latin'
import {ParseDutch} from 'parse-dutch'
import {ParseEnglish} from 'parse-english'
import {isHidden} from 'is-hidden'
import {toNlcst} from '../index.js'

test('hast-util-to-nlcst', function (t) {
t.throws(
Expand Down Expand Up @@ -63,7 +60,7 @@ test('hast-util-to-nlcst', function (t) {

t.throws(
function () {
toNlcst({type: 'text', value: 'foo'}, vfile(), Latin)
toNlcst({type: 'text', value: 'foo'}, vfile(), ParseLatin)
},
/hast-util-to-nlcst expected position on nodes/,
'should fail when not given positional information'
Expand All @@ -80,7 +77,7 @@ test('hast-util-to-nlcst', function (t) {
}
},
vfile(),
English
ParseEnglish
)
}, 'should accept a parser constructor')

Expand All @@ -95,7 +92,7 @@ test('hast-util-to-nlcst', function (t) {
}
},
vfile(),
new Dutch()
new ParseDutch()
)
}, 'should accept a parser instance')

Expand All @@ -108,7 +105,7 @@ test('hast-util-to-nlcst', function (t) {
position: {start: {}, end: {}}
},
vfile(),
Latin
ParseLatin
)
},
/hast-util-to-nlcst expected position on nodes/,
Expand All @@ -126,7 +123,7 @@ test('hast-util-to-nlcst', function (t) {
}
},
vfile('foo'),
Latin
ParseLatin
)

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

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

test('Fixtures', function (t) {
var root = path.join(__dirname, 'fixtures')
var files = fs.readdirSync(root).filter(negate(hidden))
var root = path.join('test', 'fixtures')
var files = fs.readdirSync(root)
var index = -1
var input
var output
Expand All @@ -176,14 +173,16 @@ test('Fixtures', function (t) {
var expected

while (++index < files.length) {
if (isHidden(files[index])) continue

input = path.join(root, files[index], 'input.html')
output = path.join(root, files[index], 'output.json')
file = vfile(fs.readFileSync(input))
actual = toNlcst(rehype().parse(file), file, Latin)
actual = toNlcst(rehype().parse(file), file, ParseLatin)

try {
expected = JSON.parse(fs.readFileSync(output))
} catch (_) {
} catch {
fs.writeFileSync(output, JSON.stringify(actual, null, 2) + '\n')
return
}
Expand Down

0 comments on commit a074a94

Please sign in to comment.