Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 19, 2021
1 parent 04214da commit a840260
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 113 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,4 +1,3 @@
coverage/
*.html
*.json
*.md
42 changes: 0 additions & 42 deletions from-markdown.js

This file was deleted.

59 changes: 57 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,57 @@
exports.fromMarkdown = require('./from-markdown.js')
exports.toMarkdown = require('./to-markdown.js')
import gfmAutolinkLiteralFromMarkdown from 'mdast-util-gfm-autolink-literal/from-markdown.js'
import gfmAutolinkLiteralToMarkdown from 'mdast-util-gfm-autolink-literal/to-markdown.js'
import gfmStrikethroughFromMarkdown from 'mdast-util-gfm-strikethrough/from-markdown.js'
import gfmStrikethroughToMarkdown from 'mdast-util-gfm-strikethrough/to-markdown.js'
import gfmTableFromMarkdown from 'mdast-util-gfm-table/from-markdown.js'
import gfmTableToMarkdown from 'mdast-util-gfm-table/to-markdown.js'
import gfmTaskListItemFromMarkdown from 'mdast-util-gfm-task-list-item/from-markdown.js'
import gfmTaskListItemToMarkdown from 'mdast-util-gfm-task-list-item/to-markdown.js'

var own = {}.hasOwnProperty

export const gfmFromMarkdown = configure([
gfmAutolinkLiteralFromMarkdown,
gfmStrikethroughFromMarkdown,
gfmTableFromMarkdown,
gfmTaskListItemFromMarkdown
])

export function gfmToMarkdown(options) {
return {
extensions: [
gfmAutolinkLiteralToMarkdown,
gfmStrikethroughToMarkdown,
gfmTableToMarkdown(options),
gfmTaskListItemToMarkdown
]
}
}

function configure(extensions) {
var config = {transforms: [], canContainEols: []}
var length = extensions.length
var index = -1

while (++index < length) {
extension(config, extensions[index])
}

return config
}

function extension(config, extension) {
var key
var left
var right

for (key in extension) {
left = own.call(config, key) ? config[key] : (config[key] = {})
right = extension[key]

if (key === 'canContainEols' || key === 'transforms') {
config[key] = [].concat(left, right)
} else {
Object.assign(left, right)
}
}
}
28 changes: 12 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
"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": {
"mdast-util-gfm-autolink-literal": "^0.1.0",
Expand All @@ -43,32 +44,26 @@
"mdast-util-to-markdown": "^0.6.1"
},
"devDependencies": {
"c8": "^7.0.0",
"github-slugger": "^1.0.0",
"hast-util-to-html": "^7.0.0",
"hast-util-to-html": "^8.0.0",
"mdast-util-from-markdown": "^0.8.0",
"mdast-util-to-hast": "^10.0.0",
"mdast-util-to-hast": "^11.0.0",
"micromark-extension-gfm": "^0.3.0",
"node-fetch": "^2.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"crawl": "node script/crawl-tests",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test-api": "node --conditions development test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test/index.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 @@ -79,8 +74,9 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"guard-for-in": "off",
"unicorn/prefer-optional-catch-binding": "off"
}
Expand Down
13 changes: 8 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Alternatively, the extensions can be used separately:

## 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 @@ -212,13 +215,13 @@ Now, running `node example` yields:

## API

### `gfm.fromMarkdown`
This package exports the following identifier: `gfmFromMarkdown`,
`gfmToMarkdown`.
There is no default export.

### `gfm.toMarkdown(options?)`
### `gfmFromMarkdown`

> Note: the separate extensions are also available at
> `mdast-util-gfm/from-markdown` and
> `mdast-util-gfm/to-markdown`.
### `gfmToMarkdown(options?)`

Support GFM.
The exports of `fromMarkdown` is an extension for
Expand Down
8 changes: 3 additions & 5 deletions script/crawl-tests.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

var fs = require('fs')
var path = require('path')
var fetch = require('node-fetch')
import fs from 'fs'
import path from 'path'
import fetch from 'node-fetch'

fetch(
'https://api.github.com/repos/micromark/micromark-extension-gfm/contents/test/spec.json',
Expand Down
35 changes: 18 additions & 17 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
var fs = require('fs')
var path = require('path')
var test = require('tape')
var slug = require('github-slugger').slug
var toHast = require('mdast-util-to-hast')
var toHtml = require('hast-util-to-html')
var fromMarkdown = require('mdast-util-from-markdown')
var toMarkdown = require('mdast-util-to-markdown')
var syntax = require('micromark-extension-gfm')()
var gfm = require('../index.js')
var spec = require('./spec.json')
import fs from 'fs'
import path from 'path'
import test from 'tape'
import Slugger from 'github-slugger'
import {toHast} from 'mdast-util-to-hast'
import {toHtml} from 'hast-util-to-html'
import fromMarkdown from 'mdast-util-from-markdown'
import toMarkdown from 'mdast-util-to-markdown'
import gfm from 'micromark-extension-gfm'
import {gfmFromMarkdown, gfmToMarkdown} from '../index.js'

const spec = JSON.parse(fs.readFileSync(path.join('test', 'spec.json')))

test('markdown -> mdast', function (t) {
const files = spec.filter(
Expand All @@ -18,19 +19,19 @@ test('markdown -> mdast', function (t) {

while (++index < files.length) {
const example = files[index]
var category = slug(example.category)
var category = Slugger.slug(example.category)
var name = index + '-' + category
var fixtureHtmlPath = path.join(__dirname, name + '.html')
var fixtureMarkdownPath = path.join(__dirname, name + '.md')
var fixtureHtmlPath = path.join('test', name + '.html')
var fixtureMarkdownPath = path.join('test', name + '.md')
var fixtureHtml
var fixtureMarkdown
var mdast
var html
var md

mdast = fromMarkdown(example.input, {
extensions: [syntax],
mdastExtensions: [gfm.fromMarkdown]
extensions: [gfm()],
mdastExtensions: [gfmFromMarkdown]
})

html = toHtml(toHast(mdast, {allowDangerousHtml: true, commonmark: true}), {
Expand All @@ -45,7 +46,7 @@ test('markdown -> mdast', function (t) {
fixtureHtml = example.output.slice(0, -1)
}

md = toMarkdown(mdast, {extensions: [gfm.toMarkdown()]})
md = toMarkdown(mdast, {extensions: [gfmToMarkdown()]})

try {
fixtureMarkdown = String(fs.readFileSync(fixtureMarkdownPath))
Expand Down
22 changes: 0 additions & 22 deletions to-markdown.js

This file was deleted.

0 comments on commit a840260

Please sign in to comment.