Skip to content

Commit 309f2a4

Browse files
committed
Use ESM & update micromark
1 parent 71df1f1 commit 309f2a4

12 files changed

+145
-130
lines changed

Diff for: .babelrc

-14
This file was deleted.

Diff for: .gitignore

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
.nyc_output/
21
coverage/
3-
dist/
4-
micromark/
52
node_modules/
3+
/lib/
4+
/index.js
65
*.log
76
.DS_Store
87
yarn.lock
9-
mdast-util-from-markdown.min.js

Diff for: .prettierignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
coverage/
2-
micromark/
3-
*.json
42
*.md

Diff for: dev/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {fromMarkdown} from './lib/index.js'

Diff for: lib/index.js renamed to dev/lib/index.js

+70-35
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
1-
'use strict'
2-
3-
module.exports = fromMarkdown
4-
5-
// These three are compiled away in the `dist/`
6-
var codes = require('micromark/dist/character/codes')
7-
var constants = require('micromark/dist/constant/constants')
8-
var types = require('micromark/dist/constant/types')
9-
10-
var toString = require('mdast-util-to-string')
11-
var assign = require('micromark/dist/constant/assign')
12-
var own = require('micromark/dist/constant/has-own-property')
13-
var normalizeIdentifier = require('micromark/dist/util/normalize-identifier')
14-
var safeFromInt = require('micromark/dist/util/safe-from-int')
15-
var parser = require('micromark/dist/parse')
16-
var preprocessor = require('micromark/dist/preprocess')
17-
var postprocess = require('micromark/dist/postprocess')
18-
var decode = require('parse-entities/decode-entity')
19-
var stringifyPosition = require('unist-util-stringify-position')
20-
21-
function fromMarkdown(value, encoding, options) {
1+
import {toString} from 'mdast-util-to-string'
2+
import {parse} from 'micromark/lib/parse.js'
3+
import {preprocess} from 'micromark/lib/preprocess.js'
4+
import {postprocess} from 'micromark/lib/postprocess.js'
5+
import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
6+
import {codes} from 'micromark-util-symbol/codes.js'
7+
import {values} from 'micromark-util-symbol/values.js'
8+
import {constants} from 'micromark-util-symbol/constants.js'
9+
import {types} from 'micromark-util-symbol/types.js'
10+
import {decodeEntity} from 'parse-entities/decode-entity.js'
11+
import {stringifyPosition} from 'unist-util-stringify-position'
12+
13+
const own = {}.hasOwnProperty
14+
15+
export function fromMarkdown(value, encoding, options) {
2216
if (typeof encoding !== 'string') {
2317
options = encoding
2418
encoding = undefined
2519
}
2620

2721
return compiler(options)(
2822
postprocess(
29-
parser(options).document().write(preprocessor()(value, encoding, true))
23+
parse(options).document().write(preprocess()(value, encoding, true))
3024
)
3125
)
3226
}
@@ -155,15 +149,15 @@ function compiler(options) {
155149
var listStart
156150

157151
var context = {
158-
stack: stack,
159-
tokenStack: tokenStack,
160-
config: config,
161-
enter: enter,
162-
exit: exit,
163-
buffer: buffer,
164-
resume: resume,
165-
setData: setData,
166-
getData: getData
152+
stack,
153+
tokenStack,
154+
config,
155+
enter,
156+
exit,
157+
buffer,
158+
resume,
159+
setData,
160+
getData
167161
}
168162

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

190184
if (own.call(handler, events[index][1].type)) {
191185
handler[events[index][1].type].call(
192-
assign({sliceSerialize: events[index][2].sliceSerialize}, context),
186+
Object.assign(
187+
{sliceSerialize: events[index][2].sliceSerialize},
188+
context
189+
),
193190
events[index][1]
194191
)
195192
}
@@ -472,16 +469,18 @@ function compiler(options) {
472469

473470
function onexitcodefenced() {
474471
var data = this.resume()
472+
475473
this.stack[this.stack.length - 1].value = data.replace(
476474
/^(\r?\n|\r)|(\r?\n|\r)$/g,
477475
''
478476
)
477+
479478
setData('flowCodeInside')
480479
}
481480

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

487486
function onexitdefinitionlabelstring(token) {
@@ -679,15 +678,15 @@ function compiler(options) {
679678
var tail
680679

681680
if (type) {
682-
value = safeFromInt(
681+
value = parseNumericCharacterReference(
683682
data,
684683
type === types.characterReferenceMarkerNumeric
685684
? constants.numericBaseDecimal
686685
: constants.numericBaseHexadecimal
687686
)
688687
setData('characterReferenceType')
689688
} else {
690-
value = decode(data)
689+
value = decodeEntity(data)
691690
}
692691

693692
tail = this.stack.pop()
@@ -816,3 +815,39 @@ function extension(config, extension) {
816815
}
817816
}
818817
}
818+
819+
// To do: externalize this from `micromark/lib/compile`
820+
/**
821+
* Turn the number (in string form as either hexa- or plain decimal) coming from
822+
* a numeric character reference into a character.
823+
*
824+
* @param {string} value
825+
* @param {number} base
826+
* @returns {string}
827+
*/
828+
function parseNumericCharacterReference(value, base) {
829+
const code = Number.parseInt(value, base)
830+
831+
if (
832+
// C0 except for HT, LF, FF, CR, space
833+
code < codes.ht ||
834+
code === codes.vt ||
835+
(code > codes.cr && code < codes.space) ||
836+
// Control character (DEL) of the basic block and C1 controls.
837+
(code > codes.tilde && code < 160) ||
838+
// Lone high surrogates and low surrogates.
839+
/* c8 ignore next */
840+
(code > 55295 && code < 57344) ||
841+
// Noncharacters.
842+
/* c8 ignore next */
843+
(code > 64975 && code < 65008) ||
844+
(code & 65535) === 65535 ||
845+
(code & 65535) === 65534 ||
846+
// Out of range
847+
code > 1114111
848+
) {
849+
return values.replacementCharacter
850+
}
851+
852+
return String.fromCharCode(code)
853+
}

Diff for: index.js

-3
This file was deleted.

Diff for: package.json

+28-33
Original file line numberDiff line numberDiff line change
@@ -26,56 +26,49 @@
2626
"contributors": [
2727
"Titus Wormer <[email protected]> (https://wooorm.com)"
2828
],
29+
"sideEffects": false,
30+
"type": "module",
31+
"main": "index.js",
2932
"files": [
30-
"dist/",
33+
"dev/",
3134
"lib/",
32-
"index.js",
33-
"types/index.d.ts"
35+
"index.js"
3436
],
35-
"types": "types",
37+
"exports": {
38+
"development": "./dev/index.js",
39+
"default": "./index.js"
40+
},
3641
"dependencies": {
3742
"@types/mdast": "^3.0.0",
38-
"mdast-util-to-string": "^2.0.0",
39-
"micromark": "~2.11.0",
40-
"parse-entities": "^2.0.0",
41-
"unist-util-stringify-position": "^2.0.0"
43+
"mdast-util-to-string": "^3.0.0",
44+
"micromark": "^3.0.0-alpha.3",
45+
"micromark-util-normalize-identifier": "^1.0.0-alpha.3",
46+
"micromark-util-symbol": "^1.0.0-alpha.3",
47+
"parse-entities": "^3.0.0",
48+
"unist-util-stringify-position": "^3.0.0"
4249
},
4350
"devDependencies": {
44-
"@babel/cli": "^7.0.0",
45-
"@babel/core": "^7.0.0",
46-
"babel-plugin-inline-constants": "^1.0.0",
47-
"browserify": "^17.0.0",
48-
"commonmark.json": "^0.29.0",
49-
"dtslint": "^4.0.0",
51+
"c8": "^7.0.0",
52+
"commonmark.json": "^0.30.0",
5053
"gzip-size-cli": "^5.0.0",
51-
"hast-util-to-html": "^7.0.0",
52-
"mdast-util-to-hast": "^10.0.0",
53-
"nyc": "^15.0.0",
54+
"hast-util-to-html": "^8.0.0",
55+
"mdast-util-to-hast": "^11.0.0",
56+
"micromark-build": "^1.0.0-alpha.1",
5457
"prettier": "^2.0.0",
5558
"rehype-parse": "^7.0.0",
5659
"rehype-stringify": "^8.0.0",
5760
"remark-cli": "^9.0.0",
5861
"remark-preset-wooorm": "^8.0.0",
5962
"tape": "^5.0.0",
60-
"tinyify": "^3.0.0",
6163
"unified": "^9.0.0",
62-
"xo": "^0.38.0"
64+
"xo": "^0.39.0"
6365
},
6466
"scripts": {
67+
"build": "micromark-build",
6568
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
66-
"generate-dist": "babel lib/ --out-dir dist/ --quiet --retain-lines; prettier dist/index.js --loglevel error --write",
67-
"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",
68-
"generate": "npm run generate-dist && npm run generate-size",
69-
"test-api": "node test",
70-
"test-coverage": "nyc --reporter lcov tape test/index.js",
71-
"test-types": "dtslint types",
72-
"test": "npm run format && npm run generate && npm run test-coverage && npm run test-types"
73-
},
74-
"nyc": {
75-
"check-coverage": true,
76-
"lines": 100,
77-
"functions": 100,
78-
"branches": 100
69+
"test-api": "node --conditions development test/index.js",
70+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test/index.js",
71+
"test": "npm run build && npm run format && npm run test-coverage"
7972
},
8073
"prettier": {
8174
"tabWidth": 2,
@@ -87,10 +80,12 @@
8780
},
8881
"xo": {
8982
"prettier": true,
90-
"esnext": false,
9183
"rules": {
84+
"no-var": "off",
85+
"prefer-arrow-callback": "off",
9286
"complexity": "off",
9387
"guard-for-in": "off",
88+
"unicorn/prefer-switch": "off",
9489
"unicorn/explicit-length-check": "off",
9590
"unicorn/no-array-callback-reference": "off",
9691
"unicorn/prefer-includes": "off",

Diff for: readme.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
## Install
1414

15+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
17+
1518
[npm][]:
1619

1720
```sh
@@ -29,12 +32,12 @@ Say we have the following markdown file, `example.md`:
2932
And our script, `example.js`, looks as follows:
3033

3134
```js
32-
var fs = require('fs')
33-
var fromMarkdown = require('mdast-util-from-markdown')
35+
import fs from 'node:fs'
36+
import {fromMarkdown} from 'mdast-util-from-markdown'
3437

35-
var doc = fs.readFileSync('example.md')
38+
const doc = fs.readFileSync('example.md')
3639

37-
var tree = fromMarkdown(doc)
40+
const tree = fromMarkdown(doc)
3841

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

6467
## API
6568

69+
This package exports the following identifier: `fromMarkdown`.
70+
There is no default export.
71+
6672
### `fromMarkdown(doc[, encoding][, options])`
6773

6874
Parse markdown to a **[mdast][]** tree.

Diff for: test/fixtures/blockquote.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@
233233
"offset": 111
234234
},
235235
"end": {
236-
"line": 13,
237-
"column": 1,
238-
"offset": 144
236+
"line": 12,
237+
"column": 29,
238+
"offset": 143
239239
}
240240
}
241241
}
@@ -247,9 +247,9 @@
247247
"offset": 95
248248
},
249249
"end": {
250-
"line": 13,
251-
"column": 1,
252-
"offset": 144
250+
"line": 12,
251+
"column": 29,
252+
"offset": 143
253253
}
254254
}
255255
},

0 commit comments

Comments
 (0)