Browse Source

Rewrite core to es6 modules

pull/979/head
Vitaly Puzrin 1 year ago
parent
commit
e92e776411
  1. 6
      .eslintrc.yml
  2. 2
      Procfile
  3. 4
      README.md
  4. 138
      benchmark/benchmark.js
  5. 110
      benchmark/benchmark.mjs
  6. 9
      benchmark/implementations/commonmark-reference/index.js
  7. 11
      benchmark/implementations/commonmark-reference/index.mjs
  8. 8
      benchmark/implementations/current-commonmark/index.mjs
  9. 11
      benchmark/implementations/current/index.js
  10. 11
      benchmark/implementations/current/index.mjs
  11. 7
      benchmark/implementations/markdown-it-2.2.1-commonmark/index.js
  12. 10
      benchmark/implementations/markdown-it-2.2.1-commonmark/index.mjs
  13. 7
      benchmark/implementations/marked/index.js
  14. 8
      benchmark/implementations/marked/index.mjs
  15. 19
      benchmark/profile.js
  16. 17
      benchmark/profile.mjs
  17. 2
      benchmark/samples/README.md
  18. 13
      bin/markdown-it.mjs
  19. 4
      index.js
  20. 1
      index.mjs
  21. 5
      lib/.eslintrc.yml
  22. 5
      lib/common/html_blocks.mjs
  23. 5
      lib/common/html_re.mjs
  24. 66
      lib/common/utils.mjs
  25. 7
      lib/helpers/index.js
  26. 11
      lib/helpers/index.mjs
  27. 9
      lib/helpers/parse_link_destination.mjs
  28. 5
      lib/helpers/parse_link_label.mjs
  29. 8
      lib/helpers/parse_link_title.mjs
  30. 36
      lib/index.mjs
  31. 44
      lib/parser_block.mjs
  32. 61
      lib/parser_core.js
  33. 68
      lib/parser_core.mjs
  34. 56
      lib/parser_inline.mjs
  35. 5
      lib/presets/commonmark.mjs
  36. 5
      lib/presets/default.mjs
  37. 5
      lib/presets/zero.mjs
  38. 9
      lib/renderer.mjs
  39. 3
      lib/ruler.mjs
  40. 9
      lib/rules_block/blockquote.mjs
  41. 7
      lib/rules_block/code.mjs
  42. 7
      lib/rules_block/fence.mjs
  43. 9
      lib/rules_block/heading.mjs
  44. 9
      lib/rules_block/hr.mjs
  45. 11
      lib/rules_block/html_block.mjs
  46. 7
      lib/rules_block/lheading.mjs
  47. 9
      lib/rules_block/list.mjs
  48. 7
      lib/rules_block/paragraph.mjs
  49. 11
      lib/rules_block/reference.mjs
  50. 8
      lib/rules_block/state_block.mjs
  51. 9
      lib/rules_block/table.mjs
  52. 7
      lib/rules_core/block.mjs
  53. 6
      lib/rules_core/inline.mjs
  54. 8
      lib/rules_core/linkify.mjs
  55. 6
      lib/rules_core/normalize.mjs
  56. 5
      lib/rules_core/replacements.mjs
  57. 10
      lib/rules_core/smartquotes.mjs
  58. 6
      lib/rules_core/state_core.mjs
  59. 6
      lib/rules_core/text_join.mjs
  60. 7
      lib/rules_inline/autolink.mjs
  61. 7
      lib/rules_inline/backticks.mjs
  62. 6
      lib/rules_inline/balance_pairs.mjs
  63. 13
      lib/rules_inline/emphasis.mjs
  64. 11
      lib/rules_inline/entity.mjs
  65. 8
      lib/rules_inline/escape.mjs
  66. 6
      lib/rules_inline/fragments_join.mjs
  67. 9
      lib/rules_inline/html_inline.mjs
  68. 9
      lib/rules_inline/image.mjs
  69. 10
      lib/rules_inline/link.mjs
  70. 7
      lib/rules_inline/linkify.mjs
  71. 9
      lib/rules_inline/newline.mjs
  72. 12
      lib/rules_inline/state_inline.mjs
  73. 14
      lib/rules_inline/strikethrough.mjs
  74. 7
      lib/rules_inline/text.mjs
  75. 5
      lib/token.mjs
  76. 9
      package.json
  77. 11
      support/babelmark-responder.mjs
  78. 23
      support/specsplit.mjs
  79. 4
      test/.eslintrc.yml
  80. 9
      test/babelmark-responder.mjs
  81. 2
      test/commonmark.mjs
  82. 2
      test/markdown-it.mjs
  83. 2
      test/misc.mjs
  84. 7
      test/pathological.mjs
  85. 6
      test/pathological_worker.js
  86. 2
      test/ruler.mjs
  87. 2
      test/token.mjs
  88. 2
      test/utils.mjs

6
.eslintrc.yml

@ -1,11 +1,17 @@
env: env:
es6: true
node: true node: true
parserOptions:
ecmaVersion: 2022
overrides: overrides:
- -
files: [ '*.mjs' ] files: [ '*.mjs' ]
parserOptions: parserOptions:
sourceType: module sourceType: module
rules:
no-restricted-globals: [ 2, require, __dirname ]
ignorePatterns: ignorePatterns:
- coverage/ - coverage/

2
Procfile

@ -1 +1 @@
web: node support/babelmark-responder.js web: node support/babelmark-responder.mjs

4
README.md

@ -253,8 +253,8 @@ You can find all rules in sources:
Here is the result of readme parse at MB Pro Retina 2013 (2.4 GHz): Here is the result of readme parse at MB Pro Retina 2013 (2.4 GHz):
```bash ```bash
make benchmark-deps npm run benchmark-deps
benchmark/benchmark.js readme benchmark/benchmark.mjs readme
Selected samples: (1 of 28) Selected samples: (1 of 28)
> README > README

138
benchmark/benchmark.js

@ -1,138 +0,0 @@
#!/usr/bin/env node
/*eslint no-console:0*/
'use strict';
var path = require('path');
var fs = require('fs');
var util = require('util');
var Benchmark = require('benchmark');
var ansi = require('ansi');
var cursor = ansi(process.stdout);
var IMPLS_DIRECTORY = path.join(__dirname, 'implementations');
var IMPLS_PATHS = {};
var IMPLS = [];
fs.readdirSync(IMPLS_DIRECTORY).sort().forEach(function (name) {
var file = path.join(IMPLS_DIRECTORY, name);
var code = require(file);
IMPLS_PATHS[name] = file;
IMPLS.push({
name: name,
code: code
});
});
var SAMPLES_DIRECTORY = path.join(__dirname, 'samples');
var SAMPLES = [];
fs.readdirSync(SAMPLES_DIRECTORY).sort().forEach(function (sample) {
var filepath = path.join(SAMPLES_DIRECTORY, sample),
extname = path.extname(filepath),
basename = path.basename(filepath, extname);
var content = {};
content.string = fs.readFileSync(filepath, 'utf8');
var title = util.format('(%d bytes)', content.string.length);
function onComplete() {
cursor.write('\n');
}
var suite = new Benchmark.Suite(title, {
onStart: function onStart() {
console.log('\nSample: %s %s', sample, title);
},
onComplete: onComplete
});
IMPLS.forEach(function (impl) {
suite.add(impl.name, {
onCycle: function onCycle(event) {
cursor.horizontalAbsolute();
cursor.eraseLine();
cursor.write(' > ' + event.target);
},
onComplete: onComplete,
fn: function () {
impl.code.run(content.string);
return;
}
});
});
SAMPLES.push({
name: basename,
title: title,
content: content,
suite: suite
});
});
function select(patterns) {
var result = [];
if (!(patterns instanceof Array)) {
patterns = [ patterns ];
}
function checkName(name) {
return patterns.length === 0 || patterns.some(function (regexp) {
return regexp.test(name);
});
}
SAMPLES.forEach(function (sample) {
if (checkName(sample.name)) {
result.push(sample);
}
});
return result;
}
function run(files) {
var selected = select(files);
if (selected.length > 0) {
console.log('Selected samples: (%d of %d)', selected.length, SAMPLES.length);
selected.forEach(function (sample) {
console.log(' > %s', sample.name);
});
} else {
console.log('There isn\'t any sample matches any of these patterns: %s', util.inspect(files));
}
selected.forEach(function (sample) {
sample.suite.run();
});
}
module.exports.IMPLS_DIRECTORY = IMPLS_DIRECTORY;
module.exports.IMPLS_PATHS = IMPLS_PATHS;
module.exports.IMPLS = IMPLS;
module.exports.SAMPLES_DIRECTORY = SAMPLES_DIRECTORY;
module.exports.SAMPLES = SAMPLES;
module.exports.select = select;
module.exports.run = run;
run(process.argv.slice(2).map(function (source) {
return new RegExp(source, 'i');
}));

110
benchmark/benchmark.mjs

@ -0,0 +1,110 @@
#!/usr/bin/env node
/*eslint no-console:0*/
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
var fs = require('fs');
var util = require('util');
var Benchmark = require('benchmark');
var ansi = require('ansi');
var cursor = ansi(process.stdout);
var IMPLS = [];
for (const name of fs.readdirSync(new URL('./implementations', import.meta.url)).sort()) {
const filepath = new URL(`./implementations/${name}/index.mjs`, import.meta.url);
const code = (await import(filepath));
IMPLS.push({
name: name,
code: code
});
}
const SAMPLES = [];
fs.readdirSync(new URL('./samples', import.meta.url)).sort().forEach(sample => {
const filepath = new URL(`./samples/${sample}`, import.meta.url);
const content = {};
content.string = fs.readFileSync(filepath, 'utf8');
var title = `(${content.string.length} bytes)`;
function onComplete() { cursor.write('\n'); }
var suite = new Benchmark.Suite(
title,
{
onStart: () => { console.log('\nSample: %s %s', sample, title); },
onComplete: onComplete
}
);
IMPLS.forEach(function (impl) {
suite.add(
impl.name,
{
onCycle: function onCycle(event) {
cursor.horizontalAbsolute();
cursor.eraseLine();
cursor.write(' > ' + event.target);
},
onComplete: onComplete,
fn: function () { impl.code.run(content.string); }
}
);
});
SAMPLES.push({
name: sample.split('.')[0],
title: title,
content: content,
suite: suite
});
});
function select(patterns) {
var result = [];
if (!(patterns instanceof Array)) {
patterns = [ patterns ];
}
function checkName(name) {
return patterns.length === 0 || patterns.some(function (regexp) {
return regexp.test(name);
});
}
SAMPLES.forEach(function (sample) {
if (checkName(sample.name)) {
result.push(sample);
}
});
return result;
}
function run(files) {
var selected = select(files);
if (selected.length > 0) {
console.log('Selected samples: (%d of %d)', selected.length, SAMPLES.length);
selected.forEach(function (sample) {
console.log(' > %s', sample.name);
});
} else {
console.log('There isn\'t any sample matches any of these patterns: %s', util.inspect(files));
}
selected.forEach(function (sample) {
sample.suite.run();
});
}
run(process.argv.slice(2).map(source => new RegExp(source, 'i')));

9
benchmark/implementations/commonmark-reference/index.js

@ -1,9 +0,0 @@
'use strict';
var commonmark = require('../../extra/lib/node_modules/commonmark');
var parser = new commonmark.Parser();
var renderer = new commonmark.HtmlRenderer();
exports.run = function (data) {
return renderer.render(parser.parse(data));
};

11
benchmark/implementations/commonmark-reference/index.mjs

@ -0,0 +1,11 @@
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const commonmark = require('../../extra/lib/node_modules/commonmark');
var parser = new commonmark.Parser();
var renderer = new commonmark.HtmlRenderer();
export function run(data) {
return renderer.render(parser.parse(data));
}

8
benchmark/implementations/current-commonmark/index.js → benchmark/implementations/current-commonmark/index.mjs

@ -1,6 +1,6 @@
'use strict'; import markdownit from '../../../index.mjs';
var md = require('../../../')('commonmark'); var md = markdownit('commonmark');
// Replace normalizers to more primitive, for more "honest" compare. // Replace normalizers to more primitive, for more "honest" compare.
// Default ones can cause 1.5x slowdown. // Default ones can cause 1.5x slowdown.
@ -9,6 +9,6 @@ var encode = md.utils.lib.mdurl.encode;
md.normalizeLink = function (url) { return encode(url); }; md.normalizeLink = function (url) { return encode(url); };
md.normalizeLinkText = function (str) { return str; }; md.normalizeLinkText = function (str) { return str; };
exports.run = function (data) { export function run(data) {
return md.render(data); return md.render(data);
}; }

11
benchmark/implementations/current/index.js

@ -1,11 +0,0 @@
'use strict';
var md = require('../../../')({
html: true,
linkify: true,
typographer: true
});
exports.run = function (data) {
return md.render(data);
};

11
benchmark/implementations/current/index.mjs

@ -0,0 +1,11 @@
import markdownit from '../../../index.mjs';
var md = markdownit({
html: true,
linkify: true,
typographer: true
});
export function run(data) {
return md.render(data);
}

7
benchmark/implementations/markdown-it-2.2.1-commonmark/index.js

@ -1,7 +0,0 @@
'use strict';
var md = require('../../extra/lib/node_modules/markdown-it')('commonmark');
exports.run = function (data) {
return md.render(data);
};

10
benchmark/implementations/markdown-it-2.2.1-commonmark/index.mjs

@ -0,0 +1,10 @@
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const markdownit = require('../../extra/lib/node_modules/markdown-it');
var md = markdownit('commonmark');
export function run(data) {
return md.render(data);
}

7
benchmark/implementations/marked/index.js

@ -1,7 +0,0 @@
'use strict';
var marked = require('../../extra/lib/node_modules/marked');
exports.run = function (data) {
return marked(data);
};

8
benchmark/implementations/marked/index.mjs

@ -0,0 +1,8 @@
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const marked = require('../../extra/lib/node_modules/marked');
export function run(data) {
return marked(data);
}

19
benchmark/profile.js

@ -1,19 +0,0 @@
#!/usr/bin/env node
/*eslint no-console:0*/
'use strict';
var fs = require('fs');
var path = require('path');
var md = require('../')({
html: true,
linkify: false,
typographer: false
});
// var data = fs.readFileSync(path.join(__dirname, '/samples/lorem1.txt'), 'utf8');
var data = fs.readFileSync(path.join(__dirname, '../test/fixtures/commonmark/spec.txt'), 'utf8');
for (var i = 0; i < 20; i++) {
md.render(data);
}

17
benchmark/profile.mjs

@ -0,0 +1,17 @@
#!/usr/bin/env node
/*eslint no-console:0*/
import { readFileSync } from 'fs';
import markdownit from '../index.mjs';
var md = markdownit({
html: true,
linkify: false,
typographer: false
});
var data = readFileSync(new URL('../test/fixtures/commonmark/spec.txt', import.meta.url), 'utf8');
for (var i = 0; i < 20; i++) {
md.render(data);
}

2
benchmark/samples/README.md

@ -217,7 +217,7 @@ md = require('markdown-it')('full', {
Here is result of CommonMark spec parse at Core i5 2.4 GHz (i5-4258U): Here is result of CommonMark spec parse at Core i5 2.4 GHz (i5-4258U):
```bash ```bash
$ benchmark/benchmark.js spec $ benchmark/benchmark.mjs spec
Selected samples: (1 of 27) Selected samples: (1 of 27)
> spec > spec

13
bin/markdown-it.js → bin/markdown-it.mjs

@ -1,12 +1,9 @@
#!/usr/bin/env node #!/usr/bin/env node
/*eslint no-console:0*/ /*eslint no-console:0*/
'use strict'; import fs from 'node:fs';
import argparse from 'argparse';
import markdownit from '../index.mjs';
var fs = require('fs');
var argparse = require('argparse');
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -17,7 +14,7 @@ var cli = new argparse.ArgumentParser({
cli.add_argument('-v', '--version', { cli.add_argument('-v', '--version', {
action: 'version', action: 'version',
version: require('../package.json').version version: JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url))).version
}); });
cli.add_argument('--no-html', { cli.add_argument('--no-html', {
@ -89,7 +86,7 @@ readFile(options.file, 'utf8', function (err, input) {
process.exit(1); process.exit(1);
} }
md = require('..')({ md = markdownit({
html: !options.no_html, html: !options.no_html,
xhtmlOut: false, xhtmlOut: false,
typographer: options.typographer, typographer: options.typographer,

4
index.js

@ -1,4 +0,0 @@
'use strict';
module.exports = require('./lib/');

1
index.mjs

@ -0,0 +1 @@
export { default } from './lib/index.mjs';

5
lib/.eslintrc.yml

@ -0,0 +1,5 @@
env:
node: false
parserOptions:
ecmaVersion: 2015

5
lib/common/html_blocks.js → lib/common/html_blocks.mjs

@ -1,10 +1,7 @@
// List of valid html blocks names, according to commonmark spec // List of valid html blocks names, according to commonmark spec
// https://spec.commonmark.org/0.30/#html-blocks // https://spec.commonmark.org/0.30/#html-blocks
'use strict'; export default [
module.exports = [
'address', 'address',
'article', 'article',
'aside', 'aside',

5
lib/common/html_re.js → lib/common/html_re.mjs

@ -1,7 +1,5 @@
// Regexps to match html elements // Regexps to match html elements
'use strict';
var attr_name = '[a-zA-Z_:][a-zA-Z0-9:._-]*'; var attr_name = '[a-zA-Z_:][a-zA-Z0-9:._-]*';
var unquoted = '[^"\'=<>`\\x00-\\x20]+'; var unquoted = '[^"\'=<>`\\x00-\\x20]+';
@ -24,5 +22,4 @@ var HTML_TAG_RE = new RegExp('^(?:' + open_tag + '|' + close_tag + '|' + comment
'|' + processing + '|' + declaration + '|' + cdata + ')'); '|' + processing + '|' + declaration + '|' + cdata + ')');
var HTML_OPEN_CLOSE_TAG_RE = new RegExp('^(?:' + open_tag + '|' + close_tag + ')'); var HTML_OPEN_CLOSE_TAG_RE = new RegExp('^(?:' + open_tag + '|' + close_tag + ')');
module.exports.HTML_TAG_RE = HTML_TAG_RE; export { HTML_TAG_RE, HTML_OPEN_CLOSE_TAG_RE };
module.exports.HTML_OPEN_CLOSE_TAG_RE = HTML_OPEN_CLOSE_TAG_RE;

66
lib/common/utils.js → lib/common/utils.mjs

@ -1,7 +1,10 @@
// Utilities // Utilities
// //
'use strict';
import mdurl from 'mdurl';
import ucmicro from 'uc.micro';
import { decodeHTML } from 'entities';
import UNICODE_PUNCT_RE from 'uc.micro/categories/P/regex.js';
function _class(obj) { return Object.prototype.toString.call(obj); } function _class(obj) { return Object.prototype.toString.call(obj); }
@ -77,7 +80,6 @@ var UNESCAPE_ALL_RE = new RegExp(UNESCAPE_MD_RE.source + '|' + ENTITY_RE.source,
var DIGITAL_ENTITY_TEST_RE = /^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))$/i; var DIGITAL_ENTITY_TEST_RE = /^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))$/i;
var decodeHTML = require('entities').decodeHTML;
function replaceEntityPattern(match, name) { function replaceEntityPattern(match, name) {
var decoded, code; var decoded, code;
@ -185,7 +187,6 @@ function isWhiteSpace(code) {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/*eslint-disable max-len*/ /*eslint-disable max-len*/
var UNICODE_PUNCT_RE = require('uc.micro/categories/P/regex');
// Currently without astral characters support. // Currently without astral characters support.
function isPunctChar(ch) { function isPunctChar(ch) {
@ -298,23 +299,42 @@ function normalizeReference(str) {
// so plugins won't have to depend on them explicitly, which reduces their // so plugins won't have to depend on them explicitly, which reduces their
// bundled size (e.g. a browser build). // bundled size (e.g. a browser build).
// //
exports.lib = {}; const lib = { mdurl, ucmicro };
exports.lib.mdurl = require('mdurl');
exports.lib.ucmicro = require('uc.micro'); export {
lib,
exports.assign = assign; assign,
exports.isString = isString; isString,
exports.has = has; has,
exports.unescapeMd = unescapeMd; unescapeMd,
exports.unescapeAll = unescapeAll; unescapeAll,
exports.isValidEntityCode = isValidEntityCode; isValidEntityCode,
exports.fromCodePoint = fromCodePoint; fromCodePoint,
// exports.replaceEntities = replaceEntities; escapeHtml,
exports.escapeHtml = escapeHtml; arrayReplaceAt,
exports.arrayReplaceAt = arrayReplaceAt; isSpace,
exports.isSpace = isSpace; isWhiteSpace,
exports.isWhiteSpace = isWhiteSpace; isMdAsciiPunct,
exports.isMdAsciiPunct = isMdAsciiPunct; isPunctChar,
exports.isPunctChar = isPunctChar; escapeRE,
exports.escapeRE = escapeRE; normalizeReference
exports.normalizeReference = normalizeReference; };
export default {
lib,
assign,
isString,
has,
unescapeMd,
unescapeAll,
isValidEntityCode,
fromCodePoint,
escapeHtml,
arrayReplaceAt,
isSpace,
isWhiteSpace,
isMdAsciiPunct,
isPunctChar,
escapeRE,
normalizeReference
};

7
lib/helpers/index.js

@ -1,7 +0,0 @@
// Just a shortcut for bulk export
'use strict';
exports.parseLinkLabel = require('./parse_link_label');
exports.parseLinkDestination = require('./parse_link_destination');
exports.parseLinkTitle = require('./parse_link_title');

11
lib/helpers/index.mjs

@ -0,0 +1,11 @@
// Just a shortcut for bulk export
import parseLinkLabel from './parse_link_label.mjs';
import parseLinkDestination from './parse_link_destination.mjs';
import parseLinkTitle from './parse_link_title.mjs';
export default {
parseLinkLabel,
parseLinkDestination,
parseLinkTitle
};

9
lib/helpers/parse_link_destination.js → lib/helpers/parse_link_destination.mjs

@ -1,12 +1,9 @@
// Parse link destination // Parse link destination
// //
'use strict';
import { unescapeAll } from '../common/utils.mjs';
var unescapeAll = require('../common/utils').unescapeAll; export default function parseLinkDestination(str, start, max) {
module.exports = function parseLinkDestination(str, start, max) {
var code, level, var code, level,
pos = start, pos = start,
result = { result = {
@ -77,4 +74,4 @@ module.exports = function parseLinkDestination(str, start, max) {
result.pos = pos; result.pos = pos;
result.ok = true; result.ok = true;
return result; return result;
}; }

5
lib/helpers/parse_link_label.js → lib/helpers/parse_link_label.mjs

@ -3,9 +3,8 @@
// this function assumes that first character ("[") already matches; // this function assumes that first character ("[") already matches;
// returns the end of the label // returns the end of the label
// //
'use strict';
module.exports = function parseLinkLabel(state, start, disableNested) { export default function parseLinkLabel(state, start, disableNested) {
var level, found, marker, prevPos, var level, found, marker, prevPos,
labelEnd = -1, labelEnd = -1,
max = state.posMax, max = state.posMax,
@ -45,4 +44,4 @@ module.exports = function parseLinkLabel(state, start, disableNested) {
state.pos = oldPos; state.pos = oldPos;
return labelEnd; return labelEnd;
}; }

8
lib/helpers/parse_link_title.js → lib/helpers/parse_link_title.mjs

@ -1,12 +1,10 @@
// Parse link title // Parse link title
// //
'use strict';
import { unescapeAll } from '../common/utils.mjs';
var unescapeAll = require('../common/utils').unescapeAll;
export default function parseLinkTitle(str, start, max) {
module.exports = function parseLinkTitle(str, start, max) {
var code, var code,
marker, marker,
lines = 0, lines = 0,
@ -52,4 +50,4 @@ module.exports = function parseLinkTitle(str, start, max) {
} }
return result; return result;
}; }

36
lib/index.js → lib/index.mjs

@ -1,23 +1,23 @@
// Main parser class // Main parser class
'use strict'; import utils from './common/utils.mjs';
import helpers from './helpers/index.mjs';
import Renderer from './renderer.mjs';
var utils = require('./common/utils'); import ParserCore from './parser_core.mjs';
var helpers = require('./helpers'); import ParserBlock from './parser_block.mjs';
var Renderer = require('./renderer'); import ParserInline from './parser_inline.mjs';
var ParserCore = require('./parser_core'); import LinkifyIt from 'linkify-it';
var ParserBlock = require('./parser_block'); import mdurl from 'mdurl';
var ParserInline = require('./parser_inline'); import punycode from 'punycode';
var LinkifyIt = require('linkify-it');
var mdurl = require('mdurl'); import cfg_default from './presets/default.mjs';
var punycode = require('punycode/'); import cfg_zero from './presets/zero.mjs';
import cfg_commonmark from './presets/commonmark.mjs';
var config = { var config = {
default: require('./presets/default'), default: cfg_default,
zero: require('./presets/zero'), zero: cfg_zero,
commonmark: require('./presets/commonmark') commonmark: cfg_commonmark
}; };
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -329,7 +329,7 @@ function MarkdownIt(presetName, options) {
* MarkdownIt#utils -> utils * MarkdownIt#utils -> utils
* *
* Assorted utility functions, useful to write plugins. See details * Assorted utility functions, useful to write plugins. See details
* [here](https://github.com/markdown-it/markdown-it/blob/master/lib/common/utils.js). * [here](https://github.com/markdown-it/markdown-it/blob/master/lib/common/utils.mjs).
**/ **/
this.utils = utils; this.utils = utils;
@ -579,4 +579,4 @@ MarkdownIt.prototype.renderInline = function (src, env) {
}; };
module.exports = MarkdownIt; export default MarkdownIt;

44
lib/parser_block.js → lib/parser_block.mjs

@ -3,26 +3,36 @@
* *
* Block-level tokenizer. * Block-level tokenizer.
**/ **/
'use strict';
var Ruler = require('./ruler');
import Ruler from './ruler.mjs';
import StateBlock from './rules_block/state_block.mjs';
import r_table from './rules_block/table.mjs';
import r_code from './rules_block/code.mjs';
import r_fence from './rules_block/fence.mjs';
import r_blockquote from './rules_block/blockquote.mjs';
import r_hr from './rules_block/hr.mjs';
import r_list from './rules_block/list.mjs';
import r_reference from './rules_block/reference.mjs';
import r_html_block from './rules_block/html_block.mjs';
import r_heading from './rules_block/heading.mjs';
import r_lheading from './rules_block/lheading.mjs';
import r_paragraph from './rules_block/paragraph.mjs';
var _rules = [ var _rules = [
// First 2 params - rule name & source. Secondary array - list of rules, // First 2 params - rule name & source. Secondary array - list of rules,
// which can be terminated by this one. // which can be terminated by this one.
[ 'table', require('./rules_block/table'), [ 'paragraph', 'reference' ] ], [ 'table', r_table, [ 'paragraph', 'reference' ] ],
[ 'code', require('./rules_block/code') ], [ 'code', r_code ],
[ 'fence', require('./rules_block/fence'), [ 'paragraph', 'reference', 'blockquote', 'list' ] ], [ 'fence', r_fence, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
[ 'blockquote', require('./rules_block/blockquote'), [ 'paragraph', 'reference', 'blockquote', 'list' ] ], [ 'blockquote', r_blockquote, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
[ 'hr', require('./rules_block/hr'), [ 'paragraph', 'reference', 'blockquote', 'list' ] ], [ 'hr', r_hr, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
[ 'list', require('./rules_block/list'), [ 'paragraph', 'reference', 'blockquote' ] ], [ 'list', r_list, [ 'paragraph', 'reference', 'blockquote' ] ],
[ 'reference', require('./rules_block/reference') ], [ 'reference', r_reference ],
[ 'html_block', require('./rules_block/html_block'), [ 'paragraph', 'reference', 'blockquote' ] ], [ 'html_block', r_html_block, [ 'paragraph', 'reference', 'blockquote' ] ],
[ 'heading', require('./rules_block/heading'), [ 'paragraph', 'reference', 'blockquote' ] ], [ 'heading', r_heading, [ 'paragraph', 'reference', 'blockquote' ] ],
[ 'lheading', require('./rules_block/lheading') ], [ 'lheading', r_lheading ],
[ 'paragraph', require('./rules_block/paragraph') ] [ 'paragraph', r_paragraph ]
]; ];
@ -125,7 +135,7 @@ ParserBlock.prototype.parse = function (src, md, env, outTokens) {
}; };
ParserBlock.prototype.State = require('./rules_block/state_block'); ParserBlock.prototype.State = StateBlock;
module.exports = ParserBlock; export default ParserBlock;

61
lib/parser_core.js

@ -1,61 +0,0 @@
/** internal
* class Core
*
* Top-level rules executor. Glues block/inline parsers and does intermediate
* transformations.
**/
'use strict';
var Ruler = require('./ruler');
var _rules = [
[ 'normalize', require('./rules_core/normalize') ],
[ 'block', require('./rules_core/block') ],
[ 'inline', require('./rules_core/inline') ],
[ 'linkify', require('./rules_core/linkify') ],
[ 'replacements', require('./rules_core/replacements') ],
[ 'smartquotes', require('./rules_core/smartquotes') ],
// `text_join` finds `text_special` tokens (for escape sequences)
// and joins them with the rest of the text
[ 'text_join', require('./rules_core/text_join') ]
];
/**
* new Core()
**/
function Core() {
/**
* Core#ruler -> Ruler
*
* [[Ruler]] instance. Keep configuration of core rules.
**/
this.ruler = new Ruler();
for (var i = 0; i < _rules.length; i++) {
this.ruler.push(_rules[i][0], _rules[i][1]);
}
}
/**
* Core.process(state)
*
* Executes core chain rules.
**/
Core.prototype.process = function (state) {
var i, l, rules;
rules = this.ruler.getRules('');
for (i = 0, l = rules.length; i < l; i++) {
rules[i](state);
}
};
Core.prototype.State = require('./rules_core/state_core');
module.exports = Core;

68
lib/parser_core.mjs

@ -0,0 +1,68 @@
/** internal
* class Core
*
* Top-level rules executor. Glues block/inline parsers and does intermediate
* transformations.
**/
import Ruler from './ruler.mjs';
import StateCore from './rules_core/state_core.mjs';
import r_normalize from './rules_core/normalize.mjs';
import r_block from './rules_core/block.mjs';
import r_inline from './rules_core/inline.mjs';
import r_linkify from './rules_core/linkify.mjs';
import r_replacements from './rules_core/replacements.mjs';
import r_smartquotes from './rules_core/smartquotes.mjs';
import r_text_join from './rules_core/text_join.mjs';
var _rules = [
[ 'normalize', r_normalize ],
[ 'block', r_block ],
[ 'inline', r_inline ],
[ 'linkify', r_linkify ],
[ 'replacements', r_replacements ],
[ 'smartquotes', r_smartquotes ],
// `text_join` finds `text_special` tokens (for escape sequences)
// and joins them with the rest of the text
[ 'text_join', r_text_join ]
];
/**
* new Core()
**/
function Core() {
/**
* Core#ruler -> Ruler
*
* [[Ruler]] instance. Keep configuration of core rules.
**/
this.ruler = new Ruler();
for (var i = 0; i < _rules.length; i++) {
this.ruler.push(_rules[i][0], _rules[i][1]);
}
}
/**
* Core.process(state)
*
* Executes core chain rules.
**/
Core.prototype.process = function (state) {
var i, l, rules;
rules = this.ruler.getRules('');
for (i = 0, l = rules.length; i < l; i++) {
rules[i](state);
}
};
Core.prototype.State = StateCore;
export default Core;

56
lib/parser_inline.js → lib/parser_inline.mjs

@ -3,28 +3,44 @@
* *
* Tokenizes paragraph content. * Tokenizes paragraph content.
**/ **/
'use strict';
import Ruler from './ruler.mjs';
import StateInline from './rules_inline/state_inline.mjs';
import r_text from './rules_inline/text.mjs';
import r_linkify from './rules_inline/linkify.mjs';
import r_newline from './rules_inline/newline.mjs';
import r_escape from './rules_inline/escape.mjs';
import r_backticks from './rules_inline/backticks.mjs';
import r_strikethrough from './rules_inline/strikethrough.mjs';
import r_emphasis from './rules_inline/emphasis.mjs';
import r_link from './rules_inline/link.mjs';
import r_image from './rules_inline/image.mjs';
import r_autolink from './rules_inline/autolink.mjs';
import r_html_inline from './rules_inline/html_inline.mjs';
import r_entity from './rules_inline/entity.mjs';
import r_balance_pairs from './rules_inline/balance_pairs.mjs';
import r_fragments_join from './rules_inline/fragments_join.mjs';
var Ruler = require('./ruler');
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Parser rules // Parser rules
var _rules = [ var _rules = [
[ 'text', require('./rules_inline/text') ], [ 'text', r_text ],
[ 'linkify', require('./rules_inline/linkify') ], [ 'linkify', r_linkify ],
[ 'newline', require('./rules_inline/newline') ], [ 'newline', r_newline ],
[ 'escape', require('./rules_inline/escape') ], [ 'escape', r_escape ],
[ 'backticks', require('./rules_inline/backticks') ], [ 'backticks', r_backticks ],
[ 'strikethrough', require('./rules_inline/strikethrough').tokenize ], [ 'strikethrough', r_strikethrough.tokenize ],
[ 'emphasis', require('./rules_inline/emphasis').tokenize ], [ 'emphasis', r_emphasis.tokenize ],
[ 'link', require('./rules_inline/link') ], [ 'link', r_link ],
[ 'image', require('./rules_inline/image') ], [ 'image', r_image ],
[ 'autolink', require('./rules_inline/autolink') ], [ 'autolink', r_autolink ],
[ 'html_inline', require('./rules_inline/html_inline') ], [ 'html_inline', r_html_inline ],
[ 'entity', require('./rules_inline/entity') ] [ 'entity', r_entity ]
]; ];
// `rule2` ruleset was created specifically for emphasis/strikethrough // `rule2` ruleset was created specifically for emphasis/strikethrough
@ -33,12 +49,12 @@ var _rules = [
// Don't use this for anything except pairs (plugins working with `balance_pairs`). // Don't use this for anything except pairs (plugins working with `balance_pairs`).
// //
var _rules2 = [ var _rules2 = [
[ 'balance_pairs', require('./rules_inline/balance_pairs') ], [ 'balance_pairs', r_balance_pairs ],
[ 'strikethrough', require('./rules_inline/strikethrough').postProcess ], [ 'strikethrough', r_strikethrough.postProcess ],
[ 'emphasis', require('./rules_inline/emphasis').postProcess ], [ 'emphasis', r_emphasis.postProcess ],
// rules for pairs separate '**' into its own text tokens, which may be left unused, // rules for pairs separate '**' into its own text tokens, which may be left unused,
// rule below merges unused segments back with the rest of the text // rule below merges unused segments back with the rest of the text
[ 'fragments_join', require('./rules_inline/fragments_join') ] [ 'fragments_join', r_fragments_join ]
]; ];
@ -186,7 +202,7 @@ ParserInline.prototype.parse = function (str, md, env, outTokens) {
}; };
ParserInline.prototype.State = require('./rules_inline/state_inline'); ParserInline.prototype.State = StateInline;
module.exports = ParserInline; export default ParserInline;

5
lib/presets/commonmark.js → lib/presets/commonmark.mjs

@ -1,9 +1,6 @@
// Commonmark default options // Commonmark default options
'use strict'; export default {
module.exports = {
options: { options: {
html: true, // Enable HTML tags in source html: true, // Enable HTML tags in source
xhtmlOut: true, // Use '/' to close single tags (<br />) xhtmlOut: true, // Use '/' to close single tags (<br />)

5
lib/presets/default.js → lib/presets/default.mjs

@ -1,9 +1,6 @@
// markdown-it default options // markdown-it default options
'use strict'; export default {
module.exports = {
options: { options: {
html: false, // Enable HTML tags in source html: false, // Enable HTML tags in source
xhtmlOut: false, // Use '/' to close single tags (<br />) xhtmlOut: false, // Use '/' to close single tags (<br />)

5
lib/presets/zero.js → lib/presets/zero.mjs

@ -1,10 +1,7 @@
// "Zero" preset, with nothing enabled. Useful for manual configuring of simple // "Zero" preset, with nothing enabled. Useful for manual configuring of simple
// modes. For example, to parse bold/italic only. // modes. For example, to parse bold/italic only.
'use strict'; export default {
module.exports = {
options: { options: {
html: false, // Enable HTML tags in source html: false, // Enable HTML tags in source
xhtmlOut: false, // Use '/' to close single tags (<br />) xhtmlOut: false, // Use '/' to close single tags (<br />)

9
lib/renderer.js → lib/renderer.mjs

@ -5,13 +5,8 @@
* copy of rules. Those can be rewritten with ease. Also, you can add new * copy of rules. Those can be rewritten with ease. Also, you can add new
* rules if you create plugin and adds new token types. * rules if you create plugin and adds new token types.
**/ **/
'use strict';
var assign = require('./common/utils').assign;
var unescapeAll = require('./common/utils').unescapeAll;
var escapeHtml = require('./common/utils').escapeHtml;
import { assign, unescapeAll, escapeHtml } from './common/utils.mjs';
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -338,4 +333,4 @@ Renderer.prototype.render = function (tokens, options, env) {
return result; return result;
}; };
module.exports = Renderer; export default Renderer;

3
lib/ruler.js → lib/ruler.mjs

@ -15,7 +15,6 @@
* rules control use [[MarkdownIt.disable]], [[MarkdownIt.enable]] and * rules control use [[MarkdownIt.disable]], [[MarkdownIt.enable]] and
* [[MarkdownIt.use]]. * [[MarkdownIt.use]].
**/ **/
'use strict';
/** /**
@ -349,4 +348,4 @@ Ruler.prototype.getRules = function (chainName) {
return this.__cache__[chainName] || []; return this.__cache__[chainName] || [];
}; };
module.exports = Ruler; export default Ruler;

9
lib/rules_block/blockquote.js → lib/rules_block/blockquote.mjs

@ -1,11 +1,8 @@
// Block quotes // Block quotes
'use strict'; import { isSpace } from '../common/utils.mjs';
var isSpace = require('../common/utils').isSpace; export default function blockquote(state, startLine, endLine, silent) {
module.exports = function blockquote(state, startLine, endLine, silent) {
var adjustTab, var adjustTab,
ch, ch,
i, i,
@ -223,4 +220,4 @@ module.exports = function blockquote(state, startLine, endLine, silent) {
state.blkIndent = oldIndent; state.blkIndent = oldIndent;
return true; return true;
}; }

7
lib/rules_block/code.js → lib/rules_block/code.mjs

@ -1,9 +1,6 @@
// Code block (4 spaces padded) // Code block (4 spaces padded)
'use strict'; export default function code(state, startLine, endLine/*, silent*/) {
module.exports = function code(state, startLine, endLine/*, silent*/) {
var nextLine, last, token; var nextLine, last, token;
if (state.sCount[startLine] - state.blkIndent < 4) { return false; } if (state.sCount[startLine] - state.blkIndent < 4) { return false; }
@ -31,4 +28,4 @@ module.exports = function code(state, startLine, endLine/*, silent*/) {
token.map = [ startLine, state.line ]; token.map = [ startLine, state.line ];
return true; return true;
}; }

7
lib/rules_block/fence.js → lib/rules_block/fence.mjs

@ -1,9 +1,6 @@
// fences (``` lang, ~~~ lang) // fences (``` lang, ~~~ lang)
'use strict'; export default function fence(state, startLine, endLine, silent) {
module.exports = function fence(state, startLine, endLine, silent) {
var marker, len, params, nextLine, mem, token, markup, var marker, len, params, nextLine, mem, token, markup,
haveEndMarker = false, haveEndMarker = false,
pos = state.bMarks[startLine] + state.tShift[startLine], pos = state.bMarks[startLine] + state.tShift[startLine],
@ -95,4 +92,4 @@ module.exports = function fence(state, startLine, endLine, silent) {
token.map = [ startLine, state.line ]; token.map = [ startLine, state.line ];
return true; return true;
}; }

9
lib/rules_block/heading.js → lib/rules_block/heading.mjs

@ -1,11 +1,8 @@
// heading (#, ##, ...) // heading (#, ##, ...)
'use strict'; import { isSpace } from '../common/utils.mjs';
var isSpace = require('../common/utils').isSpace; export default function heading(state, startLine, endLine, silent) {
module.exports = function heading(state, startLine, endLine, silent) {
var ch, level, tmp, token, var ch, level, tmp, token,
pos = state.bMarks[startLine] + state.tShift[startLine], pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine]; max = state.eMarks[startLine];
@ -52,4 +49,4 @@ module.exports = function heading(state, startLine, endLine, silent) {
token.markup = '########'.slice(0, level); token.markup = '########'.slice(0, level);
return true; return true;
}; }

9
lib/rules_block/hr.js → lib/rules_block/hr.mjs

@ -1,11 +1,8 @@
// Horizontal rule // Horizontal rule
'use strict'; import { isSpace } from '../common/utils.mjs';
var isSpace = require('../common/utils').isSpace; export default function hr(state, startLine, endLine, silent) {
module.exports = function hr(state, startLine, endLine, silent) {
var marker, cnt, ch, token, var marker, cnt, ch, token,
pos = state.bMarks[startLine] + state.tShift[startLine], pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine]; max = state.eMarks[startLine];
@ -42,4 +39,4 @@ module.exports = function hr(state, startLine, endLine, silent) {
token.markup = Array(cnt + 1).join(String.fromCharCode(marker)); token.markup = Array(cnt + 1).join(String.fromCharCode(marker));
return true; return true;
}; }

11
lib/rules_block/html_block.js → lib/rules_block/html_block.mjs

@ -1,10 +1,7 @@
// HTML block // HTML block
'use strict'; import block_names from '../common/html_blocks.mjs';
import { HTML_OPEN_CLOSE_TAG_RE } from '../common/html_re.mjs';
var block_names = require('../common/html_blocks');
var HTML_OPEN_CLOSE_TAG_RE = require('../common/html_re').HTML_OPEN_CLOSE_TAG_RE;
// An array of opening and corresponding closing sequences for html tags, // An array of opening and corresponding closing sequences for html tags,
// last argument defines whether it can terminate a paragraph or not // last argument defines whether it can terminate a paragraph or not
@ -20,7 +17,7 @@ var HTML_SEQUENCES = [
]; ];
module.exports = function html_block(state, startLine, endLine, silent) { export default function html_block(state, startLine, endLine, silent) {
var i, nextLine, token, lineText, var i, nextLine, token, lineText,
pos = state.bMarks[startLine] + state.tShift[startLine], pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine]; max = state.eMarks[startLine];
@ -71,4 +68,4 @@ module.exports = function html_block(state, startLine, endLine, silent) {
token.content = state.getLines(startLine, nextLine, state.blkIndent, true); token.content = state.getLines(startLine, nextLine, state.blkIndent, true);
return true; return true;
}; }

7
lib/rules_block/lheading.js → lib/rules_block/lheading.mjs

@ -1,9 +1,6 @@
// lheading (---, ===) // lheading (---, ===)
'use strict'; export default function lheading(state, startLine, endLine/*, silent*/) {
module.exports = function lheading(state, startLine, endLine/*, silent*/) {
var content, terminate, i, l, token, pos, max, level, marker, var content, terminate, i, l, token, pos, max, level, marker,
nextLine = startLine + 1, oldParentType, nextLine = startLine + 1, oldParentType,
terminatorRules = state.md.block.ruler.getRules('paragraph'); terminatorRules = state.md.block.ruler.getRules('paragraph');
@ -80,4 +77,4 @@ module.exports = function lheading(state, startLine, endLine/*, silent*/) {
state.parentType = oldParentType; state.parentType = oldParentType;
return true; return true;
}; }

9
lib/rules_block/list.js → lib/rules_block/list.mjs

@ -1,9 +1,6 @@
// Lists // Lists
'use strict'; import { isSpace } from '../common/utils.mjs';
var isSpace = require('../common/utils').isSpace;
// Search `[-+*][\n ]`, returns next pos after marker on success // Search `[-+*][\n ]`, returns next pos after marker on success
// or -1 on fail. // or -1 on fail.
@ -97,7 +94,7 @@ function markTightParagraphs(state, idx) {
} }
module.exports = function list(state, startLine, endLine, silent) { export default function list(state, startLine, endLine, silent) {
var ch, var ch,
contentStart, contentStart,
i, i,
@ -359,4 +356,4 @@ module.exports = function list(state, startLine, endLine, silent) {
} }
return true; return true;
}; }

7
lib/rules_block/paragraph.js → lib/rules_block/paragraph.mjs

@ -1,9 +1,6 @@
// Paragraph // Paragraph
'use strict'; export default function paragraph(state, startLine, endLine) {
module.exports = function paragraph(state, startLine, endLine) {
var content, terminate, i, l, token, oldParentType, var content, terminate, i, l, token, oldParentType,
nextLine = startLine + 1, nextLine = startLine + 1,
terminatorRules = state.md.block.ruler.getRules('paragraph'); terminatorRules = state.md.block.ruler.getRules('paragraph');
@ -48,4 +45,4 @@ module.exports = function paragraph(state, startLine, endLine) {
state.parentType = oldParentType; state.parentType = oldParentType;
return true; return true;
}; }

11
lib/rules_block/reference.js → lib/rules_block/reference.mjs

@ -1,11 +1,6 @@
'use strict'; import { isSpace, normalizeReference } from '../common/utils.mjs';
export default function reference(state, startLine, _endLine, silent) {
var normalizeReference = require('../common/utils').normalizeReference;
var isSpace = require('../common/utils').isSpace;
module.exports = function reference(state, startLine, _endLine, silent) {
var ch, var ch,
destEndPos, destEndPos,
destEndLineNo, destEndLineNo,
@ -195,4 +190,4 @@ module.exports = function reference(state, startLine, _endLine, silent) {
state.line = startLine + lines + 1; state.line = startLine + lines + 1;
return true; return true;
}; }

8
lib/rules_block/state_block.js → lib/rules_block/state_block.mjs

@ -1,9 +1,7 @@
// Parser state class // Parser state class
'use strict'; import Token from '../token.mjs';
import { isSpace } from '../common/utils.mjs';
var Token = require('../token');
var isSpace = require('../common/utils').isSpace;
function StateBlock(src, md, env, tokens) { function StateBlock(src, md, env, tokens) {
@ -225,4 +223,4 @@ StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF
StateBlock.prototype.Token = Token; StateBlock.prototype.Token = Token;
module.exports = StateBlock; export default StateBlock;

9
lib/rules_block/table.js → lib/rules_block/table.mjs

@ -1,9 +1,6 @@
// GFM table, https://github.github.com/gfm/#tables-extension- // GFM table, https://github.github.com/gfm/#tables-extension-
'use strict'; import { isSpace } from '../common/utils.mjs';
var isSpace = require('../common/utils').isSpace;
function getLine(state, line) { function getLine(state, line) {
var pos = state.bMarks[line] + state.tShift[line], var pos = state.bMarks[line] + state.tShift[line],
@ -49,7 +46,7 @@ function escapedSplit(str) {
} }
module.exports = function table(state, startLine, endLine, silent) { export default function table(state, startLine, endLine, silent) {
var ch, lineText, pos, i, l, nextLine, columns, columnCount, token, var ch, lineText, pos, i, l, nextLine, columns, columnCount, token,
aligns, t, tableLines, tbodyLines, oldParentType, terminate, aligns, t, tableLines, tbodyLines, oldParentType, terminate,
terminatorRules, firstCh, secondCh; terminatorRules, firstCh, secondCh;
@ -218,4 +215,4 @@ module.exports = function table(state, startLine, endLine, silent) {
state.parentType = oldParentType; state.parentType = oldParentType;
state.line = nextLine; state.line = nextLine;
return true; return true;
}; }

7
lib/rules_core/block.js → lib/rules_core/block.mjs

@ -1,7 +1,4 @@
'use strict'; export default function block(state) {
module.exports = function block(state) {
var token; var token;
if (state.inlineMode) { if (state.inlineMode) {
@ -13,4 +10,4 @@ module.exports = function block(state) {
} else { } else {
state.md.block.parse(state.src, state.md, state.env, state.tokens); state.md.block.parse(state.src, state.md, state.env, state.tokens);
} }
}; }

6
lib/rules_core/inline.js → lib/rules_core/inline.mjs

@ -1,6 +1,4 @@
'use strict'; export default function inline(state) {
module.exports = function inline(state) {
var tokens = state.tokens, tok, i, l; var tokens = state.tokens, tok, i, l;
// Parse inlines // Parse inlines
@ -10,4 +8,4 @@ module.exports = function inline(state) {
state.md.inline.parse(tok.content, state.md, state.env, tok.children); state.md.inline.parse(tok.content, state.md, state.env, tok.children);
} }
} }
}; }

8
lib/rules_core/linkify.js → lib/rules_core/linkify.mjs

@ -2,10 +2,8 @@
// //
// Currently restricted by `md.validateLink()` to http/https/ftp // Currently restricted by `md.validateLink()` to http/https/ftp
// //
'use strict';
import { arrayReplaceAt } from '../common/utils.mjs';
var arrayReplaceAt = require('../common/utils').arrayReplaceAt;
function isLinkOpen(str) { function isLinkOpen(str) {
@ -16,7 +14,7 @@ function isLinkClose(str) {
} }
module.exports = function linkify(state) { export default function linkify(state) {
var i, j, l, tokens, token, currentToken, nodes, ln, text, pos, lastPos, var i, j, l, tokens, token, currentToken, nodes, ln, text, pos, lastPos,
level, htmlLinkLevel, url, fullUrl, urlText, level, htmlLinkLevel, url, fullUrl, urlText,
blockTokens = state.tokens, blockTokens = state.tokens,
@ -139,4 +137,4 @@ module.exports = function linkify(state) {
} }
} }
} }
}; }

6
lib/rules_core/normalize.js → lib/rules_core/normalize.mjs

@ -1,14 +1,12 @@
// Normalize input string // Normalize input string
'use strict';
// https://spec.commonmark.org/0.29/#line-ending // https://spec.commonmark.org/0.29/#line-ending
var NEWLINES_RE = /\r\n?|\n/g; var NEWLINES_RE = /\r\n?|\n/g;
var NULL_RE = /\0/g; var NULL_RE = /\0/g;
module.exports = function normalize(state) { export default function normalize(state) {
var str; var str;
// Normalize newlines // Normalize newlines
@ -18,4 +16,4 @@ module.exports = function normalize(state) {
str = str.replace(NULL_RE, '\uFFFD'); str = str.replace(NULL_RE, '\uFFFD');
state.src = str; state.src = str;
}; }

5
lib/rules_core/replacements.js → lib/rules_core/replacements.mjs

@ -8,7 +8,6 @@
// ???????? → ???, !!!!! → !!!, `,,` → `,` // ???????? → ???, !!!!! → !!!, `,,` → `,`
// -- → &ndash;, --- → &mdash; // -- → &ndash;, --- → &mdash;
// //
'use strict';
// TODO: // TODO:
// - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾ // - fractionals 1/2, 1/4, 3/4 -> ½, ¼, ¾
@ -84,7 +83,7 @@ function replace_rare(inlineTokens) {
} }
module.exports = function replace(state) { export default function replace(state) {
var blkIdx; var blkIdx;
if (!state.md.options.typographer) { return; } if (!state.md.options.typographer) { return; }
@ -102,4 +101,4 @@ module.exports = function replace(state) {
} }
} }
}; }

10
lib/rules_core/smartquotes.js → lib/rules_core/smartquotes.mjs

@ -1,11 +1,7 @@
// Convert straight quotation marks to typographic ones // Convert straight quotation marks to typographic ones
// //
'use strict';
import { isWhiteSpace, isPunctChar, isMdAsciiPunct } from '../common/utils.mjs';
var isWhiteSpace = require('../common/utils').isWhiteSpace;
var isPunctChar = require('../common/utils').isPunctChar;
var isMdAsciiPunct = require('../common/utils').isMdAsciiPunct;
var QUOTE_TEST_RE = /['"]/; var QUOTE_TEST_RE = /['"]/;
var QUOTE_RE = /['"]/g; var QUOTE_RE = /['"]/g;
@ -183,7 +179,7 @@ function process_inlines(tokens, state) {
} }
module.exports = function smartquotes(state) { export default function smartquotes(state) {
/*eslint max-depth:0*/ /*eslint max-depth:0*/
var blkIdx; var blkIdx;
@ -198,4 +194,4 @@ module.exports = function smartquotes(state) {
process_inlines(state.tokens[blkIdx].children, state); process_inlines(state.tokens[blkIdx].children, state);
} }
}; }

6
lib/rules_core/state_core.js → lib/rules_core/state_core.mjs

@ -1,9 +1,7 @@
// Core state object // Core state object
// //
'use strict';
var Token = require('../token');
import Token from '../token.mjs';
function StateCore(src, md, env) { function StateCore(src, md, env) {
this.src = src; this.src = src;
@ -17,4 +15,4 @@ function StateCore(src, md, env) {
StateCore.prototype.Token = Token; StateCore.prototype.Token = Token;
module.exports = StateCore; export default StateCore;

6
lib/rules_core/text_join.js → lib/rules_core/text_join.mjs

@ -5,10 +5,8 @@
// //
// For example, `\:)` shouldn't be replaced with an emoji. // For example, `\:)` shouldn't be replaced with an emoji.
// //
'use strict';
export default function text_join(state) {
module.exports = function text_join(state) {
var j, l, tokens, curr, max, last, var j, l, tokens, curr, max, last,
blockTokens = state.tokens; blockTokens = state.tokens;
@ -42,4 +40,4 @@ module.exports = function text_join(state) {
tokens.length = last; tokens.length = last;
} }
} }
}; }

7
lib/rules_inline/autolink.js → lib/rules_inline/autolink.mjs

@ -1,14 +1,11 @@
// Process autolinks '<protocol:...>' // Process autolinks '<protocol:...>'
'use strict';
/*eslint max-len:0*/ /*eslint max-len:0*/
var EMAIL_RE = /^([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)$/; var EMAIL_RE = /^([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)$/;
var AUTOLINK_RE = /^([a-zA-Z][a-zA-Z0-9+.\-]{1,31}):([^<>\x00-\x20]*)$/; var AUTOLINK_RE = /^([a-zA-Z][a-zA-Z0-9+.\-]{1,31}):([^<>\x00-\x20]*)$/;
module.exports = function autolink(state, silent) { export default function autolink(state, silent) {
var url, fullUrl, token, ch, start, max, var url, fullUrl, token, ch, start, max,
pos = state.pos; pos = state.pos;
@ -73,4 +70,4 @@ module.exports = function autolink(state, silent) {
} }
return false; return false;
}; }

7
lib/rules_inline/backticks.js → lib/rules_inline/backticks.mjs

@ -1,9 +1,6 @@
// Parse backticks // Parse backticks
'use strict'; export default function backtick(state, silent) {
module.exports = function backtick(state, silent) {
var start, max, marker, token, matchStart, matchEnd, openerLength, closerLength, var start, max, marker, token, matchStart, matchEnd, openerLength, closerLength,
pos = state.pos, pos = state.pos,
ch = state.src.charCodeAt(pos); ch = state.src.charCodeAt(pos);
@ -60,4 +57,4 @@ module.exports = function backtick(state, silent) {
if (!silent) state.pending += marker; if (!silent) state.pending += marker;
state.pos += openerLength; state.pos += openerLength;
return true; return true;
}; }

6
lib/rules_inline/balance_pairs.js → lib/rules_inline/balance_pairs.mjs

@ -1,7 +1,5 @@
// For each opening emphasis-like marker find a matching closing one // For each opening emphasis-like marker find a matching closing one
// //
'use strict';
function processDelimiters(delimiters) { function processDelimiters(delimiters) {
var closerIdx, openerIdx, closer, opener, minOpenerIdx, newMinOpenerIdx, var closerIdx, openerIdx, closer, opener, minOpenerIdx, newMinOpenerIdx,
@ -115,7 +113,7 @@ function processDelimiters(delimiters) {
} }
module.exports = function link_pairs(state) { export default function link_pairs(state) {
var curr, var curr,
tokens_meta = state.tokens_meta, tokens_meta = state.tokens_meta,
max = state.tokens_meta.length; max = state.tokens_meta.length;
@ -127,4 +125,4 @@ module.exports = function link_pairs(state) {
processDelimiters(tokens_meta[curr].delimiters); processDelimiters(tokens_meta[curr].delimiters);
} }
} }
}; }

13
lib/rules_inline/emphasis.js → lib/rules_inline/emphasis.mjs

@ -1,11 +1,9 @@
// Process *this* and _that_ // Process *this* and _that_
// //
'use strict';
// Insert each marker as a separate text token, and add it to delimiter list // Insert each marker as a separate text token, and add it to delimiter list
// //
module.exports.tokenize = function emphasis(state, silent) { function emphasis_tokenize(state, silent) {
var i, scanned, token, var i, scanned, token,
start = state.pos, start = state.pos,
marker = state.src.charCodeAt(start); marker = state.src.charCodeAt(start);
@ -49,7 +47,7 @@ module.exports.tokenize = function emphasis(state, silent) {
state.pos += scanned.length; state.pos += scanned.length;
return true; return true;
}; }
function postProcess(state, delimiters) { function postProcess(state, delimiters) {
@ -115,7 +113,7 @@ function postProcess(state, delimiters) {
// Walk through delimiter list and replace text tokens with tags // Walk through delimiter list and replace text tokens with tags
// //
module.exports.postProcess = function emphasis(state) { function emphasis_post_process(state) {
var curr, var curr,
tokens_meta = state.tokens_meta, tokens_meta = state.tokens_meta,
max = state.tokens_meta.length; max = state.tokens_meta.length;
@ -127,4 +125,9 @@ module.exports.postProcess = function emphasis(state) {
postProcess(state, tokens_meta[curr].delimiters); postProcess(state, tokens_meta[curr].delimiters);
} }
} }
}
export default {
tokenize: emphasis_tokenize,
postProcess: emphasis_post_process
}; };

11
lib/rules_inline/entity.js → lib/rules_inline/entity.mjs

@ -1,17 +1,14 @@
// Process html entity - &#123;, &#xAF;, &quot;, ... // Process html entity - &#123;, &#xAF;, &quot;, ...
'use strict'; import { decodeHTML } from 'entities';
import { isValidEntityCode, fromCodePoint } from '../common/utils.mjs';
var decodeHTML = require('entities').decodeHTML;
var isValidEntityCode = require('../common/utils').isValidEntityCode;
var fromCodePoint = require('../common/utils').fromCodePoint;
var DIGITAL_RE = /^&#((?:x[a-f0-9]{1,6}|[0-9]{1,7}));/i; var DIGITAL_RE = /^&#((?:x[a-f0-9]{1,6}|[0-9]{1,7}));/i;
var NAMED_RE = /^&([a-z][a-z0-9]{1,31});/i; var NAMED_RE = /^&([a-z][a-z0-9]{1,31});/i;
module.exports = function entity(state, silent) { export default function entity(state, silent) {
var ch, code, match, decoded, token, pos = state.pos, max = state.posMax; var ch, code, match, decoded, token, pos = state.pos, max = state.posMax;
if (state.src.charCodeAt(pos) !== 0x26/* & */) return false; if (state.src.charCodeAt(pos) !== 0x26/* & */) return false;
@ -52,4 +49,4 @@ module.exports = function entity(state, silent) {
} }
return false; return false;
}; }

8
lib/rules_inline/escape.js → lib/rules_inline/escape.mjs

@ -1,8 +1,6 @@
// Process escaped chars and hardbreaks // Process escaped chars and hardbreaks
'use strict'; import { isSpace } from '../common/utils.mjs';
var isSpace = require('../common/utils').isSpace;
var ESCAPED = []; var ESCAPED = [];
@ -12,7 +10,7 @@ for (var i = 0; i < 256; i++) { ESCAPED.push(0); }
.split('').forEach(function (ch) { ESCAPED[ch.charCodeAt(0)] = 1; }); .split('').forEach(function (ch) { ESCAPED[ch.charCodeAt(0)] = 1; });
module.exports = function escape(state, silent) { export default function escape(state, silent) {
var ch1, ch2, origStr, escapedStr, token, pos = state.pos, max = state.posMax; var ch1, ch2, origStr, escapedStr, token, pos = state.pos, max = state.posMax;
if (state.src.charCodeAt(pos) !== 0x5C/* \ */) return false; if (state.src.charCodeAt(pos) !== 0x5C/* \ */) return false;
@ -68,4 +66,4 @@ module.exports = function escape(state, silent) {
state.pos = pos + 1; state.pos = pos + 1;
return true; return true;
}; }

6
lib/rules_inline/fragments_join.js → lib/rules_inline/fragments_join.mjs

@ -6,10 +6,8 @@
// leaves them as text (needed to merge with adjacent text) or turns them // leaves them as text (needed to merge with adjacent text) or turns them
// into opening/closing tags (which messes up levels inside). // into opening/closing tags (which messes up levels inside).
// //
'use strict';
export default function fragments_join(state) {
module.exports = function fragments_join(state) {
var curr, last, var curr, last,
level = 0, level = 0,
tokens = state.tokens, tokens = state.tokens,
@ -38,4 +36,4 @@ module.exports = function fragments_join(state) {
if (curr !== last) { if (curr !== last) {
tokens.length = last; tokens.length = last;
} }
}; }

9
lib/rules_inline/html_inline.js → lib/rules_inline/html_inline.mjs

@ -1,9 +1,6 @@
// Process html tags // Process html tags
'use strict'; import { HTML_TAG_RE } from '../common/html_re.mjs';
var HTML_TAG_RE = require('../common/html_re').HTML_TAG_RE;
function isLinkOpen(str) { function isLinkOpen(str) {
@ -21,7 +18,7 @@ function isLetter(ch) {
} }
module.exports = function html_inline(state, silent) { export default function html_inline(state, silent) {
var ch, match, max, token, var ch, match, max, token,
pos = state.pos; pos = state.pos;
@ -55,4 +52,4 @@ module.exports = function html_inline(state, silent) {
} }
state.pos += match[0].length; state.pos += match[0].length;
return true; return true;
}; }

9
lib/rules_inline/image.js → lib/rules_inline/image.mjs

@ -1,12 +1,9 @@
// Process ![image](<src> "title") // Process ![image](<src> "title")
'use strict'; import { normalizeReference, isSpace } from '../common/utils.mjs';
var normalizeReference = require('../common/utils').normalizeReference;
var isSpace = require('../common/utils').isSpace;
export default function image(state, silent) {
module.exports = function image(state, silent) {
var attrs, var attrs,
code, code,
content, content,
@ -149,4 +146,4 @@ module.exports = function image(state, silent) {
state.pos = pos; state.pos = pos;
state.posMax = max; state.posMax = max;
return true; return true;
}; }

10
lib/rules_inline/link.js → lib/rules_inline/link.mjs

@ -1,12 +1,8 @@
// Process [link](<to> "stuff") // Process [link](<to> "stuff")
'use strict'; import { normalizeReference, isSpace } from '../common/utils.mjs';
var normalizeReference = require('../common/utils').normalizeReference; export default function link(state, silent) {
var isSpace = require('../common/utils').isSpace;
module.exports = function link(state, silent) {
var attrs, var attrs,
code, code,
label, label,
@ -147,4 +143,4 @@ module.exports = function link(state, silent) {
state.pos = pos; state.pos = pos;
state.posMax = max; state.posMax = max;
return true; return true;
}; }

7
lib/rules_inline/linkify.js → lib/rules_inline/linkify.mjs

@ -1,13 +1,10 @@
// Process links like https://example.org/ // Process links like https://example.org/
'use strict';
// RFC3986: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) // RFC3986: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
var SCHEME_RE = /(?:^|[^a-z0-9.+-])([a-z][a-z0-9.+-]*)$/i; var SCHEME_RE = /(?:^|[^a-z0-9.+-])([a-z][a-z0-9.+-]*)$/i;
module.exports = function linkify(state, silent) { export default function linkify(state, silent) {
var pos, max, match, proto, link, url, fullUrl, token; var pos, max, match, proto, link, url, fullUrl, token;
if (!state.md.options.linkify) return false; if (!state.md.options.linkify) return false;
@ -59,4 +56,4 @@ module.exports = function linkify(state, silent) {
state.pos += url.length - proto.length; state.pos += url.length - proto.length;
return true; return true;
}; }

9
lib/rules_inline/newline.js → lib/rules_inline/newline.mjs

@ -1,11 +1,8 @@
// Proceess '\n' // Proceess '\n'
'use strict'; import { isSpace } from '../common/utils.mjs';
var isSpace = require('../common/utils').isSpace; export default function newline(state, silent) {
module.exports = function newline(state, silent) {
var pmax, max, ws, pos = state.pos; var pmax, max, ws, pos = state.pos;
if (state.src.charCodeAt(pos) !== 0x0A/* \n */) { return false; } if (state.src.charCodeAt(pos) !== 0x0A/* \n */) { return false; }
@ -43,4 +40,4 @@ module.exports = function newline(state, silent) {
state.pos = pos; state.pos = pos;
return true; return true;
}; }

12
lib/rules_inline/state_inline.js → lib/rules_inline/state_inline.mjs

@ -1,13 +1,7 @@
// Inline parser state // Inline parser state
'use strict'; import Token from '../token.mjs';
import { isWhiteSpace, isPunctChar, isMdAsciiPunct } from '../common/utils.mjs';
var Token = require('../token');
var isWhiteSpace = require('../common/utils').isWhiteSpace;
var isPunctChar = require('../common/utils').isPunctChar;
var isMdAsciiPunct = require('../common/utils').isMdAsciiPunct;
function StateInline(src, md, env, outTokens) { function StateInline(src, md, env, outTokens) {
this.src = src; this.src = src;
@ -155,4 +149,4 @@ StateInline.prototype.scanDelims = function (start, canSplitWord) {
StateInline.prototype.Token = Token; StateInline.prototype.Token = Token;
module.exports = StateInline; export default StateInline;

14
lib/rules_inline/strikethrough.js → lib/rules_inline/strikethrough.mjs

@ -1,11 +1,9 @@
// ~~strike through~~ // ~~strike through~~
// //
'use strict';
// Insert each marker as a separate text token, and add it to delimiter list // Insert each marker as a separate text token, and add it to delimiter list
// //
module.exports.tokenize = function strikethrough(state, silent) { function strikethrough_tokenize(state, silent) {
var i, scanned, token, len, ch, var i, scanned, token, len, ch,
start = state.pos, start = state.pos,
marker = state.src.charCodeAt(start); marker = state.src.charCodeAt(start);
@ -43,7 +41,7 @@ module.exports.tokenize = function strikethrough(state, silent) {
state.pos += scanned.length; state.pos += scanned.length;
return true; return true;
}; }
function postProcess(state, delimiters) { function postProcess(state, delimiters) {
@ -115,7 +113,7 @@ function postProcess(state, delimiters) {
// Walk through delimiter list and replace text tokens with tags // Walk through delimiter list and replace text tokens with tags
// //
module.exports.postProcess = function strikethrough(state) { function strikethrough_postProcess(state) {
var curr, var curr,
tokens_meta = state.tokens_meta, tokens_meta = state.tokens_meta,
max = state.tokens_meta.length; max = state.tokens_meta.length;
@ -127,4 +125,10 @@ module.exports.postProcess = function strikethrough(state) {
postProcess(state, tokens_meta[curr].delimiters); postProcess(state, tokens_meta[curr].delimiters);
} }
} }
}
export default {
tokenize: strikethrough_tokenize,
postProcess: strikethrough_postProcess
}; };

7
lib/rules_inline/text.js → lib/rules_inline/text.mjs

@ -1,9 +1,6 @@
// Skip text characters for text token, place those to pending buffer // Skip text characters for text token, place those to pending buffer
// and increment current pos // and increment current pos
'use strict';
// Rule to skip pure text // Rule to skip pure text
// '{}$%@~+=:' reserved for extentions // '{}$%@~+=:' reserved for extentions
@ -42,7 +39,7 @@ function isTerminatorChar(ch) {
} }
} }
module.exports = function text(state, silent) { export default function text(state, silent) {
var pos = state.pos; var pos = state.pos;
while (pos < state.posMax && !isTerminatorChar(state.src.charCodeAt(pos))) { while (pos < state.posMax && !isTerminatorChar(state.src.charCodeAt(pos))) {
@ -56,7 +53,7 @@ module.exports = function text(state, silent) {
state.pos = pos; state.pos = pos;
return true; return true;
}; }
// Alternative implementation, for memory. // Alternative implementation, for memory.
// //

5
lib/token.js → lib/token.mjs

@ -1,8 +1,5 @@
// Token class // Token class
'use strict';
/** /**
* class Token * class Token
**/ **/
@ -198,4 +195,4 @@ Token.prototype.attrJoin = function attrJoin(name, value) {
}; };
module.exports = Token; export default Token;

9
package.json

@ -11,13 +11,12 @@
], ],
"repository": "markdown-it/markdown-it", "repository": "markdown-it/markdown-it",
"license": "MIT", "license": "MIT",
"main": "index.js",
"bin": { "bin": {
"markdown-it": "bin/markdown-it.js" "markdown-it": "bin/markdown-it.mjs"
}, },
"scripts": { "scripts": {
"lint": "eslint --ext js --ext mjs .", "lint": "eslint --ext js --ext mjs .",
"test": "npm run lint && nyc mocha && node support/specsplit.js", "test": "npm run lint && nyc mocha && node support/specsplit.mjs",
"coverage": "npm run test && nyc report --reporter html", "coverage": "npm run test && nyc report --reporter html",
"report-coveralls": "nyc --reporter=lcov mocha", "report-coveralls": "nyc --reporter=lcov mocha",
"doc": "node support/build_doc.js", "doc": "node support/build_doc.js",
@ -26,12 +25,12 @@
"gh-demo": "npm run demo && gh-pages -d demo -f -b master -r git@github.com:markdown-it/markdown-it.github.io.git", "gh-demo": "npm run demo && gh-pages -d demo -f -b master -r git@github.com:markdown-it/markdown-it.github.io.git",
"browserify": "rollup -c support/rollup.config.js", "browserify": "rollup -c support/rollup.config.js",
"benchmark-deps": "npm install --prefix benchmark/extra/ -g marked@0.3.6 commonmark@0.26.0 markdown-it/markdown-it.git#2.2.1", "benchmark-deps": "npm install --prefix benchmark/extra/ -g marked@0.3.6 commonmark@0.26.0 markdown-it/markdown-it.git#2.2.1",
"specsplit": "support/specsplit.js good -o test/fixtures/commonmark/good.txt && support/specsplit.js bad -o test/fixtures/commonmark/bad.txt && support/specsplit.js", "specsplit": "support/specsplit.mjs good -o test/fixtures/commonmark/good.txt && support/specsplit.mjs bad -o test/fixtures/commonmark/bad.txt && support/specsplit.mjs",
"todo": "grep 'TODO' -n -r ./lib 2>/dev/null", "todo": "grep 'TODO' -n -r ./lib 2>/dev/null",
"prepublishOnly": "npm run gh-demo && npm run gh-doc" "prepublishOnly": "npm run gh-demo && npm run gh-doc"
}, },
"files": [ "files": [
"index.js", "index.mjs",
"bin/", "bin/",
"lib/", "lib/",
"dist/" "dist/"

11
support/babelmark-responder.js → support/babelmark-responder.mjs

@ -1,13 +1,14 @@
#!/usr/bin/env node #!/usr/bin/env node
/* eslint-env es6 */
/* eslint-disable no-console */ /* eslint-disable no-console */
'use strict';
const md = require('../')('commonmark'); import markdownit from '../index.mjs';
const app = require('express')(); import express from 'express';
import { readFileSync } from 'fs';
const version = require('../package.json').version; const md = markdownit('commonmark');
const app = express();
const version = JSON.parse(readFileSync(new URL('../package.json', import.meta.url))).version;
const banner = `<!doctype html> const banner = `<!doctype html>
<html lang="en"> <html lang="en">

23
support/specsplit.js → support/specsplit.mjs

@ -4,12 +4,9 @@
// Fixtures generator from commonmark specs. Split spec to working / not working // Fixtures generator from commonmark specs. Split spec to working / not working
// examples, or show total stat. // examples, or show total stat.
'use strict'; import fs from 'node:fs';
import argparse from 'argparse';
import markdownit from '../index.mjs';
var fs = require('fs');
var util = require('util');
var argparse = require('argparse');
var cli = new argparse.ArgumentParser({ var cli = new argparse.ArgumentParser({
@ -24,7 +21,7 @@ cli.add_argument('type', {
cli.add_argument('-s', '--spec', { cli.add_argument('-s', '--spec', {
help: 'spec file to read', help: 'spec file to read',
default: require('path').join(__dirname, '../test/fixtures/commonmark/spec.txt') default: new URL('../test/fixtures/commonmark/spec.txt', import.meta.url)
}); });
cli.add_argument('-o', '--output', { cli.add_argument('-o', '--output', {
@ -65,7 +62,7 @@ function readFile(filename, encoding, callback) {
readFile(options.spec, 'utf8', function (error, input) { readFile(options.spec, 'utf8', function (error, input) {
var good = [], bad = [], var good = [], bad = [],
markdown = require('..')('commonmark'); markdown = markdownit('commonmark');
if (error) { if (error) {
if (error.code === 'ENOENT') { if (error.code === 'ENOENT') {
@ -112,17 +109,17 @@ readFile(options.spec, 'utf8', function (error, input) {
var out = []; var out = [];
if (!options.type) { if (!options.type) {
out.push(util.format('CM spec stat: passed samples - %s, failed samples - %s', good.length, bad.length)); out.push(`CM spec stat: passed samples - ${good.length}, failed samples - ${bad.length}`);
} else { } else {
var data = options.type === 'good' ? good : bad; var data = options.type === 'good' ? good : bad;
data.forEach(function (sample) { data.forEach(function (sample) {
out.push(util.format( out.push(
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' + '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' +
'src line: %s\n\n.\n%s.\n%s.\n', `src line: ${sample.line}\n\n.\n${sample.md}.\n${sample.html}.\n`
sample.line, sample.md, sample.html)); );
if (sample.err) { if (sample.err) {
out.push(util.format('error:\n\n%s\n', sample.err)); out.push(`error:\n\n${sample.err}\n`);
} }
}); });
} }

4
test/.eslintrc.yml

@ -1,6 +1,2 @@
env: env:
mocha: true mocha: true
es6: true
parserOptions:
ecmaVersion: 2020

9
test/babelmark-responder.mjs

@ -1,6 +1,7 @@
import supertest from 'supertest'; import supertest from 'supertest';
import { execFile } from 'child_process'; import { execFile } from 'child_process';
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
import { setTimeout as sleep } from 'node:timers/promises';
describe('babelmark responder app', function () { describe('babelmark responder app', function () {
@ -9,14 +10,10 @@ describe('babelmark responder app', function () {
var PORT = 5005; var PORT = 5005;
var request = supertest('http://127.0.0.1:' + PORT); var request = supertest('http://127.0.0.1:' + PORT);
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
before(async () => { before(async () => {
app = execFile( app = execFile(
'node', 'node',
[ '../support/babelmark-responder.js' ], [ '../support/babelmark-responder.mjs' ],
{ {
cwd: new URL('.', import.meta.url), cwd: new URL('.', import.meta.url),
env: Object.assign({}, process.env, { PORT: PORT }) env: Object.assign({}, process.env, { PORT: PORT })
@ -29,7 +26,7 @@ describe('babelmark responder app', function () {
await request.get('/').expect(200); await request.get('/').expect(200);
break; break;
} catch (e) {} } catch (e) {}
await timeout(100); await sleep(100);
} }
}); });

2
test/commonmark.mjs

@ -1,7 +1,7 @@
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import { relative } from 'node:path'; import { relative } from 'node:path';
import { load } from 'markdown-it-testgen'; import { load } from 'markdown-it-testgen';
import markdownit from '../index.js'; import markdownit from '../index.mjs';
import { assert } from 'chai'; import { assert } from 'chai';

2
test/markdown-it.mjs

@ -1,6 +1,6 @@
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import generate from 'markdown-it-testgen'; import generate from 'markdown-it-testgen';
import markdownit from '../index.js'; import markdownit from '../index.mjs';
describe('markdown-it', function () { describe('markdown-it', function () {

2
test/misc.mjs

@ -1,5 +1,5 @@
import { assert } from 'chai'; import { assert } from 'chai';
import markdownit from '../index.js'; import markdownit from '../index.mjs';
import forInline from 'markdown-it-for-inline'; import forInline from 'markdown-it-for-inline';

7
test/pathological.mjs

@ -15,18 +15,17 @@ async function test_pattern(str) {
); );
let result; let result;
const ac = new AbortController();
try { try {
result = await Promise.race([ result = await Promise.race([
worker.render(str), worker.render(str),
new Promise((_, reject) => setTimeout(() => reject(new Error('Terminated (timeout exceeded)')), 3000).unref())
new Promise(function (resolve, reject){
setTimeout(() => { reject(new Error('Terminated (timeout exceeded)')); }, 3000);
})
]); ]);
} catch (e) { } catch (e) {
throw e; throw e;
} finally { } finally {
ac.abort();
await worker.end(); await worker.end();
} }

6
test/pathological_worker.js

@ -1,7 +1,5 @@
'use strict'; 'use strict';
const markdownit = require('../'); exports.render = async (str) => {
return (await import('../index.mjs')).default().render(str);
exports.render = (str) => {
return markdownit().render(str);
}; };

2
test/ruler.mjs

@ -1,5 +1,5 @@
import { assert } from 'chai'; import { assert } from 'chai';
import Ruler from '../lib/ruler.js'; import Ruler from '../lib/ruler.mjs';
describe('Ruler', function () { describe('Ruler', function () {

2
test/token.mjs

@ -1,5 +1,5 @@
import { assert } from 'chai'; import { assert } from 'chai';
import Token from '../lib/token.js'; import Token from '../lib/token.mjs';
describe('Token', function () { describe('Token', function () {

2
test/utils.mjs

@ -1,5 +1,5 @@
import { assert } from 'chai'; import { assert } from 'chai';
import utils from '../lib/common/utils.js'; import utils from '../lib/common/utils.mjs';
describe('Utils', function () { describe('Utils', function () {

Loading…
Cancel
Save