Skip to content

Commit

Permalink
Use ESM & update micromark
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 17, 2021
1 parent 71df1f1 commit 309f2a4
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 130 deletions.
14 changes: 0 additions & 14 deletions .babelrc

This file was deleted.

6 changes: 2 additions & 4 deletions .gitignore
@@ -1,9 +1,7 @@
.nyc_output/
coverage/
dist/
micromark/
node_modules/
/lib/
/index.js
*.log
.DS_Store
yarn.lock
mdast-util-from-markdown.min.js
2 changes: 0 additions & 2 deletions .prettierignore
@@ -1,4 +1,2 @@
coverage/
micromark/
*.json
*.md
1 change: 1 addition & 0 deletions dev/index.js
@@ -0,0 +1 @@
export {fromMarkdown} from './lib/index.js'
105 changes: 70 additions & 35 deletions lib/index.js → dev/lib/index.js
@@ -1,32 +1,26 @@
'use strict'

module.exports = fromMarkdown

// These three are compiled away in the `dist/`
var codes = require('micromark/dist/character/codes')
var constants = require('micromark/dist/constant/constants')
var types = require('micromark/dist/constant/types')

var toString = require('mdast-util-to-string')
var assign = require('micromark/dist/constant/assign')
var own = require('micromark/dist/constant/has-own-property')
var normalizeIdentifier = require('micromark/dist/util/normalize-identifier')
var safeFromInt = require('micromark/dist/util/safe-from-int')
var parser = require('micromark/dist/parse')
var preprocessor = require('micromark/dist/preprocess')
var postprocess = require('micromark/dist/postprocess')
var decode = require('parse-entities/decode-entity')
var stringifyPosition = require('unist-util-stringify-position')

function fromMarkdown(value, encoding, options) {
import {toString} from 'mdast-util-to-string'
import {parse} from 'micromark/lib/parse.js'
import {preprocess} from 'micromark/lib/preprocess.js'
import {postprocess} from 'micromark/lib/postprocess.js'
import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
import {codes} from 'micromark-util-symbol/codes.js'
import {values} from 'micromark-util-symbol/values.js'
import {constants} from 'micromark-util-symbol/constants.js'
import {types} from 'micromark-util-symbol/types.js'
import {decodeEntity} from 'parse-entities/decode-entity.js'
import {stringifyPosition} from 'unist-util-stringify-position'

const own = {}.hasOwnProperty

export function fromMarkdown(value, encoding, options) {
if (typeof encoding !== 'string') {
options = encoding
encoding = undefined
}

return compiler(options)(
postprocess(
parser(options).document().write(preprocessor()(value, encoding, true))
parse(options).document().write(preprocess()(value, encoding, true))
)
)
}
Expand Down Expand Up @@ -155,15 +149,15 @@ function compiler(options) {
var listStart

var context = {
stack: stack,
tokenStack: tokenStack,
config: config,
enter: enter,
exit: exit,
buffer: buffer,
resume: resume,
setData: setData,
getData: getData
stack,
tokenStack,
config,
enter,
exit,
buffer,
resume,
setData,
getData
}

while (++index < events.length) {
Expand All @@ -189,7 +183,10 @@ function compiler(options) {

if (own.call(handler, events[index][1].type)) {
handler[events[index][1].type].call(
assign({sliceSerialize: events[index][2].sliceSerialize}, context),
Object.assign(
{sliceSerialize: events[index][2].sliceSerialize},
context
),
events[index][1]
)
}
Expand Down Expand Up @@ -472,16 +469,18 @@ function compiler(options) {

function onexitcodefenced() {
var data = this.resume()

this.stack[this.stack.length - 1].value = data.replace(
/^(\r?\n|\r)|(\r?\n|\r)$/g,
''
)

setData('flowCodeInside')
}

function onexitcodeindented() {
var data = this.resume()
this.stack[this.stack.length - 1].value = data
this.stack[this.stack.length - 1].value = data.replace(/(\r?\n|\r)$/g, '')
}

function onexitdefinitionlabelstring(token) {
Expand Down Expand Up @@ -679,15 +678,15 @@ function compiler(options) {
var tail

if (type) {
value = safeFromInt(
value = parseNumericCharacterReference(
data,
type === types.characterReferenceMarkerNumeric
? constants.numericBaseDecimal
: constants.numericBaseHexadecimal
)
setData('characterReferenceType')
} else {
value = decode(data)
value = decodeEntity(data)
}

tail = this.stack.pop()
Expand Down Expand Up @@ -816,3 +815,39 @@ function extension(config, extension) {
}
}
}

// To do: externalize this from `micromark/lib/compile`
/**
* Turn the number (in string form as either hexa- or plain decimal) coming from
* a numeric character reference into a character.
*
* @param {string} value
* @param {number} base
* @returns {string}
*/
function parseNumericCharacterReference(value, base) {
const code = Number.parseInt(value, base)

if (
// C0 except for HT, LF, FF, CR, space
code < codes.ht ||
code === codes.vt ||
(code > codes.cr && code < codes.space) ||
// Control character (DEL) of the basic block and C1 controls.
(code > codes.tilde && code < 160) ||
// Lone high surrogates and low surrogates.
/* c8 ignore next */
(code > 55295 && code < 57344) ||
// Noncharacters.
/* c8 ignore next */
(code > 64975 && code < 65008) ||
(code & 65535) === 65535 ||
(code & 65535) === 65534 ||
// Out of range
code > 1114111
) {
return values.replacementCharacter
}

return String.fromCharCode(code)
}
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

61 changes: 28 additions & 33 deletions package.json
Expand Up @@ -26,56 +26,49 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"files": [
"dist/",
"dev/",
"lib/",
"index.js",
"types/index.d.ts"
"index.js"
],
"types": "types",
"exports": {
"development": "./dev/index.js",
"default": "./index.js"
},
"dependencies": {
"@types/mdast": "^3.0.0",
"mdast-util-to-string": "^2.0.0",
"micromark": "~2.11.0",
"parse-entities": "^2.0.0",
"unist-util-stringify-position": "^2.0.0"
"mdast-util-to-string": "^3.0.0",
"micromark": "^3.0.0-alpha.3",
"micromark-util-normalize-identifier": "^1.0.0-alpha.3",
"micromark-util-symbol": "^1.0.0-alpha.3",
"parse-entities": "^3.0.0",
"unist-util-stringify-position": "^3.0.0"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"babel-plugin-inline-constants": "^1.0.0",
"browserify": "^17.0.0",
"commonmark.json": "^0.29.0",
"dtslint": "^4.0.0",
"c8": "^7.0.0",
"commonmark.json": "^0.30.0",
"gzip-size-cli": "^5.0.0",
"hast-util-to-html": "^7.0.0",
"mdast-util-to-hast": "^10.0.0",
"nyc": "^15.0.0",
"hast-util-to-html": "^8.0.0",
"mdast-util-to-hast": "^11.0.0",
"micromark-build": "^1.0.0-alpha.1",
"prettier": "^2.0.0",
"rehype-parse": "^7.0.0",
"rehype-stringify": "^8.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"unified": "^9.0.0",
"xo": "^0.38.0"
"xo": "^0.39.0"
},
"scripts": {
"build": "micromark-build",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"generate-dist": "babel lib/ --out-dir dist/ --quiet --retain-lines; prettier dist/index.js --loglevel error --write",
"generate-size": "browserify . -p tinyify -s mdast-util-from-markdown -o mdast-util-from-markdown.min.js; gzip-size mdast-util-from-markdown.min.js --raw",
"generate": "npm run generate-dist && npm run generate-size",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test/index.js",
"test-types": "dtslint types",
"test": "npm run format && npm run generate && 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 build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -87,10 +80,12 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"no-var": "off",
"prefer-arrow-callback": "off",
"complexity": "off",
"guard-for-in": "off",
"unicorn/prefer-switch": "off",
"unicorn/explicit-length-check": "off",
"unicorn/no-array-callback-reference": "off",
"unicorn/prefer-includes": "off",
Expand Down
14 changes: 10 additions & 4 deletions readme.md
Expand Up @@ -12,6 +12,9 @@

## 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 All @@ -29,12 +32,12 @@ Say we have the following markdown file, `example.md`:
And our script, `example.js`, looks as follows:

```js
var fs = require('fs')
var fromMarkdown = require('mdast-util-from-markdown')
import fs from 'node:fs'
import {fromMarkdown} from 'mdast-util-from-markdown'

var doc = fs.readFileSync('example.md')
const doc = fs.readFileSync('example.md')

var tree = fromMarkdown(doc)
const tree = fromMarkdown(doc)

console.log(tree)
```
Expand Down Expand Up @@ -63,6 +66,9 @@ Now, running `node example` yields (positional info removed for brevity):

## API

This package exports the following identifier: `fromMarkdown`.
There is no default export.

### `fromMarkdown(doc[, encoding][, options])`

Parse markdown to a **[mdast][]** tree.
Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/blockquote.json
Expand Up @@ -233,9 +233,9 @@
"offset": 111
},
"end": {
"line": 13,
"column": 1,
"offset": 144
"line": 12,
"column": 29,
"offset": 143
}
}
}
Expand All @@ -247,9 +247,9 @@
"offset": 95
},
"end": {
"line": 13,
"column": 1,
"offset": 144
"line": 12,
"column": 29,
"offset": 143
}
}
},
Expand Down

0 comments on commit 309f2a4

Please sign in to comment.