Browse Source

Rewrite core to es6 modules

pull/979/head
Vitaly Puzrin 1 year ago
parent
commit
e92e776411
  1. 8
      .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

8
.eslintrc.yml

@ -1,11 +1,17 @@
env:
node: true
es6: true
node: true
parserOptions:
ecmaVersion: 2022
overrides:
-
files: [ '*.mjs' ]
parserOptions:
sourceType: module
rules:
no-restricted-globals: [ 2, require, __dirname ]
ignorePatterns:
- 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):
```bash
make benchmark-deps
benchmark/benchmark.js readme
npm run benchmark-deps
benchmark/benchmark.mjs readme
Selected samples: (1 of 28)
> 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.
// 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.normalizeLinkText = function (str) { return str; };
exports.run = function (data) {
export function run(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):
```bash
$ benchmark/benchmark.js spec
$ benchmark/benchmark.mjs spec
Selected samples: (1 of 27)
> spec

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

@ -1,12 +1,9 @@
#!/usr/bin/env node
/*eslint no-console:0*/
'use strict';
var fs = require('fs');
var argparse = require('argparse');
import fs from 'node:fs';
import argparse from 'argparse';
import markdownit from '../index.mjs';
////////////////////////////////////////////////////////////////////////////////
@ -17,7 +14,7 @@ var cli = new argparse.ArgumentParser({
cli.add_argument('-v', '--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', {
@ -89,7 +86,7 @@ readFile(options.file, 'utf8', function (err, input) {
process.exit(1);
}
md = require('..')({
md = markdownit({
html: !options.no_html,
xhtmlOut: false,
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
// https://spec.commonmark.org/0.30/#html-blocks
'use strict';
module.exports = [
export default [
'address',
'article',
'aside',

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

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

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

@ -1,7 +1,10 @@
// 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); }
@ -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 decodeHTML = require('entities').decodeHTML;
function replaceEntityPattern(match, name) {
var decoded, code;
@ -185,7 +187,6 @@ function isWhiteSpace(code) {
////////////////////////////////////////////////////////////////////////////////
/*eslint-disable max-len*/
var UNICODE_PUNCT_RE = require('uc.micro/categories/P/regex');
// Currently without astral characters support.
function isPunctChar(ch) {
@ -298,23 +299,42 @@ function normalizeReference(str) {
// so plugins won't have to depend on them explicitly, which reduces their
// bundled size (e.g. a browser build).
//
exports.lib = {};
exports.lib.mdurl = require('mdurl');
exports.lib.ucmicro = require('uc.micro');
exports.assign = assign;
exports.isString = isString;
exports.has = has;
exports.unescapeMd = unescapeMd;
exports.unescapeAll = unescapeAll;
exports.isValidEntityCode = isValidEntityCode;
exports.fromCodePoint = fromCodePoint;
// exports.replaceEntities = replaceEntities;
exports.escapeHtml = escapeHtml;
exports.arrayReplaceAt = arrayReplaceAt;
exports.isSpace = isSpace;
exports.isWhiteSpace = isWhiteSpace;
exports.isMdAsciiPunct = isMdAsciiPunct;
exports.isPunctChar = isPunctChar;
exports.escapeRE = escapeRE;
exports.normalizeReference = normalizeReference;
const lib = { mdurl, ucmicro };
export {
lib,
assign,
isString,
has,
unescapeMd,
unescapeAll,
isValidEntityCode,
fromCodePoint,
escapeHtml,
arrayReplaceAt,
isSpace,
isWhiteSpace,
isMdAsciiPunct,
isPunctChar,
escapeRE,
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
//
'use strict';
import { unescapeAll } from '../common/utils.mjs';
var unescapeAll = require('../common/utils').unescapeAll;
module.exports = function parseLinkDestination(str, start, max) {
export default function parseLinkDestination(str, start, max) {
var code, level,
pos = start,
result = {
@ -77,4 +74,4 @@ module.exports = function parseLinkDestination(str, start, max) {
result.pos = pos;
result.ok = true;
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;
// 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,
labelEnd = -1,
max = state.posMax,
@ -45,4 +44,4 @@ module.exports = function parseLinkLabel(state, start, disableNested) {
state.pos = oldPos;
return labelEnd;
};
}

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

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

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

@ -1,23 +1,23 @@
// Main parser class
'use strict';
var utils = require('./common/utils');
var helpers = require('./helpers');
var Renderer = require('./renderer');
var ParserCore = require('./parser_core');
var ParserBlock = require('./parser_block');
var ParserInline = require('./parser_inline');
var LinkifyIt = require('linkify-it');
var mdurl = require('mdurl');
var punycode = require('punycode/');
import utils from './common/utils.mjs';
import helpers from './helpers/index.mjs';
import Renderer from './renderer.mjs';
import ParserCore from './parser_core.mjs';
import ParserBlock from './parser_block.mjs';
import ParserInline from './parser_inline.mjs';
import LinkifyIt from 'linkify-it';
import mdurl from 'mdurl';
import punycode from 'punycode';
import cfg_default from './presets/default.mjs';
import cfg_zero from './presets/zero.mjs';
import cfg_commonmark from './presets/commonmark.mjs';
var config = {
default: require('./presets/default'),
zero: require('./presets/zero'),
commonmark: require('./presets/commonmark')
default: cfg_default,
zero: cfg_zero,
commonmark: cfg_commonmark
};
////////////////////////////////////////////////////////////////////////////////
@ -329,7 +329,7 @@ function MarkdownIt(presetName, options) {
* MarkdownIt#utils -> utils
*
* 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;
@ -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.
**/
'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 = [
// First 2 params - rule name & source. Secondary array - list of rules,
// which can be terminated by this one.
[ 'table', require('./rules_block/table'), [ 'paragraph', 'reference' ] ],
[ 'code', require('./rules_block/code') ],
[ 'fence', require('./rules_block/fence'), [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
[ 'blockquote', require('./rules_block/blockquote'), [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
[ 'hr', require('./rules_block/hr'), [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
[ 'list', require('./rules_block/list'), [ 'paragraph', 'reference', 'blockquote' ] ],
[ 'reference', require('./rules_block/reference') ],
[ 'html_block', require('./rules_block/html_block'), [ 'paragraph', 'reference', 'blockquote' ] ],
[ 'heading', require('./rules_block/heading'), [ 'paragraph', 'reference', 'blockquote' ] ],
[ 'lheading', require('./rules_block/lheading') ],
[ 'paragraph', require('./rules_block/paragraph') ]
[ 'table', r_table, [ 'paragraph', 'reference' ] ],
[ 'code', r_code ],
[ 'fence', r_fence, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
[ 'blockquote', r_blockquote, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
[ 'hr', r_hr, [ 'paragraph', 'reference', 'blockquote', 'list' ] ],
[ 'list', r_list, [ 'paragraph', 'reference', 'blockquote' ] ],
[ 'reference', r_reference ],
[ 'html_block', r_html_block, [ 'paragraph', 'reference', 'blockquote' ] ],
[ 'heading', r_heading, [ 'paragraph', 'reference', 'blockquote' ] ],
[ 'lheading', r_lheading ],
[ '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.
**/
'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
var _rules = [
[ 'text', require('./rules_inline/text') ],
[ 'linkify', require('./rules_inline/linkify') ],
[ 'newline', require('./rules_inline/newline') ],
[ 'escape', require('./rules_inline/escape') ],
[ 'backticks', require('./rules_inline/backticks') ],
[ 'strikethrough', require('./rules_inline/strikethrough').tokenize ],
[ 'emphasis', require('./rules_inline/emphasis').tokenize ],
[ 'link', require('./rules_inline/link') ],
[ 'image', require('./rules_inline/image') ],
[ 'autolink', require('./rules_inline/autolink') ],
[ 'html_inline', require('./rules_inline/html_inline') ],
[ 'entity', require('./rules_inline/entity') ]
[ 'text', r_text ],
[ 'linkify', r_linkify ],
[ 'newline', r_newline ],
[ 'escape', r_escape ],
[ 'backticks', r_backticks ],
[ 'strikethrough', r_strikethrough.tokenize ],
[ 'emphasis', r_emphasis.tokenize ],
[ 'link', r_link ],
[ 'image', r_image ],
[ 'autolink', r_autolink ],
[ 'html_inline', r_html_inline ],
[ 'entity', r_entity ]
];
// `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`).
//
var _rules2 = [
[ 'balance_pairs', require('./rules_inline/balance_pairs') ],
[ 'strikethrough', require('./rules_inline/strikethrough').postProcess ],
[ 'emphasis', require('./rules_inline/emphasis').postProcess ],
[ 'balance_pairs', r_balance_pairs ],
[ 'strikethrough', r_strikethrough.postProcess ],
[ 'emphasis', r_emphasis.postProcess ],
// 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
[ '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
'use strict';
module.exports = {
export default {
options: {
html: true, // Enable HTML tags in source
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
'use strict';
module.exports = {
export default {
options: {
html: false, // Enable HTML tags in source
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
// modes. For example, to parse bold/italic only.
'use strict';
module.exports = {
export default {
options: {
html: false, // Enable HTML tags in source
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
* 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;
};
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
* [[MarkdownIt.use]].
**/
'use strict';
/**
@ -349,4 +348,4 @@ Ruler.prototype.getRules = function (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
'use strict';
import { isSpace } from '../common/utils.mjs';
var isSpace = require('../common/utils').isSpace;
module.exports = function blockquote(state, startLine, endLine, silent) {
export default function blockquote(state, startLine, endLine, silent) {
var adjustTab,
ch,
i,
@ -223,4 +220,4 @@ module.exports = function blockquote(state, startLine, endLine, silent) {
state.blkIndent = oldIndent;
return true;
};
}

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

@ -1,9 +1,6 @@
// Code block (4 spaces padded)
'use strict';
module.exports = function code(state, startLine, endLine/*, silent*/) {
export default function code(state, startLine, endLine/*, silent*/) {
var nextLine, last, token;
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 ];
return true;
};
}

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

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

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

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

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

@ -1,11 +1,8 @@
// Horizontal rule
'use strict';
import { isSpace } from '../common/utils.mjs';
var isSpace = require('../common/utils').isSpace;
module.exports = function hr(state, startLine, endLine, silent) {
export default function hr(state, startLine, endLine, silent) {
var marker, cnt, ch, token,
pos = state.bMarks[startLine] + state.tShift[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));
return true;
};
}

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

@ -1,10 +1,7 @@
// HTML block
'use strict';
var block_names = require('../common/html_blocks');
var HTML_OPEN_CLOSE_TAG_RE = require('../common/html_re').HTML_OPEN_CLOSE_TAG_RE;
import block_names from '../common/html_blocks.mjs';
import { HTML_OPEN_CLOSE_TAG_RE } from '../common/html_re.mjs';
// An array of opening and corresponding closing sequences for html tags,
// 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,
pos = state.bMarks[startLine] + state.tShift[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);
return true;
};
}

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

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

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

@ -1,9 +1,6 @@
// Lists
'use strict';
var isSpace = require('../common/utils').isSpace;
import { isSpace } from '../common/utils.mjs';
// Search `[-+*][\n ]`, returns next pos after marker on success
// 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,
contentStart,
i,
@ -359,4 +356,4 @@ module.exports = function list(state, startLine, endLine, silent) {
}
return true;
};
}

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

@ -1,9 +1,6 @@
// Paragraph
'use strict';
module.exports = function paragraph(state, startLine, endLine) {
export default function paragraph(state, startLine, endLine) {
var content, terminate, i, l, token, oldParentType,
nextLine = startLine + 1,
terminatorRules = state.md.block.ruler.getRules('paragraph');
@ -48,4 +45,4 @@ module.exports = function paragraph(state, startLine, endLine) {
state.parentType = oldParentType;
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';
var normalizeReference = require('../common/utils').normalizeReference;
var isSpace = require('../common/utils').isSpace;
module.exports = function reference(state, startLine, _endLine, silent) {
export default function reference(state, startLine, _endLine, silent) {
var ch,
destEndPos,
destEndLineNo,
@ -195,4 +190,4 @@ module.exports = function reference(state, startLine, _endLine, silent) {
state.line = startLine + lines + 1;
return true;
};
}

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

@ -1,9 +1,7 @@
// Parser state class
'use strict';
var Token = require('../token');
var isSpace = require('../common/utils').isSpace;
import Token from '../token.mjs';
import { isSpace } from '../common/utils.mjs';
function StateBlock(src, md, env, tokens) {
@ -225,4 +223,4 @@ StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF
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-
'use strict';
var isSpace = require('../common/utils').isSpace;
import { isSpace } from '../common/utils.mjs';
function getLine(state, 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,
aligns, t, tableLines, tbodyLines, oldParentType, terminate,
terminatorRules, firstCh, secondCh;
@ -218,4 +215,4 @@ module.exports = function table(state, startLine, endLine, silent) {
state.parentType = oldParentType;
state.line = nextLine;
return true;
};
}

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

@ -1,7 +1,4 @@
'use strict';
module.exports = function block(state) {
export default function block(state) {
var token;
if (state.inlineMode) {
@ -13,4 +10,4 @@ module.exports = function block(state) {
} else {
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';
module.exports = function inline(state) {
export default function inline(state) {
var tokens = state.tokens, tok, i, l;
// Parse inlines
@ -10,4 +8,4 @@ module.exports = function inline(state) {
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
//
'use strict';
var arrayReplaceAt = require('../common/utils').arrayReplaceAt;
import { arrayReplaceAt } from '../common/utils.mjs';
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,
level, htmlLinkLevel, url, fullUrl, urlText,
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
'use strict';
// https://spec.commonmark.org/0.29/#line-ending
var NEWLINES_RE = /\r\n?|\n/g;
var NULL_RE = /\0/g;
module.exports = function normalize(state) {
export default function normalize(state) {
var str;
// Normalize newlines
@ -18,4 +16,4 @@ module.exports = function normalize(state) {
str = str.replace(NULL_RE, '\uFFFD');
state.src = str;
};
}

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

@ -8,7 +8,6 @@
// ???????? → ???, !!!!! → !!!, `,,` → `,`
// -- → &ndash;, --- → &mdash;
//
'use strict';
// TODO:
// - 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;
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
//
'use strict';
var isWhiteSpace = require('../common/utils').isWhiteSpace;
var isPunctChar = require('../common/utils').isPunctChar;
var isMdAsciiPunct = require('../common/utils').isMdAsciiPunct;
import { isWhiteSpace, isPunctChar, isMdAsciiPunct } from '../common/utils.mjs';
var QUOTE_TEST_RE = /['"]/;
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*/
var blkIdx;
@ -198,4 +194,4 @@ module.exports = function smartquotes(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
//
'use strict';
var Token = require('../token');
import Token from '../token.mjs';
function StateCore(src, md, env) {
this.src = src;
@ -17,4 +15,4 @@ function StateCore(src, md, env) {
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.
//
'use strict';
module.exports = function text_join(state) {
export default function text_join(state) {
var j, l, tokens, curr, max, last,
blockTokens = state.tokens;
@ -42,4 +40,4 @@ module.exports = function text_join(state) {
tokens.length = last;
}
}
};
}

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

@ -1,14 +1,11 @@
// Process autolinks '<protocol:...>'
'use strict';
/*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 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,
pos = state.pos;
@ -73,4 +70,4 @@ module.exports = function autolink(state, silent) {
}
return false;
};
}

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

@ -1,9 +1,6 @@
// Parse backticks
'use strict';
module.exports = function backtick(state, silent) {
export default function backtick(state, silent) {
var start, max, marker, token, matchStart, matchEnd, openerLength, closerLength,
pos = state.pos,
ch = state.src.charCodeAt(pos);
@ -60,4 +57,4 @@ module.exports = function backtick(state, silent) {
if (!silent) state.pending += marker;
state.pos += openerLength;
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
//
'use strict';
function processDelimiters(delimiters) {
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,
tokens_meta = state.tokens_meta,
max = state.tokens_meta.length;
@ -127,4 +125,4 @@ module.exports = function link_pairs(state) {
processDelimiters(tokens_meta[curr].delimiters);
}
}
};
}

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

@ -1,11 +1,9 @@
// Process *this* and _that_
//
'use strict';
// 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,
start = state.pos,
marker = state.src.charCodeAt(start);
@ -49,7 +47,7 @@ module.exports.tokenize = function emphasis(state, silent) {
state.pos += scanned.length;
return true;
};
}
function postProcess(state, delimiters) {
@ -115,7 +113,7 @@ function postProcess(state, delimiters) {
// Walk through delimiter list and replace text tokens with tags
//
module.exports.postProcess = function emphasis(state) {
function emphasis_post_process(state) {
var curr,
tokens_meta = state.tokens_meta,
max = state.tokens_meta.length;
@ -127,4 +125,9 @@ module.exports.postProcess = function emphasis(state) {
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;, ...
'use strict';
var decodeHTML = require('entities').decodeHTML;
var isValidEntityCode = require('../common/utils').isValidEntityCode;
var fromCodePoint = require('../common/utils').fromCodePoint;
import { decodeHTML } from 'entities';
import { isValidEntityCode, fromCodePoint } from '../common/utils.mjs';
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;
module.exports = function entity(state, silent) {
export default function entity(state, silent) {
var ch, code, match, decoded, token, pos = state.pos, max = state.posMax;
if (state.src.charCodeAt(pos) !== 0x26/* & */) return false;
@ -52,4 +49,4 @@ module.exports = function entity(state, silent) {
}
return false;
};
}

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

@ -1,8 +1,6 @@
// Process escaped chars and hardbreaks
'use strict';
var isSpace = require('../common/utils').isSpace;
import { isSpace } from '../common/utils.mjs';
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; });
module.exports = function escape(state, silent) {
export default function escape(state, silent) {
var ch1, ch2, origStr, escapedStr, token, pos = state.pos, max = state.posMax;
if (state.src.charCodeAt(pos) !== 0x5C/* \ */) return false;
@ -68,4 +66,4 @@ module.exports = function escape(state, silent) {
state.pos = pos + 1;
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
// into opening/closing tags (which messes up levels inside).
//
'use strict';
module.exports = function fragments_join(state) {
export default function fragments_join(state) {
var curr, last,
level = 0,
tokens = state.tokens,
@ -38,4 +36,4 @@ module.exports = function fragments_join(state) {
if (curr !== last) {
tokens.length = last;
}
};
}

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

@ -1,9 +1,6 @@
// Process html tags
'use strict';
var HTML_TAG_RE = require('../common/html_re').HTML_TAG_RE;
import { HTML_TAG_RE } from '../common/html_re.mjs';
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,
pos = state.pos;
@ -55,4 +52,4 @@ module.exports = function html_inline(state, silent) {
}
state.pos += match[0].length;
return true;
};
}

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

@ -1,12 +1,9 @@
// 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;
module.exports = function image(state, silent) {
export default function image(state, silent) {
var attrs,
code,
content,
@ -149,4 +146,4 @@ module.exports = function image(state, silent) {
state.pos = pos;
state.posMax = max;
return true;
};
}

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

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

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

@ -1,13 +1,10 @@
// Process links like https://example.org/
'use strict';
// RFC3986: scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
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;
if (!state.md.options.linkify) return false;
@ -59,4 +56,4 @@ module.exports = function linkify(state, silent) {
state.pos += url.length - proto.length;
return true;
};
}

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

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

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

@ -1,13 +1,7 @@
// Inline parser state
'use strict';
var Token = require('../token');
var isWhiteSpace = require('../common/utils').isWhiteSpace;
var isPunctChar = require('../common/utils').isPunctChar;
var isMdAsciiPunct = require('../common/utils').isMdAsciiPunct;
import Token from '../token.mjs';
import { isWhiteSpace, isPunctChar, isMdAsciiPunct } from '../common/utils.mjs';
function StateInline(src, md, env, outTokens) {
this.src = src;
@ -155,4 +149,4 @@ StateInline.prototype.scanDelims = function (start, canSplitWord) {
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~~
//
'use strict';
// 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,
start = state.pos,
marker = state.src.charCodeAt(start);
@ -43,7 +41,7 @@ module.exports.tokenize = function strikethrough(state, silent) {
state.pos += scanned.length;
return true;
};
}
function postProcess(state, delimiters) {
@ -115,7 +113,7 @@ function postProcess(state, delimiters) {
// Walk through delimiter list and replace text tokens with tags
//
module.exports.postProcess = function strikethrough(state) {
function strikethrough_postProcess(state) {
var curr,
tokens_meta = state.tokens_meta,
max = state.tokens_meta.length;
@ -127,4 +125,10 @@ module.exports.postProcess = function strikethrough(state) {
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
// and increment current pos
'use strict';
// Rule to skip pure text
// '{}$%@~+=:' 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;
while (pos < state.posMax && !isTerminatorChar(state.src.charCodeAt(pos))) {
@ -56,7 +53,7 @@ module.exports = function text(state, silent) {
state.pos = pos;
return true;
};
}
// Alternative implementation, for memory.
//

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

@ -1,8 +1,5 @@
// Token class
'use strict';
/**
* 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",
"license": "MIT",
"main": "index.js",
"bin": {
"markdown-it": "bin/markdown-it.js"
"markdown-it": "bin/markdown-it.mjs"
},
"scripts": {
"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",
"report-coveralls": "nyc --reporter=lcov mocha",
"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",
"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",
"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",
"prepublishOnly": "npm run gh-demo && npm run gh-doc"
},
"files": [
"index.js",
"index.mjs",
"bin/",
"lib/",
"dist/"

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

@ -1,13 +1,14 @@
#!/usr/bin/env node
/* eslint-env es6 */
/* eslint-disable no-console */
'use strict';
const md = require('../')('commonmark');
const app = require('express')();
import markdownit from '../index.mjs';
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>
<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
// examples, or show total stat.
'use strict';
var fs = require('fs');
var util = require('util');
var argparse = require('argparse');
import fs from 'node:fs';
import argparse from 'argparse';
import markdownit from '../index.mjs';
var cli = new argparse.ArgumentParser({
@ -24,7 +21,7 @@ cli.add_argument('type', {
cli.add_argument('-s', '--spec', {
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', {
@ -65,7 +62,7 @@ function readFile(filename, encoding, callback) {
readFile(options.spec, 'utf8', function (error, input) {
var good = [], bad = [],
markdown = require('..')('commonmark');
markdown = markdownit('commonmark');
if (error) {
if (error.code === 'ENOENT') {
@ -112,17 +109,17 @@ readFile(options.spec, 'utf8', function (error, input) {
var out = [];
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 {
var data = options.type === 'good' ? good : bad;
data.forEach(function (sample) {
out.push(util.format(
out.push(
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' +
'src line: %s\n\n.\n%s.\n%s.\n',
sample.line, sample.md, sample.html));
`src line: ${sample.line}\n\n.\n${sample.md}.\n${sample.html}.\n`
);
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:
mocha: true
es6: true
parserOptions:
ecmaVersion: 2020

9
test/babelmark-responder.mjs

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

2
test/commonmark.mjs

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

2
test/markdown-it.mjs

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

2
test/misc.mjs

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

7
test/pathological.mjs

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

6
test/pathological_worker.js

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

2
test/ruler.mjs

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

2
test/token.mjs

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

2
test/utils.mjs

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

Loading…
Cancel
Save