Skip to content

Commit

Permalink
Rewrite core to es6 modules
Browse files Browse the repository at this point in the history
  • Loading branch information
puzrin committed Nov 20, 2023
1 parent beed9ae commit e92e776
Show file tree
Hide file tree
Showing 88 changed files with 549 additions and 642 deletions.
8 changes: 7 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: node support/babelmark-responder.js
web: node support/babelmark-responder.mjs
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
138 changes: 0 additions & 138 deletions benchmark/benchmark.js

This file was deleted.

110 changes: 110 additions & 0 deletions benchmark/benchmark.mjs
Original file line number Diff line number Diff line change
@@ -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 changes: 0 additions & 9 deletions benchmark/implementations/commonmark-reference/index.js

This file was deleted.

11 changes: 11 additions & 0 deletions benchmark/implementations/commonmark-reference/index.mjs
Original file line number Diff line number Diff line change
@@ -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));
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 changes: 0 additions & 11 deletions benchmark/implementations/current/index.js

This file was deleted.

11 changes: 11 additions & 0 deletions benchmark/implementations/current/index.mjs
Original file line number Diff line number Diff line change
@@ -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);
}

This file was deleted.

10 changes: 10 additions & 0 deletions benchmark/implementations/markdown-it-2.2.1-commonmark/index.mjs
Original file line number Diff line number Diff line change
@@ -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 changes: 0 additions & 7 deletions benchmark/implementations/marked/index.js

This file was deleted.

8 changes: 8 additions & 0 deletions benchmark/implementations/marked/index.mjs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit e92e776

Please sign in to comment.