Skip to content

Commit 52c25d7

Browse files
iddanjgm
authored andcommitted
Use ES modules.
* Use ES modules. We define both legacy node and new node friendly files. * Use rollup instead of browserify. * Reformat code with prettier.
1 parent 162407a commit 52c25d7

25 files changed

+11672
-5397
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
dingus/

.eslintrc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"env": {
3+
"browser": false,
4+
"node": true
5+
},
6+
"parserOptions": {
7+
"sourceType": "module"
8+
},
9+
"rules": {
10+
"no-unused-vars": [
11+
"error",
12+
{ "vars": "all", "args": "after-used", "ignoreRestSiblings": false }
13+
],
14+
"no-constant-condition": 0,
15+
"no-underscore-dangle": 0,
16+
"camelcase": 0,
17+
"quotes": 0,
18+
"no-process-exit": 0,
19+
"no-empty": 0,
20+
"new-cap": [
21+
2,
22+
{
23+
"newIsCap": true,
24+
"capIsNew": true,
25+
"newIsCapExceptions": [],
26+
"capIsNewExceptions": []
27+
}
28+
]
29+
}
30+
}

Makefile

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
SPEC=test/spec.txt
22
SPECVERSION=$(shell perl -ne 'print $$1 if /^version: *([0-9.]+)/' $(SPEC))
33
BENCHINP?=bench/samples/README.md
4-
JSMODULES=$(wildcard lib/*.js)
54
VERSION?=$(SPECVERSION)
6-
ESLINT=node_modules/.bin/eslint
5+
JSMODULES=$(wildcard lib/*.js)
76
UGLIFYJS=node_modules/.bin/uglifyjs
8-
BROWSERIFY=node_modules/.bin/browserify
97

108
.PHONY: dingus dist test bench bench-detailed npm lint clean update-spec
119

1210
lint:
13-
$(ESLINT) -c eslint.json ${JSMODULES} bin/commonmark test/test.js dingus/dingus.js
11+
npm run lint
1412

1513
dist: dist/commonmark.js dist/commonmark.min.js
1614

1715
dist/commonmark.js: lib/index.js ${JSMODULES}
1816
echo '/* commonmark $(VERSION) https://github.com/CommonMark/commonmark.js @license BSD3 */' > $@
19-
$(BROWSERIFY) --standalone commonmark $< >> $@
17+
npm run build >> $@
2018

2119
dist/commonmark.min.js: dist/commonmark.js
2220
$(UGLIFYJS) --version # version should be at least 2.5.0
@@ -26,7 +24,7 @@ update-spec:
2624
curl 'https://raw.githubusercontent.com/jgm/CommonMark/master/spec.txt' > $(SPEC)
2725

2826
test: $(SPEC)
29-
node test/test.js
27+
npm test
3028

3129
bench:
3230
node bench/bench.js ${BENCHINP}

bench/bench.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
11
"use strict";
22

3-
var Benchmark = require('benchmark').Benchmark;
4-
var suite = new Benchmark.Suite();
5-
var fs = require('fs');
6-
var commonmark = require('../lib/index.js');
7-
var Showdown = require('showdown');
8-
var marked = require('marked');
9-
var markdownit = require('markdown-it')('commonmark');
3+
import benchmark from "benchmark";
4+
import fs from "fs";
5+
import * as commonmark from "../lib/index.js";
6+
import Showdown from "showdown";
7+
import marked from "marked";
8+
import _markdownit from "markdown-it";
9+
10+
var suite = new benchmark.Suite();
11+
var markdownit = _markdownit("commonmark");
1012

1113
// disable expensive IDNa links encoding:
1214
var markdownit_encode = markdownit.utils.lib.mdurl.encode;
13-
markdownit.normalizeLink = function(url) { return markdownit_encode(url); };
14-
markdownit.normalizeLinkText = function(str) { return str; };
15+
markdownit.normalizeLink = function(url) {
16+
return markdownit_encode(url);
17+
};
18+
markdownit.normalizeLinkText = function(str) {
19+
return str;
20+
};
1521

1622
var showdown = new Showdown.Converter();
1723
var parser = new commonmark.Parser();
18-
var parserSmart = new commonmark.Parser({smart: true});
1924
var renderer = new commonmark.HtmlRenderer();
2025

2126
var benchfile = process.argv[2];
2227

23-
var contents = fs.readFileSync(benchfile, 'utf8');
28+
var contents = fs.readFileSync(benchfile, "utf8");
2429

25-
suite.add('commonmark.js', function() {
26-
renderer.render(parser.parse(contents));
27-
})
30+
suite
31+
.add("commonmark.js", function() {
32+
renderer.render(parser.parse(contents));
33+
})
2834

29-
.add('showdown.js', function() {
30-
showdown.makeHtml(contents);
31-
})
35+
.add("showdown.js", function() {
36+
showdown.makeHtml(contents);
37+
})
3238

33-
.add('marked.js', function() {
34-
marked(contents);
35-
})
39+
.add("marked.js", function() {
40+
marked(contents);
41+
})
3642

37-
.add('markdown-it', function() {
38-
markdownit.render(contents);
39-
})
43+
.add("markdown-it", function() {
44+
markdownit.render(contents);
45+
})
4046

41-
.on('cycle', function(event) {
42-
console.log(String(event.target));
43-
})
44-
.run();
47+
.on("cycle", function(event) {
48+
console.log(String(event.target));
49+
})
50+
.run();

bower.json

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
{
2-
"name": "commonmark",
3-
"main": "dist/commonmark.js",
4-
"homepage": "https://github.com/commonmark/commonmark.js",
5-
"description": "CommonMark parsing and rendering library",
6-
"license": "BSD-2-Clause",
7-
"ignore": [
8-
"**/.*",
9-
"lib",
10-
"bin",
11-
"test",
12-
"tools",
13-
"bench",
14-
"dingus",
15-
"Makefile",
16-
"*.md",
17-
"release_checklist.md",
18-
"eslint.json",
19-
"package.json"
20-
],
21-
"keywords": [
22-
"markdown",
23-
"commonmark"
24-
]
2+
"name": "commonmark",
3+
"main": "dist/commonmark.js",
4+
"homepage": "https://github.com/commonmark/commonmark.js",
5+
"description": "CommonMark parsing and rendering library",
6+
"license": "BSD-2-Clause",
7+
"ignore": [
8+
"**/.*",
9+
"lib",
10+
"bin",
11+
"test",
12+
"tools",
13+
"bench",
14+
"dingus",
15+
"Makefile",
16+
"*.md",
17+
"release_checklist.md",
18+
".eslintrc",
19+
"package.json"
20+
],
21+
"keywords": ["markdown", "commonmark"]
2522
}

dingus/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ ALL=commonmark.js
22
COMPONENTS=lodash.min.js jquery.min.js jquery.min.map bootstrap.min.js bootstrap.min.css
33
LIBFILES=../lib/blocks.js ../lib/common.js ../lib/from-code-point.js ../lib/inlines.js ../lib/index.js ../lib/node.js ../lib/render/xml.js ../lib/render/html.js ../lib/render/renderer.js
44
BOWER=../node_modules/.bin/bower
5-
BROWSERIFY=../node_modules/.bin/browserify
65
HTTPSERVER=../node_modules/.bin/http-server
76

87
all: $(ALL) components
@@ -34,7 +33,8 @@ clean:
3433
rm -r bower_components
3534

3635
commonmark.js: ../lib/index.js $(LIBFILES)
37-
$(BROWSERIFY) --standalone commonmark $< -o $@
36+
npm run build; \
37+
cp ../dist/commonmark.js commonmark.js
3838

3939
dingus: all
4040
echo "Starting dingus server at http://localhost:9000/"; \

dingus/dingus.js

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function getQueryVariable(variable) {
1414
var vars = query.split("&");
1515
for (var i = 0; i < vars.length; i++) {
1616
var pair = vars[i].split("=");
17-
if (pair[0] === variable){
17+
if (pair[0] === variable) {
1818
return decodeURIComponent(pair[1]);
1919
}
2020
}
@@ -29,7 +29,9 @@ var render = function(parsed) {
2929
var result = writer.render(parsed);
3030
var endTime = new Date().getTime();
3131
var renderTime = endTime - startTime;
32-
var preview = $("#preview iframe").contents().find('body');
32+
var preview = $("#preview iframe")
33+
.contents()
34+
.find("body");
3335
preview.get(0).innerHTML = result;
3436
$("#html").text(htmlwriter.render(parsed));
3537
$("#ast").text(xmlwriter.render(parsed));
@@ -38,17 +40,22 @@ var render = function(parsed) {
3840

3941
var syncScroll = function() {
4042
var textarea = $("#text");
41-
var preview = $("#preview iframe").contents().find('body');
42-
var lineHeight = parseFloat(textarea.css('line-height'));
43+
var preview = $("#preview iframe")
44+
.contents()
45+
.find("body");
46+
var lineHeight = parseFloat(textarea.css("line-height"));
4347
// NOTE this assumes we don't have wrapped lines,
4448
// so we have set white-space:nowrap on the textarea:
4549
var lineNumber = Math.floor(textarea.scrollTop() / lineHeight) + 1;
4650
var elt = preview.find("*[data-sourcepos^='" + lineNumber + ":']").last();
4751
if (elt.length > 0) {
4852
if (elt.offset()) {
49-
preview.animate({
50-
scrollTop: elt.offset().top - 100
51-
}, 50);
53+
preview.animate(
54+
{
55+
scrollTop: elt.offset().top - 100
56+
},
57+
50
58+
);
5259
}
5360
}
5461
};
@@ -59,11 +66,13 @@ var markSelection = function() {
5966
var textval = $("#text").val();
6067
var lineNumber = 1;
6168
for (var i = 0; i < cursorPos; i++) {
62-
if (textval.charAt(i) === '\n') {
69+
if (textval.charAt(i) === "\n") {
6370
lineNumber++;
6471
}
6572
}
66-
var preview = $("#preview iframe").contents().find('body');
73+
var preview = $("#preview iframe")
74+
.contents()
75+
.find("body");
6776
var elt = preview.find("[data-sourcepos^='" + lineNumber + ":']").last();
6877
if (elt.length > 0) {
6978
preview.find(".selected").removeClass("selected");
@@ -79,46 +88,52 @@ var parseAndRender = function() {
7988
var endTime = new Date().getTime();
8089
var parseTime = endTime - startTime;
8190
$("#parsetime").text(parseTime);
82-
$(".timing").css('visibility', 'visible');
91+
$(".timing").css("visibility", "visible");
8392
render(parsed);
8493
markSelection();
8594
};
8695

8796
$(document).ready(function() {
88-
$('iframe').on('load', function() {
89-
var textarea = $("#text");
90-
var initial_text = getQueryVariable("text");
91-
var smartSelected = getQueryVariable("smart") === "1";
92-
$("#smart").prop('checked', smartSelected);
93-
reader.options.smart = smartSelected;
94-
if (initial_text) {
95-
textarea.val(initial_text);
96-
}
97-
98-
parseAndRender();
97+
$("iframe").on("load", function() {
98+
var textarea = $("#text");
99+
var initial_text = getQueryVariable("text");
100+
var smartSelected = getQueryVariable("smart") === "1";
101+
$("#smart").prop("checked", smartSelected);
102+
reader.options.smart = smartSelected;
103+
if (initial_text) {
104+
textarea.val(initial_text);
105+
}
99106

100-
$("#clear-text-box").click(function() {
101-
textarea.val('');
102107
parseAndRender();
103-
});
104108

105-
$("#permalink").click(function() {
106-
var smart = $("#smart").prop('checked');
107-
window.location.pathname = "/index.html";
108-
window.location.search = "text=" + encodeURIComponent(textarea.val()) +
109-
(smart ? '&smart=1' : '');
110-
});
109+
$("#clear-text-box").click(function() {
110+
textarea.val("");
111+
parseAndRender();
112+
});
111113

112-
textarea.bind('input propertychange',
113-
_.debounce(parseAndRender, 50, { maxWait: 100 }));
114-
//textarea.on('scroll', _.debounce(syncScroll, 50, { maxWait: 50 }));
115-
textarea.on('scroll', syncScroll);
116-
textarea.on('keydown click focus',
117-
_.debounce(markSelection, 50, { maxWait: 100}));
114+
$("#permalink").click(function() {
115+
var smart = $("#smart").prop("checked");
116+
window.location.pathname = "/index.html";
117+
window.location.search =
118+
"text=" +
119+
encodeURIComponent(textarea.val()) +
120+
(smart ? "&smart=1" : "");
121+
});
118122

119-
$("#smart").click(function() {
120-
reader.options.smart = $("#smart").prop('checked');
121-
parseAndRender();
123+
textarea.bind(
124+
"input propertychange",
125+
_.debounce(parseAndRender, 50, { maxWait: 100 })
126+
);
127+
//textarea.on('scroll', _.debounce(syncScroll, 50, { maxWait: 50 }));
128+
textarea.on("scroll", syncScroll);
129+
textarea.on(
130+
"keydown click focus",
131+
_.debounce(markSelection, 50, { maxWait: 100 })
132+
);
133+
134+
$("#smart").click(function() {
135+
reader.options.smart = $("#smart").prop("checked");
136+
parseAndRender();
137+
});
122138
});
123-
});
124139
});

0 commit comments

Comments
 (0)