Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 9, 2021
1 parent dd2b6f9 commit 9863a07
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 198 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/
*.log
.DS_Store
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
8 changes: 0 additions & 8 deletions html.js

This file was deleted.

26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
module.exports = require('./syntax.js')
import {
combineExtensions,
combineHtmlExtensions
} from 'micromark-util-combine-extensions'
import autolink from 'micromark-extension-gfm-autolink-literal'
import autolinkHtml from 'micromark-extension-gfm-autolink-literal/html.js'
import strikethrough from 'micromark-extension-gfm-strikethrough'
import strikethroughHtml from 'micromark-extension-gfm-strikethrough/html.js'
import table from 'micromark-extension-gfm-table'
import tableHtml from 'micromark-extension-gfm-table/html.js'
import tagfilterHtml from 'micromark-extension-gfm-tagfilter/html.js'
import tasklist from 'micromark-extension-gfm-task-list-item'
import tasklistHtml from 'micromark-extension-gfm-task-list-item/html.js'

export function gfm(options) {
return combineExtensions([autolink, strikethrough(options), table, tasklist])
}

export const gfmHtml = combineHtmlExtensions([
autolinkHtml,
strikethroughHtml,
tableHtml,
tagfilterHtml,
tasklistHtml
])
42 changes: 18 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,39 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"types": "types/index.d.ts",
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"types/*.d.ts",
"index.js",
"html.js",
"syntax.js"
"index.js"
],
"dependencies": {
"micromark": "~2.11.0",
"micromark-extension-gfm-autolink-literal": "~0.5.0",
"micromark-extension-gfm-strikethrough": "~0.6.5",
"micromark-extension-gfm-table": "~0.4.0",
"micromark-extension-gfm-tagfilter": "~0.3.0",
"micromark-extension-gfm-task-list-item": "~0.3.0"
"micromark-extension-gfm-task-list-item": "~0.3.0",
"micromark-util-combine-extensions": "^1.0.0-alpha.2"
},
"devDependencies": {
"dtslint": "^4.0.0",
"hast-util-select": "^4.0.0",
"hast-util-to-text": "^2.0.0",
"node-fetch": "^2.6.1",
"nyc": "^15.0.0",
"c8": "^7.0.0",
"hast-util-select": "^5.0.0",
"hast-util-to-text": "^3.0.0",
"micromark": "^3.0.0-alpha.2",
"node-fetch": "^2.0.0",
"prettier": "^2.0.0",
"rehype-parse": "^7.0.1",
"rehype-parse": "^7.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"unified": "^9.0.0",
"xo": "^0.38.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/index.js",
"test-types": "dtslint types",
"test": "npm run format && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
"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"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -83,6 +75,8 @@
"types"
],
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"guard-for-in": "off",
"unicorn/no-array-for-each": "off"
}
Expand Down
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,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 @@ -118,9 +121,12 @@ Now, running `node example` yields:

## API

### `html`
This package exports the following identifiers: `gfm`, `gfmHtml`.
There is no default export.

### `gfm(options?)`

### `syntax(options?)`
### `gfmHtml`

> Note: `syntax` is the default export of this module, `html` is available at
> `micromark-extension-gfm/html`.
Expand Down
36 changes: 17 additions & 19 deletions script/crawl-tests.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
'use strict'

var fs = require('fs')
var path = require('path')
var fetch = require('node-fetch')
var unified = require('unified')
var parse = require('rehype-parse')
var select = require('hast-util-select')
var text = require('hast-util-to-text')
import fs from 'fs'
import path from 'path'
import fetch from 'node-fetch'
import unified from 'unified'
import parse from 'rehype-parse'
import {select, selectAll} from 'hast-util-select'
import {toText} from 'hast-util-to-text'

fetch('https://github.github.com/gfm/')
.then((response) => response.text())
.then((doc) => {
var tree = unified().use(parse).parse(doc)
var data = []

select.selectAll('div.extension', tree).forEach(($extension) => {
var $heading = select.select('h2', $extension)
var category = text($heading)
selectAll('div.extension', tree).forEach(($extension) => {
var $heading = select('h2', $extension)
var category = toText($heading)
// Remove number.
.replace(/^\d+\.\d+\s*/, '')
// Remove extension.
.replace(/\s*\(extension\)$/, '')

select.selectAll('.example', $extension).forEach(($example) => {
var columns = select.selectAll('.column pre', $example)
selectAll('.example', $extension).forEach(($example) => {
var columns = selectAll('.column pre', $example)

data.push({
category: category,
input: text(columns[0]).replace(//g, '\t'),
output: text(columns[1]).replace(//g, '\t')
category,
input: toText(columns[0]).replace(//g, '\t'),
output: toText(columns[1]).replace(//g, '\t')
})
})
})
Expand All @@ -37,8 +35,8 @@ fetch('https://github.github.com/gfm/')
})
.then((data) => {
return fs.promises.writeFile(
path.join('test', 'spec.json'),
JSON.stringify(data, null, 2) + '\n'
path.join('test', 'spec.js'),
'export const spec = ' + JSON.stringify(data, null, 2) + '\n'
)
})
.then(() => {
Expand Down
11 changes: 0 additions & 11 deletions syntax.js

This file was deleted.

13 changes: 6 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
var test = require('tape')
var micromark = require('micromark')
var syntax = require('../syntax.js')({singleTilde: false})
var html = require('../html.js')
var spec = require('./spec.json')
import test from 'tape'
import {micromark} from 'micromark'
import {gfm, gfmHtml} from '../index.js'
import {spec} from './spec.js'

test('markdown -> html (micromark)', function (t) {
spec.forEach((example, index) => {
t.deepEqual(
micromark(example.input, {
allowDangerousHtml: true,
allowDangerousProtocol: true,
extensions: [syntax],
htmlExtensions: [html]
extensions: [gfm({singleTilde: false})],
htmlExtensions: [gfmHtml]
}),
example.output,
example.category + ' (' + index + ')'
Expand Down
147 changes: 147 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
export const spec = [
{
category: 'Tables',
input: '| foo | bar |\n| --- | --- |\n| baz | bim |\n',
output:
'<table>\n<thead>\n<tr>\n<th>foo</th>\n<th>bar</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>baz</td>\n<td>bim</td>\n</tr>\n</tbody>\n</table>\n'
},
{
category: 'Tables',
input: '| abc | defghi |\n:-: | -----------:\nbar | baz\n',
output:
'<table>\n<thead>\n<tr>\n<th align="center">abc</th>\n<th align="right">defghi</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td align="center">bar</td>\n<td align="right">baz</td>\n</tr>\n</tbody>\n</table>\n'
},
{
category: 'Tables',
input: '| f\\|oo |\n| ------ |\n| b `\\|` az |\n| b **\\|** im |\n',
output:
'<table>\n<thead>\n<tr>\n<th>f|oo</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>b <code>|</code> az</td>\n</tr>\n<tr>\n<td>b <strong>|</strong> im</td>\n</tr>\n</tbody>\n</table>\n'
},
{
category: 'Tables',
input: '| abc | def |\n| --- | --- |\n| bar | baz |\n> bar\n',
output:
'<table>\n<thead>\n<tr>\n<th>abc</th>\n<th>def</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>bar</td>\n<td>baz</td>\n</tr>\n</tbody>\n</table>\n<blockquote>\n<p>bar</p>\n</blockquote>\n'
},
{
category: 'Tables',
input: '| abc | def |\n| --- | --- |\n| bar | baz |\nbar\n\nbar\n',
output:
'<table>\n<thead>\n<tr>\n<th>abc</th>\n<th>def</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>bar</td>\n<td>baz</td>\n</tr>\n<tr>\n<td>bar</td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<p>bar</p>\n'
},
{
category: 'Tables',
input: '| abc | def |\n| --- |\n| bar |\n',
output: '<p>| abc | def |\n| --- |\n| bar |</p>\n'
},
{
category: 'Tables',
input: '| abc | def |\n| --- | --- |\n| bar |\n| bar | baz | boo |\n',
output:
'<table>\n<thead>\n<tr>\n<th>abc</th>\n<th>def</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>bar</td>\n<td></td>\n</tr>\n<tr>\n<td>bar</td>\n<td>baz</td>\n</tr>\n</tbody>\n</table>\n'
},
{
category: 'Tables',
input: '| abc | def |\n| --- | --- |\n',
output:
'<table>\n<thead>\n<tr>\n<th>abc</th>\n<th>def</th>\n</tr>\n</thead>\n</table>\n'
},
{
category: 'Task list items',
input: '- [ ] foo\n- [x] bar\n',
output:
'<ul>\n<li><input disabled="" type="checkbox"> foo</li>\n<li><input checked="" disabled="" type="checkbox"> bar</li>\n</ul>\n'
},
{
category: 'Task list items',
input: '- [x] foo\n - [ ] bar\n - [x] baz\n- [ ] bim\n',
output:
'<ul>\n<li><input checked="" disabled="" type="checkbox"> foo\n<ul>\n<li><input disabled="" type="checkbox"> bar</li>\n<li><input checked="" disabled="" type="checkbox"> baz</li>\n</ul>\n</li>\n<li><input disabled="" type="checkbox"> bim</li>\n</ul>\n'
},
{
category: 'Strikethrough',
input: '~~Hi~~ Hello, world!\n',
output: '<p><del>Hi</del> Hello, world!</p>\n'
},
{
category: 'Strikethrough',
input: 'This ~~has a\n\nnew paragraph~~.\n',
output: '<p>This ~~has a</p>\n<p>new paragraph~~.</p>\n'
},
{
category: 'Autolinks',
input: 'www.commonmark.org\n',
output:
'<p><a href="http://www.commonmark.org">www.commonmark.org</a></p>\n'
},
{
category: 'Autolinks',
input: 'Visit www.commonmark.org/help for more information.\n',
output:
'<p>Visit <a href="http://www.commonmark.org/help">www.commonmark.org/help</a> for more information.</p>\n'
},
{
category: 'Autolinks',
input: 'Visit www.commonmark.org.\n\nVisit www.commonmark.org/a.b.\n',
output:
'<p>Visit <a href="http://www.commonmark.org">www.commonmark.org</a>.</p>\n<p>Visit <a href="http://www.commonmark.org/a.b">www.commonmark.org/a.b</a>.</p>\n'
},
{
category: 'Autolinks',
input:
'www.google.com/search?q=Markup+(business)\n\nwww.google.com/search?q=Markup+(business)))\n\n(www.google.com/search?q=Markup+(business))\n\n(www.google.com/search?q=Markup+(business)\n',
output:
'<p><a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a></p>\n<p><a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a>))</p>\n<p>(<a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a>)</p>\n<p>(<a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a></p>\n'
},
{
category: 'Autolinks',
input: 'www.google.com/search?q=(business))+ok\n',
output:
'<p><a href="http://www.google.com/search?q=(business))+ok">www.google.com/search?q=(business))+ok</a></p>\n'
},
{
category: 'Autolinks',
input:
'www.google.com/search?q=commonmark&hl=en\n\nwww.google.com/search?q=commonmark&hl;\n',
output:
'<p><a href="http://www.google.com/search?q=commonmark&amp;hl=en">www.google.com/search?q=commonmark&amp;hl=en</a></p>\n<p><a href="http://www.google.com/search?q=commonmark">www.google.com/search?q=commonmark</a>&amp;hl;</p>\n'
},
{
category: 'Autolinks',
input: 'www.commonmark.org/he<lp\n',
output:
'<p><a href="http://www.commonmark.org/he">www.commonmark.org/he</a>&lt;lp</p>\n'
},
{
category: 'Autolinks',
input:
'http://commonmark.org\n\n(Visit https://encrypted.google.com/search?q=Markup+(business))\n',
output:
'<p><a href="http://commonmark.org">http://commonmark.org</a></p>\n<p>(Visit <a href="https://encrypted.google.com/search?q=Markup+(business)">https://encrypted.google.com/search?q=Markup+(business)</a>)</p>\n'
},
{
category: 'Autolinks',
input: 'foo@bar.baz\n',
output: '<p><a href="mailto:foo@bar.baz">foo@bar.baz</a></p>\n'
},
{
category: 'Autolinks',
input:
"hello@mail+xyz.example isn't valid, but hello+xyz@mail.example is.\n",
output:
'<p>hello@mail+xyz.example isn\'t valid, but <a href="mailto:hello+xyz@mail.example">hello+xyz@mail.example</a> is.</p>\n'
},
{
category: 'Autolinks',
input: 'a.b-c_d@a.b\n\na.b-c_d@a.b.\n\na.b-c_d@a.b-\n\na.b-c_d@a.b_\n',
output:
'<p><a href="mailto:a.b-c_d@a.b">a.b-c_d@a.b</a></p>\n<p><a href="mailto:a.b-c_d@a.b">a.b-c_d@a.b</a>.</p>\n<p>a.b-c_d@a.b-</p>\n<p>a.b-c_d@a.b_</p>\n'
},
{
category: 'Disallowed Raw HTML',
input:
'<strong> <title> <style> <em>\n\n<blockquote>\n <xmp> is disallowed. <XMP> is also disallowed.\n</blockquote>\n',
output:
'<p><strong> &lt;title> &lt;style> <em></p>\n<blockquote>\n &lt;xmp> is disallowed. &lt;XMP> is also disallowed.\n</blockquote>\n'
}
]
Loading

0 comments on commit 9863a07

Please sign in to comment.