Skip to content

Use ESM #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
mdast-util-toc.js
mdast-util-toc.min.js
15 changes: 9 additions & 6 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// remark-usage-ignore-next
import {inspect} from 'util'

// Dependencies:
var u = require('unist-builder')
var toc = require('.')
import {u} from 'unist-builder'
import {toc} from './index.js'

// Given a mdast tree:
var tree = u('root', [
// Now running:
const tree = u('root', [
u('heading', {depth: 1}, [u('text', 'Alpha')]),
u('heading', {depth: 2}, [u('text', 'Bravo')]),
u('heading', {depth: 3}, [u('text', 'Charlie')]),
u('heading', {depth: 2}, [u('text', 'Delta')])
])

var table = toc(tree)
const table = toc(tree)

// Yields:
console.log('javascript', require('util').inspect(table, {depth: 3}))
console.log('javascript', inspect(table, {depth: 3}))
Comment on lines -16 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably more specific to remark-usage being used but..

I've never really been a fan of using require inline like this. ESM forces static imports to be defined at the beginning of the module.

Sure, there are dynamic imports. However, now that code path needs to be asynchronous

4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
'use strict'

module.exports = require('./lib')
export {toc} from './lib/index.js'
36 changes: 15 additions & 21 deletions lib/contents.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict'

var extend = require('extend')

module.exports = contents
import extend from 'extend'

// Transform a list of heading objects to a markdown list.
function contents(map, tight, prefix, ordered) {
var table = {type: 'list', ordered: ordered, spread: false, children: []}
var minDepth = Number.POSITIVE_INFINITY
var index = -1
export function contents(map, tight, prefix, ordered) {
const table = {type: 'list', ordered, spread: false, children: []}
let minDepth = Number.POSITIVE_INFINITY
let index = -1

// Find minimum depth.
while (++index < map.length) {
Expand All @@ -35,11 +31,11 @@ function contents(map, tight, prefix, ordered) {
}

// Insert an entry into `parent`.
// eslint-disable-next-line max-params
function insert(entry, parent, tight, prefix, ordered) {
var siblings = parent.children
var tail = siblings[siblings.length - 1]
var index = -1
var item
const siblings = parent.children
const tail = siblings[siblings.length - 1]
let index = -1
Comment on lines -39 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not ESM related.. but the distinction of which var became a let or const is interesting. Seeing the distinction like this is useful 👍🏻

Also, the scope of item changing from function scope to block scope 👀


if (entry.depth === 1) {
siblings.push({
Expand All @@ -65,13 +61,13 @@ function insert(entry, parent, tight, prefix, ordered) {
entry.depth--
insert(entry, tail, tight, prefix, ordered)
} else if (parent.type === 'list') {
item = {type: 'listItem', spread: false, children: []}
const item = {type: 'listItem', spread: false, children: []}
siblings.push(item)
insert(entry, item, tight, prefix, ordered)
} else {
item = {
const item = {
type: 'list',
ordered: ordered,
ordered,
spread: false,
children: []
}
Expand All @@ -95,8 +91,8 @@ function insert(entry, parent, tight, prefix, ordered) {
}

function all(children) {
var result = []
var index = -1
let result = []
let index = -1

if (children) {
while (++index < children.length) {
Expand All @@ -108,8 +104,6 @@ function all(children) {
}

function one(node) {
var copy

if (
node.type === 'link' ||
node.type === 'linkReference' ||
Expand All @@ -119,7 +113,7 @@ function one(node) {
return all(node.children)
}

copy = extend({}, node)
let copy = extend({}, node)

delete copy.children
delete copy.position
Expand Down
38 changes: 18 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
'use strict'

module.exports = toc

var search = require('./search')
var contents = require('./contents')
var toExpression = require('./to-expression')
import {search} from './search.js'
import {contents} from './contents.js'
import {toExpression} from './to-expression.js'

// Get a TOC representation of `node`.
function toc(node, options) {
var settings = options || {}
var heading = settings.heading ? toExpression(settings.heading) : null
var result = search(node, heading, settings)
export function toc(node, options) {
const settings = options || {}
const heading = settings.heading ? toExpression(settings.heading) : null
const result = search(node, heading, settings)

result.map = result.map.length
? contents(
result.map,
settings.tight,
settings.prefix,
settings.ordered || false
)
: null
result.map =
result.map.length > 0
? contents(
result.map,
settings.tight,
settings.prefix,
settings.ordered || false
)
: null

// No given heading.
if (!heading) {
result.endIndex = result.index = null
result.endIndex = null
result.index = null
}

return result
Expand Down
42 changes: 21 additions & 21 deletions lib/search.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
'use strict'
import Slugger from 'github-slugger'
import toString from 'mdast-util-to-string'
import {visit} from 'unist-util-visit'
import {convert} from 'unist-util-is'
import {toExpression} from './to-expression.js'

module.exports = search

var toString = require('mdast-util-to-string')
var visit = require('unist-util-visit')
var convert = require('unist-util-is/convert')
var slugs = require('github-slugger')()
var toExpression = require('./to-expression')
const slugs = new Slugger()

// Search a node for a location.
function search(root, expression, settings) {
var skip = settings.skip && toExpression(settings.skip)
var parents = convert(settings.parents || root)
var map = []
var index
var endIndex
var opening
export function search(root, expression, settings) {
const skip = settings.skip && toExpression(settings.skip)
const parents = convert(settings.parents || root)
const map = []
let index
let endIndex
let opening

slugs.reset()

Expand All @@ -25,16 +23,18 @@ function search(root, expression, settings) {

return {
index: index || -1,
// <sindresorhus/eslint-plugin-unicorn#980>
// eslint-disable-next-line unicorn/explicit-length-check
endIndex: index ? endIndex || root.children.length : -1,
map: map
map
}

function onheading(node, position, parent) {
var value = toString(node)
/* istanbul ignore next - to do: remove this when `remark-attr` is up to
* date w/ micromark. */
var id = node.data && node.data.hProperties && node.data.hProperties.id
var slug = slugs.slug(id || value)
const value = toString(node)
// Remove this when `remark-attr` is up to date w/ micromark.
/* c8 ignore next */
const id = node.data && node.data.hProperties && node.data.hProperties.id
const slug = slugs.slug(id || value)

if (!parents(parent)) {
return
Expand Down
6 changes: 1 addition & 5 deletions lib/to-expression.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
'use strict'

module.exports = toExpression

// Transform a string into an applicable expression.
function toExpression(value) {
export function toExpression(value) {
return new RegExp('^(' + value + ')$', 'i')
}
51 changes: 21 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"Jonathan Haines <jonno.haines@gmail.com> (https://barrythepenguin.github.io)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "types/index.d.ts",
"files": [
"types/index.d.ts",
"lib",
"lib/",
"index.js"
],
"dependencies": {
Expand All @@ -37,13 +40,12 @@
"extend": "^3.0.2",
"github-slugger": "^1.2.1",
"mdast-util-to-string": "^2.0.0",
"unist-util-is": "^4.0.0",
"unist-util-visit": "^2.0.0"
"unist-util-is": "^5.0.0",
"unist-util-visit": "^3.0.0"
},
"devDependencies": {
"browserify": "^17.0.0",
"c8": "^7.0.0",
"dtslint": "^4.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark": "^13.0.0",
"remark-attr": "^0.11.0",
Expand All @@ -54,27 +56,16 @@
"remark-preset-wooorm": "^8.0.0",
"remark-usage": "^9.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"typescript": "^4.0.0",
"unified": "^9.0.0",
"unist-builder": "^2.0.0",
"xo": "^0.38.0"
"unist-builder": "^3.0.0",
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s mdastUtilTOC > mdast-util-toc.js",
"build-mangle": "browserify . -s mdastUtilTOC -p tinyify > mdast-util-toc.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"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 @@ -86,16 +77,16 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"max-params": "off",
"no-multi-assign": "off",
"unicorn/explicit-length-check": "off",
"unicorn/prefer-optional-catch-binding": "off"
},
"overrides": [
{
"files": "example.js",
"rules": {
"capitalized-comments": "off"
}
}
],
"ignores": [
"types/",
"mdast-util-toc.js"
"types/"
]
},
"remarkConfig": {
Expand Down
16 changes: 11 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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 @@ -23,21 +26,21 @@ npm install mdast-util-toc
Dependencies:

```javascript
var u = require('unist-builder')
var toc = require('mdast-util-toc')
import {u} from 'unist-builder'
import {toc} from 'mdast-util-toc'
```

Given a mdast tree:
Now running:

```javascript
var tree = u('root', [
const tree = u('root', [
u('heading', {depth: 1}, [u('text', 'Alpha')]),
u('heading', {depth: 2}, [u('text', 'Bravo')]),
u('heading', {depth: 3}, [u('text', 'Charlie')]),
u('heading', {depth: 2}, [u('text', 'Delta')])
])

var table = toc(tree)
const table = toc(tree)
```

Yields:
Expand All @@ -57,6 +60,9 @@ Yields:

## API

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

### `toc(tree[, options])`

Generate a Table of Contents from a [tree][].
Expand Down
Loading