Skip to content

Commit 4e268e6

Browse files
committed
Use ESM
1 parent d8ba099 commit 4e268e6

8 files changed

+229
-233
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

-53
This file was deleted.

index.js

+166-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,166 @@
1-
exports.fromMarkdown = require('./from-markdown.js')
2-
exports.toMarkdown = require('./to-markdown.js')
1+
import phrasing from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
2+
import defaultInlineCode from 'mdast-util-to-markdown/lib/handle/inline-code.js'
3+
import {markdownTable} from 'markdown-table'
4+
5+
export const gfmTableFromMarkdown = {
6+
enter: {
7+
table: enterTable,
8+
tableData: enterCell,
9+
tableHeader: enterCell,
10+
tableRow: enterRow
11+
},
12+
exit: {
13+
codeText: exitCodeText,
14+
table: exitTable,
15+
tableData: exit,
16+
tableHeader: exit,
17+
tableRow: exit
18+
}
19+
}
20+
21+
function enterTable(token) {
22+
this.enter({type: 'table', align: token._align, children: []}, token)
23+
this.setData('inTable', true)
24+
}
25+
26+
function exitTable(token) {
27+
this.exit(token)
28+
this.setData('inTable')
29+
}
30+
31+
function enterRow(token) {
32+
this.enter({type: 'tableRow', children: []}, token)
33+
}
34+
35+
function exit(token) {
36+
this.exit(token)
37+
}
38+
39+
function enterCell(token) {
40+
this.enter({type: 'tableCell', children: []}, token)
41+
}
42+
43+
// Overwrite the default code text data handler to unescape escaped pipes when
44+
// they are in tables.
45+
function exitCodeText(token) {
46+
var value = this.resume()
47+
48+
if (this.getData('inTable')) {
49+
value = value.replace(/\\([\\|])/g, replace)
50+
}
51+
52+
this.stack[this.stack.length - 1].value = value
53+
this.exit(token)
54+
}
55+
56+
function replace($0, $1) {
57+
// Pipes work, backslashes don’t (but can’t escape pipes).
58+
return $1 === '|' ? $1 : $0
59+
}
60+
61+
export function gfmTableToMarkdown(options) {
62+
var settings = options || {}
63+
var padding = settings.tableCellPadding
64+
var alignDelimiters = settings.tablePipeAlign
65+
var stringLength = settings.stringLength
66+
var around = padding ? ' ' : '|'
67+
68+
return {
69+
unsafe: [
70+
{character: '\r', inConstruct: 'tableCell'},
71+
{character: '\n', inConstruct: 'tableCell'},
72+
// A pipe, when followed by a tab or space (padding), or a dash or colon
73+
// (unpadded delimiter row), could result in a table.
74+
{atBreak: true, character: '|', after: '[\t :-]'},
75+
// A pipe in a cell must be encoded.
76+
{character: '|', inConstruct: 'tableCell'},
77+
// A colon must be followed by a dash, in which case it could start a
78+
// delimiter row.
79+
{atBreak: true, character: ':', after: '-'},
80+
// A delimiter row can also start with a dash, when followed by more
81+
// dashes, a colon, or a pipe.
82+
// This is a stricter version than the built in check for lists, thematic
83+
// breaks, and setex heading underlines though:
84+
// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/51a2038/lib/unsafe.js#L57>
85+
{atBreak: true, character: '-', after: '[:|-]'}
86+
],
87+
handlers: {
88+
table: handleTable,
89+
tableRow: handleTableRow,
90+
tableCell: handleTableCell,
91+
inlineCode: inlineCodeWithTable
92+
}
93+
}
94+
95+
function handleTable(node, _, context) {
96+
return serializeData(handleTableAsData(node, context), node.align)
97+
}
98+
99+
// This function isn’t really used normally, because we handle rows at the
100+
// table level.
101+
// But, if someone passes in a table row, this ensures we make somewhat sense.
102+
function handleTableRow(node, _, context) {
103+
var row = handleTableRowAsData(node, context)
104+
// `markdown-table` will always add an align row
105+
var value = serializeData([row])
106+
return value.slice(0, value.indexOf('\n'))
107+
}
108+
109+
function handleTableCell(node, _, context) {
110+
var exit = context.enter('tableCell')
111+
var value = phrasing(node, context, {before: around, after: around})
112+
exit()
113+
return value
114+
}
115+
116+
function serializeData(matrix, align) {
117+
return markdownTable(matrix, {
118+
align,
119+
alignDelimiters,
120+
padding,
121+
stringLength
122+
})
123+
}
124+
125+
function handleTableAsData(node, context) {
126+
var children = node.children
127+
var index = -1
128+
var length = children.length
129+
var result = []
130+
var subexit = context.enter('table')
131+
132+
while (++index < length) {
133+
result[index] = handleTableRowAsData(children[index], context)
134+
}
135+
136+
subexit()
137+
138+
return result
139+
}
140+
141+
function handleTableRowAsData(node, context) {
142+
var children = node.children
143+
var index = -1
144+
var length = children.length
145+
var result = []
146+
var subexit = context.enter('tableRow')
147+
148+
while (++index < length) {
149+
result[index] = handleTableCell(children[index], node, context)
150+
}
151+
152+
subexit()
153+
154+
return result
155+
}
156+
157+
function inlineCodeWithTable(node, parent, context) {
158+
var value = defaultInlineCode(node, parent, context)
159+
160+
if (context.stack.indexOf('tableCell') !== -1) {
161+
value = value.replace(/\|/g, '\\$&')
162+
}
163+
164+
return value
165+
}
166+
}

package.json

+13-17
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,34 @@
2828
"contributors": [
2929
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
3030
],
31+
"sideEffects": false,
32+
"type": "module",
33+
"main": "index.js",
3134
"files": [
32-
"from-markdown.js",
33-
"index.js",
34-
"to-markdown.js"
35+
"index.js"
3536
],
3637
"dependencies": {
37-
"markdown-table": "^2.0.0",
38+
"markdown-table": "^3.0.0",
3839
"mdast-util-to-markdown": "~0.6.0"
3940
},
4041
"devDependencies": {
42+
"c8": "^7.0.0",
4143
"mdast-util-from-markdown": "^0.8.0",
4244
"micromark-extension-gfm-table": "^0.4.0",
43-
"nyc": "^15.0.0",
4445
"prettier": "^2.0.0",
4546
"remark-cli": "^9.0.0",
4647
"remark-preset-wooorm": "^8.0.0",
47-
"string-width": "^4.0.0",
48+
"string-width": "^5.0.0",
4849
"tape": "^5.0.0",
49-
"unist-util-remove-position": "^3.0.0",
50-
"xo": "^0.38.0"
50+
"unist-util-remove-position": "^4.0.0",
51+
"xo": "^0.39.0"
5152
},
5253
"scripts": {
5354
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
54-
"test-api": "node test",
55-
"test-coverage": "nyc --reporter lcov tape test.js",
55+
"test-api": "node --conditions development test.js",
56+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
5657
"test": "npm run format && npm run test-coverage"
5758
},
58-
"nyc": {
59-
"check-coverage": true,
60-
"lines": 100,
61-
"functions": 100,
62-
"branches": 100
63-
},
6459
"prettier": {
6560
"tabWidth": 2,
6661
"useTabs": false,
@@ -71,8 +66,9 @@
7166
},
7267
"xo": {
7368
"prettier": true,
74-
"esnext": false,
7569
"rules": {
70+
"no-var": "off",
71+
"prefer-arrow-callback": "off",
7672
"unicorn/prefer-includes": "off"
7773
}
7874
},

readme.md

+9-6
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
@@ -111,15 +114,15 @@ brevity):
111114

112115
## API
113116

114-
### `table.fromMarkdown`
117+
This package exports the following identifier: `gfmTableFromMarkdown`,
118+
`gfmTableToMarkdown`.
119+
There is no default export.
115120

116-
### `table.toMarkdown(options?)`
121+
### `gfmTableFromMarkdown`
117122

118-
> Note: the separate extensions are also available at
119-
> `mdast-util-gfm-table/from-markdown` and
120-
> `mdast-util-gfm-table/to-markdown`.
123+
### `gfmTableToMarkdown(options?)`
121124

122-
Support tables.
125+
Support GFM tables.
123126
The exports of `fromMarkdown` is an extension for
124127
[`mdast-util-from-markdown`][from-markdown].
125128
The export of `toMarkdown` is a function that can be called with options and

0 commit comments

Comments
 (0)