Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 26, 2021
1 parent 0deb85b commit 53246c3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 77 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
yarn.lock
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage/
*.json
*.md
62 changes: 27 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
'use strict'
import {stringify as commas} from 'comma-separated-tokens'
import {stringify as spaces} from 'space-separated-tokens'
import {html, svg, find} from 'property-information'
import {position} from 'unist-util-position'
import {webNamespaces} from 'web-namespaces'
import {zwitch} from 'zwitch'

module.exports = toXast

var comma = require('comma-separated-tokens')
var html = require('property-information/html')
var svg = require('property-information/svg')
var find = require('property-information/find')
var space = require('space-separated-tokens')
var position = require('unist-util-position')
var namespaces = require('web-namespaces')
var xtend = require('xtend')
var zwitch = require('zwitch')
var own = {}.hasOwnProperty

var one = zwitch('type', {
handlers: {
root: root,
element: element,
text: text,
comment: comment,
doctype: doctype
},
invalid: invalid,
unknown: unknown
handlers: {root, element, text, comment, doctype},
invalid,
unknown
})

function invalid(value) {
Expand All @@ -32,7 +21,7 @@ function unknown(value) {
throw new Error('Cannot transform node of type `' + value.type + '`')
}

function toXast(tree, options) {
export function toXast(tree, options) {
var space = typeof options === 'string' ? options : (options || {}).space
return one(tree, {schema: space === 'svg' ? svg : html, ns: null})
}
Expand Down Expand Up @@ -62,6 +51,7 @@ function doctype(node, config) {
)
}

// eslint-disable-next-line complexity
function element(node, parentConfig) {
var props = node.properties || {}
var schema = parentConfig.schema
Expand All @@ -71,31 +61,39 @@ function element(node, parentConfig) {
var key
var info

if (props.xmlns === namespaces.html) {
if (props.xmlns === webNamespaces.html) {
schema = html
} else if (props.xmlns === namespaces.svg) {
} else if (props.xmlns === webNamespaces.svg) {
schema = svg
} else if (props.xmlns) {
// We don’t support non-HTML, non-SVG namespaces, so stay in the same.
} else if (schema === html && node.tagName === 'svg') {
schema = svg
}

config = xtend(parentConfig, {schema: schema, ns: namespaces[schema.space]})
config = Object.assign({}, parentConfig, {
schema,
ns: webNamespaces[schema.space]
})

if (parentConfig.ns !== config.ns) {
attributes.xmlns = config.ns
}

for (key in props) {
if (!own.call(props, key)) {
continue
}

info = find(schema, key)
value = props[key]

// Ignore nullish, false, and `NaN` values, and falsey known booleans.
if (
value == null ||
value === undefined ||
value === null ||
value === false ||
value !== value ||
(typeof value === 'number' && Number.isNaN(value)) ||
(!value && info.boolean)
) {
continue
Expand All @@ -108,9 +106,7 @@ function element(node, parentConfig) {
// Accept `array`.
// Most props are space-separated.
else if (typeof value === 'object' && 'length' in value) {
value = info.commaSeparated
? comma.stringify(value)
: space.stringify(value)
value = info.commaSeparated ? commas(value) : spaces(value)
}
// Cast everything else to string.
else if (typeof value !== 'string') {
Expand All @@ -120,11 +116,7 @@ function element(node, parentConfig) {
attributes[info.attribute] = value
}

return patch(
node,
{type: 'element', name: node.tagName, attributes: attributes},
config
)
return patch(node, {type: 'element', name: node.tagName, attributes}, config)
}

function patch(origin, node, config) {
Expand Down
41 changes: 16 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,37 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"index.js"
],
"dependencies": {
"comma-separated-tokens": "^1.0.0",
"property-information": "^5.0.0",
"space-separated-tokens": "^1.0.0",
"unist-util-position": "^3.1.0",
"web-namespaces": "^1.0.0",
"xtend": "^4.0.0",
"zwitch": "^1.0.0"
"comma-separated-tokens": "^2.0.0",
"property-information": "^6.0.0",
"space-separated-tokens": "^2.0.0",
"unist-util-position": "^4.0.0",
"web-namespaces": "^2.0.0",
"zwitch": "^2.0.0"
},
"devDependencies": {
"c8": "^7.0.0",
"hastscript": "^6.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"unist-builder": "^2.0.0",
"unist-builder": "^3.0.0",
"xastscript": "^2.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
Expand All @@ -70,14 +66,9 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"eqeqeq": "off",
"guard-for-in": "off",
"no-eq-null": "off",
"no-self-compare": "off",
"unicorn/prefer-number-properties": "off",
"unicorn/prefer-includes": "off"
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
Expand Down
16 changes: 11 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 @@ -32,11 +35,11 @@ Say we have an `example.html` file, that looks as follows:
…and our script, `example.js`, looks as follows:

```js
var fs = require('fs')
var unified = require('unified')
var parse = require('rehype-parse')
var toXast = require('hast-util-to-xast')
var toXml = require('xast-util-to-xml')
import fs from 'fs'
import unified from 'unified'
import parse from 'rehype-parse'
import {toXast} from 'hast-util-to-xast'
import {toXml} from 'xast-util-to-xml'

// Get the HTML syntax tree:
var hast = unified()
Expand All @@ -60,6 +63,9 @@ Yields:

## API

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

### `toXast(node[, space|options])`

Transform the given **[hast][]** *[tree][]* to **[xast][]**.
Expand Down
18 changes: 8 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

var test = require('tape')
var ns = require('web-namespaces')
var u = require('unist-builder')
var h = require('hastscript')
var s = require('hastscript/svg')
var x = require('xastscript')
var toXast = require('.')
import test from 'tape'
import {webNamespaces as ns} from 'web-namespaces'
import {u} from 'unist-builder'
import h from 'hastscript'
import s from 'hastscript/svg.js'
import x from 'xastscript'
import {toXast} from './index.js'

test('toXast', function (t) {
t.test('main', function (t) {
Expand Down Expand Up @@ -190,7 +188,7 @@ test('toXast', function (t) {
)

t.deepEqual(
toXast(u('element', {tagName: 'br', properties: {prop: NaN}}, [])),
toXast(u('element', {tagName: 'br', properties: {prop: Number.NaN}}, [])),
x('br', {xmlns: ns.html}),
'should support attribute values: `NaN`'
)
Expand Down

0 comments on commit 53246c3

Please sign in to comment.