Skip to content

Commit f6cc767

Browse files
committed
Use ESM
1 parent 909494d commit f6cc767

8 files changed

+122
-122
lines changed

.gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
.DS_Store
2-
*.log
3-
.nyc_output/
41
coverage/
52
node_modules/
3+
.DS_Store
4+
*.log
65
yarn.lock

.prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

from-markdown.js

-50
This file was deleted.

index.js

+74-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,74 @@
1-
exports.fromMarkdown = require('./from-markdown.js')
2-
exports.toMarkdown = require('./to-markdown.js')
1+
import defaultListItem from 'mdast-util-to-markdown/lib/handle/list-item.js'
2+
3+
export const gfmTaskListItemFromMarkdown = {
4+
exit: {
5+
taskListCheckValueChecked: exitCheck,
6+
taskListCheckValueUnchecked: exitCheck,
7+
paragraph: exitParagraphWithTaskListItem
8+
}
9+
}
10+
11+
export const gfmTaskListItemToMarkdown = {
12+
unsafe: [{atBreak: true, character: '-', after: '[:|-]'}],
13+
handlers: {listItem: listItemWithTaskListItem}
14+
}
15+
16+
function exitCheck(token) {
17+
// We’re always in a paragraph, in a list item.
18+
this.stack[this.stack.length - 2].checked =
19+
token.type === 'taskListCheckValueChecked'
20+
}
21+
22+
function exitParagraphWithTaskListItem(token) {
23+
var parent = this.stack[this.stack.length - 2]
24+
var node = this.stack[this.stack.length - 1]
25+
var siblings = parent.children
26+
var head = node.children[0]
27+
var index = -1
28+
var firstParaghraph
29+
30+
if (
31+
parent &&
32+
parent.type === 'listItem' &&
33+
typeof parent.checked === 'boolean' &&
34+
head &&
35+
head.type === 'text'
36+
) {
37+
while (++index < siblings.length) {
38+
if (siblings[index].type === 'paragraph') {
39+
firstParaghraph = siblings[index]
40+
break
41+
}
42+
}
43+
44+
if (firstParaghraph === node) {
45+
// Must start with a space or a tab.
46+
head.value = head.value.slice(1)
47+
48+
if (head.value.length === 0) {
49+
node.children.shift()
50+
} else {
51+
head.position.start.column++
52+
head.position.start.offset++
53+
node.position.start = Object.assign({}, head.position.start)
54+
}
55+
}
56+
}
57+
58+
this.exit(token)
59+
}
60+
61+
function listItemWithTaskListItem(node, parent, context) {
62+
var value = defaultListItem(node, parent, context)
63+
var head = node.children[0]
64+
65+
if (typeof node.checked === 'boolean' && head && head.type === 'paragraph') {
66+
value = value.replace(/^(?:[*+-]|\d+\.)([\r\n]| {1,3})/, check)
67+
}
68+
69+
return value
70+
71+
function check($0) {
72+
return $0 + '[' + (node.checked ? 'x' : ' ') + '] '
73+
}
74+
}

package.json

+11-15
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,32 @@
2929
"contributors": [
3030
"Titus Wormer <[email protected]> (https://wooorm.com)"
3131
],
32+
"sideEffects": false,
33+
"type": "module",
34+
"main": "index.js",
3235
"files": [
33-
"from-markdown.js",
34-
"index.js",
35-
"to-markdown.js"
36+
"index.js"
3637
],
3738
"dependencies": {
3839
"mdast-util-to-markdown": "~0.6.0"
3940
},
4041
"devDependencies": {
42+
"c8": "^7.7.3",
4143
"mdast-util-from-markdown": "^0.8.0",
4244
"micromark-extension-gfm-task-list-item": "^0.3.0",
43-
"nyc": "^15.0.0",
4445
"prettier": "^2.0.0",
4546
"remark-cli": "^9.0.0",
4647
"remark-preset-wooorm": "^8.0.0",
4748
"tape": "^5.0.0",
48-
"unist-util-remove-position": "^3.0.0",
49-
"xo": "^0.38.0"
49+
"unist-util-remove-position": "^4.0.0",
50+
"xo": "^0.39.0"
5051
},
5152
"scripts": {
5253
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
53-
"test-api": "node test",
54-
"test-coverage": "nyc --reporter lcov tape test.js",
54+
"test-api": "node --conditions development test.js",
55+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
5556
"test": "npm run format && npm run test-coverage"
5657
},
57-
"nyc": {
58-
"check-coverage": true,
59-
"lines": 100,
60-
"functions": 100,
61-
"branches": 100
62-
},
6358
"prettier": {
6459
"tabWidth": 2,
6560
"useTabs": false,
@@ -70,8 +65,9 @@
7065
},
7166
"xo": {
7267
"prettier": true,
73-
"esnext": false,
7468
"rules": {
69+
"no-var": "off",
70+
"prefer-arrow-callback": "off",
7571
"unicorn/prefer-includes": "off"
7672
}
7773
},

readme.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ You probably shouldn’t use this package directly, but instead use
1919

2020
## Install
2121

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

2427
```sh
@@ -129,13 +132,13 @@ brevity):
129132

130133
## API
131134

132-
### `taskListItem.fromMarkdown`
135+
This package exports the following identifier: `gfmTaskListItemFromMarkdown`,
136+
`gfmTaskListItemToMarkdown`.
137+
There is no default export.
133138

134-
### `taskListItem.toMarkdown`
139+
### `gfmTaskListItemFromMarkdown`
135140

136-
> Note: the separate extensions are also available at
137-
> `mdast-util-gfm-task-list-item/from-markdown` and
138-
> `mdast-util-gfm-task-list-item/to-markdown`.
141+
### `gfmTaskListItemToMarkdown`
139142

140143
Support task list items.
141144
The exports are extensions, respectively

test.js

+27-24
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
var test = require('tape')
2-
var fromMarkdown = require('mdast-util-from-markdown')
3-
var toMarkdown = require('mdast-util-to-markdown')
4-
var removePosition = require('unist-util-remove-position')
5-
var syntax = require('micromark-extension-gfm-task-list-item')
6-
var taskListItem = require('.')
1+
import test from 'tape'
2+
import fromMarkdown from 'mdast-util-from-markdown'
3+
import toMarkdown from 'mdast-util-to-markdown'
4+
import {removePosition} from 'unist-util-remove-position'
5+
import gfmTaskListItem from 'micromark-extension-gfm-task-list-item'
6+
import {
7+
gfmTaskListItemFromMarkdown,
8+
gfmTaskListItemToMarkdown
9+
} from './index.js'
710

811
test('markdown -> mdast', function (t) {
912
t.deepEqual(
1013
fromMarkdown('* [x] a', {
11-
extensions: [syntax],
12-
mdastExtensions: [taskListItem.fromMarkdown]
14+
extensions: [gfmTaskListItem],
15+
mdastExtensions: [gfmTaskListItemFromMarkdown]
1316
}),
1417
{
1518
type: 'root',
@@ -66,8 +69,8 @@ test('markdown -> mdast', function (t) {
6669
t.deepEqual(
6770
removePosition(
6871
fromMarkdown('*\n [x] after a blank line', {
69-
extensions: [syntax],
70-
mdastExtensions: [taskListItem.fromMarkdown]
72+
extensions: [gfmTaskListItem],
73+
mdastExtensions: [gfmTaskListItemFromMarkdown]
7174
}),
7275
true
7376
).children[0].children[0],
@@ -88,8 +91,8 @@ test('markdown -> mdast', function (t) {
8891
t.deepEqual(
8992
removePosition(
9093
fromMarkdown('* [x]\ttab', {
91-
extensions: [syntax],
92-
mdastExtensions: [taskListItem.fromMarkdown]
94+
extensions: [gfmTaskListItem],
95+
mdastExtensions: [gfmTaskListItemFromMarkdown]
9396
}),
9497
true
9598
).children[0].children[0],
@@ -105,8 +108,8 @@ test('markdown -> mdast', function (t) {
105108
t.deepEqual(
106109
removePosition(
107110
fromMarkdown('* [x]: definition\n [x] tasklist', {
108-
extensions: [syntax],
109-
mdastExtensions: [taskListItem.fromMarkdown]
111+
extensions: [gfmTaskListItem],
112+
mdastExtensions: [gfmTaskListItemFromMarkdown]
110113
}),
111114
true
112115
).children[0].children[0],
@@ -131,8 +134,8 @@ test('markdown -> mdast', function (t) {
131134
t.deepEqual(
132135
removePosition(
133136
fromMarkdown('[x] tasklist', {
134-
extensions: [syntax],
135-
mdastExtensions: [taskListItem.fromMarkdown]
137+
extensions: [gfmTaskListItem],
138+
mdastExtensions: [gfmTaskListItemFromMarkdown]
136139
}),
137140
true
138141
).children[0],
@@ -143,8 +146,8 @@ test('markdown -> mdast', function (t) {
143146
t.deepEqual(
144147
removePosition(
145148
fromMarkdown('* [x] *b*', {
146-
extensions: [syntax],
147-
mdastExtensions: [taskListItem.fromMarkdown]
149+
extensions: [gfmTaskListItem],
150+
mdastExtensions: [gfmTaskListItemFromMarkdown]
148151
}),
149152
true
150153
).children[0].children[0],
@@ -165,8 +168,8 @@ test('markdown -> mdast', function (t) {
165168
t.deepEqual(
166169
removePosition(
167170
fromMarkdown('* [x] a\n\n b', {
168-
extensions: [syntax],
169-
mdastExtensions: [taskListItem.fromMarkdown]
171+
extensions: [gfmTaskListItem],
172+
mdastExtensions: [gfmTaskListItemFromMarkdown]
170173
}),
171174
true
172175
).children[0].children[0],
@@ -199,7 +202,7 @@ test('mdast -> markdown', function (t) {
199202
checked: true,
200203
children: [{type: 'paragraph', children: [{type: 'text', value: 'a'}]}]
201204
},
202-
{extensions: [taskListItem.toMarkdown]}
205+
{extensions: [gfmTaskListItemToMarkdown]}
203206
),
204207
'* [x] a\n',
205208
'should serialize a checked list item'
@@ -212,7 +215,7 @@ test('mdast -> markdown', function (t) {
212215
checked: false,
213216
children: [{type: 'paragraph', children: [{type: 'text', value: 'b'}]}]
214217
},
215-
{extensions: [taskListItem.toMarkdown]}
218+
{extensions: [gfmTaskListItemToMarkdown]}
216219
),
217220
'* [ ] b\n',
218221
'should serialize an unchecked list item'
@@ -224,7 +227,7 @@ test('mdast -> markdown', function (t) {
224227
type: 'listItem',
225228
children: [{type: 'paragraph', children: [{type: 'text', value: 'c'}]}]
226229
},
227-
{extensions: [taskListItem.toMarkdown]}
230+
{extensions: [gfmTaskListItemToMarkdown]}
228231
),
229232
'* c\n',
230233
'should serialize an normal list item'
@@ -245,7 +248,7 @@ test('mdast -> markdown', function (t) {
245248
{type: 'paragraph', children: [{type: 'text', value: 'e'}]}
246249
]
247250
},
248-
{extensions: [taskListItem.toMarkdown]}
251+
{extensions: [gfmTaskListItemToMarkdown]}
249252
),
250253
'* [d]: definition\n\n e\n',
251254
'should ignore `checked` if the head is not a paragraph'

to-markdown.js

-22
This file was deleted.

0 commit comments

Comments
 (0)