diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 2784fec..0000000 --- a/.babelrc +++ /dev/null @@ -1,14 +0,0 @@ -{ - "plugins": [ - [ - "babel-plugin-inline-constants", - { - "modules": [ - "micromark/dist/character/codes", - "micromark/dist/constant/constants", - "micromark/dist/constant/types" - ] - } - ] - ] -} diff --git a/.gitignore b/.gitignore index 64dd5ef..590db2c 100644 --- a/.gitignore +++ b/.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 diff --git a/.prettierignore b/.prettierignore index 03e8cb1..cebe81f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,2 @@ coverage/ -micromark/ -*.json *.md diff --git a/dev/index.js b/dev/index.js new file mode 100644 index 0000000..808745b --- /dev/null +++ b/dev/index.js @@ -0,0 +1 @@ +export {fromMarkdown} from './lib/index.js' diff --git a/lib/index.js b/dev/lib/index.js similarity index 90% rename from lib/index.js rename to dev/lib/index.js index 9d931b0..7a885bd 100644 --- a/lib/index.js +++ b/dev/lib/index.js @@ -1,24 +1,18 @@ -'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 @@ -26,7 +20,7 @@ function fromMarkdown(value, encoding, options) { return compiler(options)( postprocess( - parser(options).document().write(preprocessor()(value, encoding, true)) + parse(options).document().write(preprocess()(value, encoding, true)) ) ) } @@ -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) { @@ -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] ) } @@ -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) { @@ -679,7 +678,7 @@ function compiler(options) { var tail if (type) { - value = safeFromInt( + value = parseNumericCharacterReference( data, type === types.characterReferenceMarkerNumeric ? constants.numericBaseDecimal @@ -687,7 +686,7 @@ function compiler(options) { ) setData('characterReferenceType') } else { - value = decode(data) + value = decodeEntity(data) } tail = this.stack.pop() @@ -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) +} diff --git a/index.js b/index.js deleted file mode 100644 index 2e3b467..0000000 --- a/index.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict' - -module.exports = require('./dist/index.js') diff --git a/package.json b/package.json index 45aef47..c1fa976 100644 --- a/package.json +++ b/package.json @@ -26,56 +26,49 @@ "contributors": [ "Titus Wormer (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, @@ -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", diff --git a/readme.md b/readme.md index 3036214..518a053 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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) ``` @@ -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. diff --git a/test/fixtures/blockquote.json b/test/fixtures/blockquote.json index 8bbede6..75e751f 100644 --- a/test/fixtures/blockquote.json +++ b/test/fixtures/blockquote.json @@ -233,9 +233,9 @@ "offset": 111 }, "end": { - "line": 13, - "column": 1, - "offset": 144 + "line": 12, + "column": 29, + "offset": 143 } } } @@ -247,9 +247,9 @@ "offset": 95 }, "end": { - "line": 13, - "column": 1, - "offset": 144 + "line": 12, + "column": 29, + "offset": 143 } } }, diff --git a/test/index.js b/test/index.js index ff0918a..2903e0f 100644 --- a/test/index.js +++ b/test/index.js @@ -1,15 +1,13 @@ -'use strict' - -var fs = require('fs') -var path = require('path') -var test = require('tape') -var unified = require('unified') -var rehypeParse = require('rehype-parse') -var rehypeStringify = require('rehype-stringify') -var toHast = require('mdast-util-to-hast') -var toHtml = require('hast-util-to-html') -var commonmark = require('commonmark.json') -var fromMarkdown = require('..') +import fs from 'fs' +import path from 'path' +import test from 'tape' +import unified from 'unified' +import rehypeParse from 'rehype-parse' +import rehypeStringify from 'rehype-stringify' +import {toHast} from 'mdast-util-to-hast' +import {toHtml} from 'hast-util-to-html' +import {commonmark} from 'commonmark.json' +import {fromMarkdown} from '../dev/index.js' var join = path.join @@ -924,6 +922,7 @@ test('fixtures', function (t) { test('commonmark', function (t) { let index = -1 + while (++index < commonmark.length) { each(commonmark[index], index) } @@ -950,6 +949,10 @@ test('commonmark', function (t) { var actual = reformat.processSync(html).toString() var expected = reformat.processSync(example.html.slice(0, -1)).toString() + if (actual !== expected) { + console.log('yyy', [example, actual, expected]) + } + t.equal(actual, expected, example.section + ' (' + index + ')') } }) diff --git a/types/tsconfig.json b/types/tsconfig.json index 9a36131..644465d 100644 --- a/types/tsconfig.json +++ b/types/tsconfig.json @@ -1,15 +1,11 @@ { - "compilerOptions": { - "moduleResolution": "node", - "lib": [ - "ES5" - ], - "strict": true, - "baseUrl": ".", - "paths": { - "mdast-util-from-markdown": [ - "./index.d.ts" - ] - } + "compilerOptions": { + "moduleResolution": "node", + "lib": ["ES5"], + "strict": true, + "baseUrl": ".", + "paths": { + "mdast-util-from-markdown": ["./index.d.ts"] } + } } diff --git a/types/tslint.json b/types/tslint.json index 9001796..70c4494 100644 --- a/types/tslint.json +++ b/types/tslint.json @@ -1,7 +1,7 @@ { - "extends": "dtslint/dtslint.json", - "rules": { - "semicolon": false, - "whitespace": false - } + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": false, + "whitespace": false + } }