Skip to content

Commit 700db23

Browse files
committed
Use ESM
1 parent eb3b83b commit 700db23

8 files changed

+153
-161
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 & 40 deletions
This file was deleted.

index.js

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,81 @@
1-
exports.fromMarkdown = require('./from-markdown.js')
2-
exports.toMarkdown = require('./to-markdown.js')
1+
import matters from 'micromark-extension-frontmatter/lib/matters.js'
2+
3+
export function frontmatterFromMarkdown(options) {
4+
var settings = matters(options)
5+
var length = settings.length
6+
var index = -1
7+
var enter = {}
8+
var exit = {}
9+
var matter
10+
11+
while (++index < length) {
12+
matter = settings[index]
13+
enter[matter.type] = opener(matter)
14+
exit[matter.type] = close
15+
exit[matter.type + 'Value'] = value
16+
}
17+
18+
return {enter, exit}
19+
}
20+
21+
function opener(matter) {
22+
return open
23+
function open(token) {
24+
this.enter({type: matter.type, value: ''}, token)
25+
this.buffer()
26+
}
27+
}
28+
29+
function close(token) {
30+
var data = this.resume()
31+
// Remove the initial and final eol.
32+
this.exit(token).value = data.replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
33+
}
34+
35+
function value(token) {
36+
this.config.enter.data.call(this, token)
37+
this.config.exit.data.call(this, token)
38+
}
39+
40+
export function frontmatterToMarkdown(options) {
41+
var unsafe = []
42+
var handlers = {}
43+
var settings = matters(options)
44+
var length = settings.length
45+
var index = -1
46+
var matter
47+
48+
while (++index < length) {
49+
matter = settings[index]
50+
handlers[matter.type] = handler(matter)
51+
unsafe.push({atBreak: true, character: fence(matter, 'open').charAt(0)})
52+
}
53+
54+
return {unsafe, handlers}
55+
}
56+
57+
function handler(matter) {
58+
var open = fence(matter, 'open')
59+
var close = fence(matter, 'close')
60+
61+
return handle
62+
63+
function handle(node) {
64+
return open + (node.value ? '\n' + node.value : '') + '\n' + close
65+
}
66+
}
67+
68+
function fence(matter, prop) {
69+
var marker
70+
71+
if (matter.marker) {
72+
marker = pick(matter.marker, prop)
73+
return marker + marker + marker
74+
}
75+
76+
return pick(matter.fence, prop)
77+
}
78+
79+
function pick(schema, prop) {
80+
return typeof schema === 'string' ? schema : schema[prop]
81+
}

package.json

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,32 @@
2626
"contributors": [
2727
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2828
],
29+
"sideEffects": false,
30+
"type": "module",
31+
"main": "index.js",
2932
"files": [
30-
"from-markdown.js",
31-
"index.js",
32-
"to-markdown.js"
33+
"index.js"
3334
],
3435
"dependencies": {
3536
"micromark-extension-frontmatter": "^0.2.0"
3637
},
3738
"devDependencies": {
39+
"c8": "^7.0.0",
3840
"mdast-util-from-markdown": "^0.8.0",
3941
"mdast-util-to-markdown": "^0.6.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-
"unist-util-remove-position": "^3.0.0",
46-
"xo": "^0.37.0"
46+
"unist-util-remove-position": "^4.0.0",
47+
"xo": "^0.39.0"
4748
},
4849
"scripts": {
4950
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
50-
"test-api": "node test",
51-
"test-coverage": "nyc --reporter lcov tape test.js",
51+
"test-api": "node --conditions development test.js",
52+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
5253
"test": "npm run format && npm run test-coverage"
5354
},
54-
"nyc": {
55-
"check-coverage": true,
56-
"lines": 100,
57-
"functions": 100,
58-
"branches": 100
59-
},
6055
"prettier": {
6156
"tabWidth": 2,
6257
"useTabs": false,
@@ -67,7 +62,10 @@
6762
},
6863
"xo": {
6964
"prettier": true,
70-
"esnext": false
65+
"rules": {
66+
"no-var": "off",
67+
"prefer-arrow-callback": "off"
68+
}
7169
},
7270
"remarkConfig": {
7371
"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
@@ -85,13 +88,13 @@ title = "New Website"
8588

8689
## API
8790

88-
### `frontmatter.fromMarkdown([options])`
91+
This package exports the following identifiers: `frontmatterFromMarkdown`,
92+
`frontmatterToMarkdown`.
93+
There is no default export.
8994

90-
### `frontmatter.toMarkdown([options])`
95+
### `frontmatterFromMarkdown([options])`
9196

92-
> Note: the separate extensions are also available at
93-
> `mdast-util-frontmatter/from-markdown` and
94-
> `mdast-util-frontmatter/to-markdown`.
97+
### `frontmatterToMarkdown([options])`
9598

9699
Support frontmatter (YAML, TOML, and more).
97100
These functions can be called with options and return extensions, respectively

0 commit comments

Comments
 (0)