-
-
Notifications
You must be signed in to change notification settings - Fork 907
Expand file tree
/
Copy pathdefault.js
More file actions
310 lines (290 loc) · 10.2 KB
/
default.js
File metadata and controls
310 lines (290 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"use strict";
(function main() {
// Dependencies
var markdownit = globalThis.markdownit;
var markdownlint = globalThis.markdownlint;
var micromark = markdownlint;
// DOM elements
var markdown = document.getElementById("markdown");
var markup = document.getElementById("markup");
var numbered = document.getElementById("numbered");
var violations = document.getElementById("violations");
var form = document.getElementsByTagName("form")[0];
var openFile = document.getElementById("openFile");
var copyLink = document.getElementById("copyLink");
// Variables
var newLineRe = /\r\n|\r|\n/;
var hashPrefix = "%m";
var allLintErrors = [];
// Do-nothing function
function noop() {}
// Sanitize string for HTML display
function sanitize(str) {
return str
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
}
// Renders directive metadata
function handleDirective(directive) {
const content = directive.content;
delete directive.content;
if (content) {
this.tag("<blockquote>");
}
this.tag("<code>");
this.raw(this.encode(JSON.stringify(directive)));
this.tag("</code>");
this.raw(content);
if (content) {
this.tag("</blockquote>");
}
}
// Renders Markdown to HTML
function render(markdown) {
markdown = markdown.replace(markdownlint.frontMatterRe, "");
const match = /^\?renderer=([a-z-]+)$/.exec(globalThis.location.search);
const renderer = match ? match[1] : "micromark";
if (renderer === "markdown-it") {
return markdownit({ "html": true }).render(markdown);
} else if (renderer === "micromark") {
const parseOptions = {
"extensions": [
micromark.directive(),
micromark.gfmAutolinkLiteral(),
micromark.gfmFootnote(),
micromark.gfmTable(),
micromark.math()
]
};
const context = micromark.parse(parseOptions);
const chunks = micromark.preprocess()(markdown, undefined, true);
const events = micromark.postprocess(context.document().write(chunks));
const compileOptions = {
"allowDangerousHtml": true,
"htmlExtensions": [
micromark.directiveHtml({ "*": handleDirective }),
micromark.gfmAutolinkLiteralHtml(),
micromark.gfmFootnoteHtml(),
micromark.gfmTableHtml(),
micromark.mathHtml()
]
};
try {
return micromark.compile(compileOptions)(events);
} catch(error) {
return `[Exception: "${error}"]`;
}
}
return `[Unsupported renderer "${renderer}"]`;
}
// Highlight ranges
function highlightRanges(results, className) {
for (const result of results) {
const { errorRange, lineNumber } = result;
const line = document.getElementById(`l${lineNumber}`);
line.classList.add(className);
if (errorRange) {
const [ col, len ] = errorRange;
for (let i = 0; i < len; i++) {
var char = document.getElementById(`l${lineNumber}c${col + i}`);
char.classList.add(className);
}
}
}
}
// Handle input
function onMarkdownInput() {
// Markdown
var content = markdown.value;
// Markup
markup.innerHTML = render(content);
// Numbered
var lines = content.split(newLineRe);
var padding = lines.length.toString().replace(/\d/g, " ");
numbered.innerHTML = lines
.map(function mapNumberedLine(line, index) {
index++;
var paddedIndex = (padding + index).slice(-padding.length);
return (
`<span><em id='l${index}'>${paddedIndex}</em>: ` +
// eslint-disable-next-line unicorn/prefer-spread
line.split("").map((c, i) => `<span id='l${index}c${i + 1}'>${sanitize(c)}</span>`).join("") +
"</span>"
);
}).join("\n");
// Violations
var options = {
"strings": {
"content": content
},
"config": {
"MD013": false
},
"handleRuleFailures": true
};
allLintErrors = markdownlint.lintSync(options).content;
violations.innerHTML = allLintErrors.map(function mapResult(result) {
var ruleName = result.ruleNames.slice(0, 2).join(" / ");
var resultJson = encodeURIComponent(JSON.stringify(result)).replaceAll("'", "%27");
return "<em><a href='#line' target='" + resultJson + "'>" +
result.lineNumber + "</a></em> - <a href='" + result.ruleInformation +
"'>" + ruleName + "</a> " +
result.ruleDescription +
(result.errorDetail ?
" [<span class='detail'>" +
sanitize(result.errorDetail) +
"</span>]" :
"") +
(result.errorContext ?
" [<span class='detail'>Context: \"" +
sanitize(result.errorContext) +
"\"</span>]" :
"") +
" [<span class='detail'>" +
result.severity +
"</span>]" +
(result.fixInfo ?
" [<a href='#fix' target='" +
resultJson +
"' class='detail'>Fix</a>]" :
"");
}).join("<br/>");
// Highlight errors and warnings
highlightRanges(allLintErrors.filter((error) => error.severity === "warning"), "warning");
highlightRanges(allLintErrors.filter((error) => error.severity === "error"), "error");
}
// Load from a string or File object
function loadMarkdown(source) {
// Reset input element
form.reset();
if (typeof source === "string") {
// Update from string
markdown.value = source;
onMarkdownInput();
} else {
// Update from File object
source.text().then((text) => {
markdown.value = text;
onMarkdownInput();
});
}
}
// Handle drag-and-drop
function onDragOver(e) {
if (e.dataTransfer && e.dataTransfer.dropEffect) {
e.dataTransfer.dropEffect = "link";
e.preventDefault();
}
}
function onDrop(e) {
if (e.dataTransfer && e.dataTransfer.files) {
loadMarkdown(e.dataTransfer.files[0]);
e.preventDefault();
}
}
// Handle file open
function onOpenFileChange(e) {
if (e.target && e.target.files) {
loadMarkdown(e.target.files[0]);
}
}
// Handle violation navigation
function onViolationClick(e) {
const resultJson = JSON.parse(decodeURIComponent(e.target.target));
switch (e.target.hash) {
case "#fix":
var errors = e.shiftKey ?
allLintErrors :
[ resultJson ];
var fixed = markdownlint.applyFixes(markdown.value, errors);
markdown.value = fixed;
onMarkdownInput();
e.preventDefault();
break;
case "#line":
// eslint-disable-next-line unicorn/no-useless-spread
for (const element of [ ...document.getElementsByClassName("highlight") ]) {
element.classList.remove("highlight");
}
highlightRanges([ resultJson ], "highlight");
var line = document.getElementById(`l${resultJson.lineNumber}`);
line.scrollIntoView();
e.preventDefault();
break;
default:
break;
}
}
// Updates the URL hash and copies the URL to the clipboard
function onCopyLinkClick(e) {
globalThis.location.hash = encodeURIComponent(hashPrefix + markdown.value);
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(globalThis.location).then(noop, noop);
} else {
/* eslint-disable-next-line no-alert */
alert("Document URL updated, select and copy it now.");
}
e.preventDefault();
}
// Show library version
var version = markdownlint.getVersion();
document.getElementById("version").textContent = "(v" + version + ")";
// Add event listeners
document.body.addEventListener("dragover", onDragOver);
document.body.addEventListener("drop", onDrop);
openFile.addEventListener("change", onOpenFileChange);
markdown.addEventListener("input", onMarkdownInput);
violations.addEventListener("click", onViolationClick, true);
copyLink.addEventListener("click", onCopyLinkClick);
markdown.value = [
"## Introduction",
"",
"`markdownlint` is a [Node.js](https://nodejs.org/) style checker and lint tool for [Markdown](https://en.wikipedia.org/wiki/Markdown)/[CommonMark](https://commonmark.org/) files to automatically validate content, prevent rendering problems, and promote consistency.",
"This page offers an easy way to try it out interactively!",
"",
"#### Instructions",
"",
"Type or paste `Markdown ` content in the upper-left box, drag-and-drop a file, or open one with the chooser at the top.",
"Content gets parsed and displayed in the upper-right box; rule violations (if any) show up in the lower-right box.",
"Click a violation for information about it or click its line number to highlighted it in the lower-left box.",
"",
"> *Note*: [All rules](https://github.com/DavidAnson/markdownlint/blob/v" + version + "/doc/Rules.md) are enabled except [MD013/line-length](https://github.com/DavidAnson/markdownlint/blob/v" + version + "/doc/md013.md).",
"",
"",
"#### Resources",
"* [`markdownlint` on GitHub](https://github.com/DavidAnson/markdownlint)",
"* [`markdownlint` on npm](https://www.npmjs.com/package/markdownlint)",
"* [Markdown specification](https://daringfireball.net/projects/markdown/)",
"*\t[CommonMark specification](https://commonmark.org/)",
"",
"#### Thanks",
"",
"[`markdownlint/Ruby`](https://github.com/markdownlint/markdownlint) for the inspiration and [`markdown-it`](https://github.com/markdown-it/markdown-it) for the parser and interactive demo idea!",
""
].join("\n");
// Update Markdown from hash (if present)
if (globalThis.location.hash) {
try {
var decodedHash = decodeURIComponent(globalThis.location.hash.substring(1));
if (hashPrefix === decodedHash.substring(0, hashPrefix.length)) {
markdown.value = decodedHash.substring(hashPrefix.length);
}
} catch {
// Invalid
}
}
// Detect legacy browsers
try {
/* eslint-disable-next-line no-new */
new URL("https://example.com/");
} catch {
markdown.value = [
"# Sorry",
"",
"This browser is not supported."
].join("\n");
}
// Initialize
onMarkdownInput();
}());