Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 18, 2021
1 parent eb3b83b commit 700db23
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 161 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
.DS_Store
*.log
yarn.lock
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
coverage/
*.json
*.md
40 changes: 0 additions & 40 deletions from-markdown.js

This file was deleted.

83 changes: 81 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,81 @@
exports.fromMarkdown = require('./from-markdown.js')
exports.toMarkdown = require('./to-markdown.js')
import matters from 'micromark-extension-frontmatter/lib/matters.js'

export function frontmatterFromMarkdown(options) {
var settings = matters(options)
var length = settings.length
var index = -1
var enter = {}
var exit = {}
var matter

while (++index < length) {
matter = settings[index]
enter[matter.type] = opener(matter)
exit[matter.type] = close
exit[matter.type + 'Value'] = value
}

return {enter, exit}
}

function opener(matter) {
return open
function open(token) {
this.enter({type: matter.type, value: ''}, token)
this.buffer()
}
}

function close(token) {
var data = this.resume()
// Remove the initial and final eol.
this.exit(token).value = data.replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
}

function value(token) {
this.config.enter.data.call(this, token)
this.config.exit.data.call(this, token)
}

export function frontmatterToMarkdown(options) {
var unsafe = []
var handlers = {}
var settings = matters(options)
var length = settings.length
var index = -1
var matter

while (++index < length) {
matter = settings[index]
handlers[matter.type] = handler(matter)
unsafe.push({atBreak: true, character: fence(matter, 'open').charAt(0)})
}

return {unsafe, handlers}
}

function handler(matter) {
var open = fence(matter, 'open')
var close = fence(matter, 'close')

return handle

function handle(node) {
return open + (node.value ? '\n' + node.value : '') + '\n' + close
}
}

function fence(matter, prop) {
var marker

if (matter.marker) {
marker = pick(matter.marker, prop)
return marker + marker + marker
}

return pick(matter.fence, prop)
}

function pick(schema, prop) {
return typeof schema === 'string' ? schema : schema[prop]
}
28 changes: 13 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,32 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"from-markdown.js",
"index.js",
"to-markdown.js"
"index.js"
],
"dependencies": {
"micromark-extension-frontmatter": "^0.2.0"
},
"devDependencies": {
"c8": "^7.0.0",
"mdast-util-from-markdown": "^0.8.0",
"mdast-util-to-markdown": "^0.6.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"unist-util-remove-position": "^3.0.0",
"xo": "^0.37.0"
"unist-util-remove-position": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
Expand All @@ -67,7 +62,10 @@
},
"xo": {
"prettier": true,
"esnext": false
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off"
}
},
"remarkConfig": {
"plugins": [
Expand Down
13 changes: 8 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ You probably shouldn’t use this package directly, but instead use

## 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 Down Expand Up @@ -85,13 +88,13 @@ title = "New Website"

## API

### `frontmatter.fromMarkdown([options])`
This package exports the following identifiers: `frontmatterFromMarkdown`,
`frontmatterToMarkdown`.
There is no default export.

### `frontmatter.toMarkdown([options])`
### `frontmatterFromMarkdown([options])`

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

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

0 comments on commit 700db23

Please sign in to comment.