Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 85d6f60

Browse files
committed
Use ESM
1 parent 37d8fef commit 85d6f60

File tree

8 files changed

+179
-185
lines changed

8 files changed

+179
-185
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

from-markdown.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

index.js

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,132 @@
1-
exports.fromMarkdown = require('./from-markdown.js')
2-
exports.toMarkdown = require('./to-markdown.js')
1+
import normalizeIdentifier from 'micromark/dist/util/normalize-identifier.js'
2+
import association from 'mdast-util-to-markdown/lib/util/association.js'
3+
import phrasing from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
4+
import flow from 'mdast-util-to-markdown/lib/util/container-flow.js'
5+
import indentLines from 'mdast-util-to-markdown/lib/util/indent-lines.js'
6+
import safe from 'mdast-util-to-markdown/lib/util/safe.js'
7+
8+
export const footnoteFromMarkdown = {
9+
canContainEols: ['footnote'],
10+
enter: {
11+
footnoteDefinition: enterFootnoteDefinition,
12+
footnoteDefinitionLabelString: enterFootnoteDefinitionLabelString,
13+
footnoteCall: enterFootnoteCall,
14+
footnoteCallString: enterFootnoteCallString,
15+
inlineNote: enterNote
16+
},
17+
exit: {
18+
footnoteDefinition: exitFootnoteDefinition,
19+
footnoteDefinitionLabelString: exitFootnoteDefinitionLabelString,
20+
footnoteCall: exitFootnoteCall,
21+
footnoteCallString: exitFootnoteCallString,
22+
inlineNote: exitNote
23+
}
24+
}
25+
26+
export const footnoteToMarkdown = {
27+
// This is on by default already.
28+
unsafe: [{character: '[', inConstruct: ['phrasing', 'label', 'reference']}],
29+
handlers: {footnote, footnoteDefinition, footnoteReference}
30+
}
31+
32+
footnoteReference.peek = footnoteReferencePeek
33+
footnote.peek = footnotePeek
34+
35+
function enterFootnoteDefinition(token) {
36+
this.enter(
37+
{type: 'footnoteDefinition', identifier: '', label: '', children: []},
38+
token
39+
)
40+
}
41+
42+
function enterFootnoteDefinitionLabelString() {
43+
this.buffer()
44+
}
45+
46+
function exitFootnoteDefinitionLabelString(token) {
47+
var label = this.resume()
48+
this.stack[this.stack.length - 1].label = label
49+
this.stack[this.stack.length - 1].identifier = normalizeIdentifier(
50+
this.sliceSerialize(token)
51+
).toLowerCase()
52+
}
53+
54+
function exitFootnoteDefinition(token) {
55+
this.exit(token)
56+
}
57+
58+
function enterFootnoteCall(token) {
59+
this.enter({type: 'footnoteReference', identifier: '', label: ''}, token)
60+
}
61+
62+
function enterFootnoteCallString() {
63+
this.buffer()
64+
}
65+
66+
function exitFootnoteCallString(token) {
67+
var label = this.resume()
68+
this.stack[this.stack.length - 1].label = label
69+
this.stack[this.stack.length - 1].identifier = normalizeIdentifier(
70+
this.sliceSerialize(token)
71+
).toLowerCase()
72+
}
73+
74+
function exitFootnoteCall(token) {
75+
this.exit(token)
76+
}
77+
78+
function enterNote(token) {
79+
this.enter({type: 'footnote', children: []}, token)
80+
}
81+
82+
function exitNote(token) {
83+
this.exit(token)
84+
}
85+
86+
function footnoteReference(node, _, context) {
87+
var exit = context.enter('footnoteReference')
88+
var subexit = context.enter('reference')
89+
var reference = safe(context, association(node), {before: '^', after: ']'})
90+
subexit()
91+
exit()
92+
return '[^' + reference + ']'
93+
}
94+
95+
function footnoteReferencePeek() {
96+
return '['
97+
}
98+
99+
function footnote(node, _, context) {
100+
var exit = context.enter('footnote')
101+
var subexit = context.enter('label')
102+
var value = '^[' + phrasing(node, context, {before: '[', after: ']'}) + ']'
103+
subexit()
104+
exit()
105+
return value
106+
}
107+
108+
function footnotePeek() {
109+
return '^'
110+
}
111+
112+
function footnoteDefinition(node, _, context) {
113+
var exit = context.enter('footnoteDefinition')
114+
var subexit = context.enter('label')
115+
var label =
116+
'[^' + safe(context, association(node), {before: '^', after: ']'}) + ']:'
117+
var value
118+
subexit()
119+
120+
value = indentLines(flow(node, context), map)
121+
exit()
122+
123+
return value
124+
125+
function map(line, index, blank) {
126+
if (index) {
127+
return (blank ? '' : ' ') + line
128+
}
129+
130+
return (blank ? label : label + ' ') + line
131+
}
132+
}

package.json

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,32 @@
2525
"contributors": [
2626
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2727
],
28+
"sideEffects": false,
29+
"type": "module",
30+
"main": "index.js",
2831
"files": [
29-
"from-markdown.js",
30-
"index.js",
31-
"to-markdown.js"
32+
"index.js"
3233
],
3334
"dependencies": {
3435
"mdast-util-to-markdown": "^0.6.0",
3536
"micromark": "~2.11.0"
3637
},
3738
"devDependencies": {
39+
"c8": "^7.0.0",
3840
"mdast-util-from-markdown": "^0.8.0",
3941
"micromark-extension-footnote": "~0.3.0",
40-
"nyc": "^15.0.0",
4142
"prettier": "^2.0.0",
4243
"remark-cli": "^9.0.0",
4344
"remark-preset-wooorm": "^8.0.0",
4445
"tape": "^5.0.0",
45-
"xo": "^0.38.0"
46+
"xo": "^0.39.0"
4647
},
4748
"scripts": {
4849
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
49-
"test-api": "node test",
50-
"test-coverage": "nyc --reporter lcov tape test.js",
50+
"test-api": "node --conditions development test.js",
51+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
5152
"test": "npm run format && npm run test-coverage"
5253
},
53-
"nyc": {
54-
"check-coverage": true,
55-
"lines": 100,
56-
"functions": 100,
57-
"branches": 100
58-
},
5954
"prettier": {
6055
"tabWidth": 2,
6156
"useTabs": false,
@@ -66,7 +61,10 @@
6661
},
6762
"xo": {
6863
"prettier": true,
69-
"esnext": false
64+
"rules": {
65+
"no-var": "off",
66+
"prefer-arrow-callback": "off"
67+
}
7068
},
7169
"remarkConfig": {
7270
"plugins": [

readme.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ You probably shouldn’t use this package directly, but instead use
1818

1919
## Install
2020

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

2326
```sh
@@ -173,13 +176,13 @@ note.]
173176

174177
## API
175178

176-
### `footnote.fromMarkdown`
179+
This package exports the following identifier: `footnoteFromMarkdown`,
180+
`footnoteToMarkdown`.
181+
There is no default export.
177182

178-
### `footnote.toMarkdown`
183+
### `footnoteFromMarkdown`
179184

180-
> Note: the separate extensions are also available at
181-
> `mdast-util-footnote/from-markdown` and
182-
> `mdast-util-footnote/to-markdown`.
185+
### `footnoteToMarkdown`
183186

184187
Support footnotes.
185188
These exports are extensions, respectively for

0 commit comments

Comments
 (0)