summaryrefslogtreecommitdiffstats
path: root/packages/markdown/marked/test/index.js
blob: 5027ee5b90bc6c5d185005cfe53537a8dfee7b23 (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#!/usr/bin/env node
'use strict';
// 'use strict' is here so we can use let and const in node 4

/**
 * marked tests
 * Copyright (c) 2011-2013, Christopher Jeffrey. (MIT Licensed)
 * https://github.com/markedjs/marked
 */

/**
 * Modules
 */

const fs = require('fs');
const path = require('path');
const fm = require('front-matter');
const g2r = require('glob-to-regexp');
let marked = require('../');
const htmlDiffer = require('./helpers/html-differ.js');

/**
 * Load Tests
 */

function load(options) {
  options = options || {};
  const dir = path.join(__dirname, 'compiled_tests');
  const glob = g2r(options.glob || '*', { extended: true });

  const list = fs
    .readdirSync(dir)
    .filter(file => {
      return path.extname(file) === '.md';
    })
    .sort();

  const files = list.reduce((obj, item) => {
    const name = path.basename(item, '.md');
    if (glob.test(name)) {
      const file = path.join(dir, item);
      const content = fm(fs.readFileSync(file, 'utf8'));

      obj[name] = {
        options: content.attributes,
        text: content.body,
        html: fs.readFileSync(file.replace(/[^.]+$/, 'html'), 'utf8')
      };
    }
    return obj;
  }, {});

  if (options.bench || options.time) {
    if (!options.glob) {
      // Change certain tests to allow
      // comparison to older benchmark times.
      fs.readdirSync(path.join(__dirname, 'new')).forEach(name => {
        if (path.extname(name) === '.html') return;
        if (name === 'main.md') return;
        delete files[name];
      });
    }

    if (files['backslash_escapes.md']) {
      files['backslash_escapes.md'] = {
        text: 'hello world \\[how](are you) today'
      };
    }

    if (files['main.md']) {
      files['main.md'].text = files['main.md'].text.replace('* * *\n\n', '');
    }
  }

  return files;
}

/**
 * Test Runner
 */

function runTests(engine, options) {
  if (typeof engine !== 'function') {
    options = engine;
    engine = null;
  }

  engine = engine || marked;
  options = options || {};

  let succeeded = 0;
  let failed = 0;
  const files = options.files || load(options);
  const filenames = Object.keys(files);

  if (options.marked) {
    marked.setOptions(options.marked);
  }

  for (let i = 0; i < filenames.length; i++) {
    const filename = filenames[i];
    const file = files[filename];

    const success = testFile(engine, file, filename, i + 1);

    if (success) {
      succeeded++;
    } else {
      failed++;
      if (options.stop) {
        break;
      }
    }
  }

  console.log('\n%d/%d tests completed successfully.', succeeded, filenames.length);
  if (failed) console.log('%d/%d tests failed.', failed, filenames.length);

  return !failed;
}

/**
 * Test a file
 */

function testFile(engine, file, filename, index) {
  const opts = Object.keys(file.options);

  if (marked._original) {
    marked.defaults = marked._original;
    delete marked._original;
  }

  console.log('#%d. Test %s', index, filename);

  if (opts.length) {
    marked._original = marked.defaults;
    marked.defaults = {};
    Object.keys(marked._original).forEach(key => {
      marked.defaults[key] = marked._original[key];
    });
    opts.forEach(key => {
      if (marked.defaults.hasOwnProperty(key)) {
        marked.defaults[key] = file.options[key];
      }
    });
  }

  const before = process.hrtime();

  let text, html, elapsed;
  try {
    text = engine(file.text);
    html = file.html;
  } catch (e) {
    elapsed = process.hrtime(before);
    console.log('\n    failed in %dms\n', prettyElapsedTime(elapsed));
    throw e;
  }

  elapsed = process.hrtime(before);

  if (htmlDiffer.isEqual(text, html)) {
    if (elapsed[0] > 0) {
      console.log('\n    failed because it took too long.\n\n    passed in %dms\n', prettyElapsedTime(elapsed));
      return false;
    }
    console.log('    passed in %dms', prettyElapsedTime(elapsed));
    return true;
  }

  const diff = htmlDiffer.firstDiff(text, html);

  console.log('\n    failed in %dms', prettyElapsedTime(elapsed));
  console.log('    Expected: %s', diff.expected);
  console.log('      Actual: %s\n', diff.actual);
  return false;
}

/**
 * Benchmark a function
 */

function bench(name, files, engine) {
  const start = Date.now();

  for (let i = 0; i < 1000; i++) {
    for (const filename in files) {
      engine(files[filename].text);
    }
  }

  const end = Date.now();

  console.log('%s completed in %dms.', name, end - start);
}

/**
 * Benchmark all engines
 */

function runBench(options) {
  options = options || {};
  const files = load(options);

  // Non-GFM, Non-pedantic
  marked.setOptions({
    gfm: false,
    tables: false,
    breaks: false,
    pedantic: false,
    sanitize: false,
    smartLists: false
  });
  if (options.marked) {
    marked.setOptions(options.marked);
  }
  bench('marked', files, marked);

  // GFM
  marked.setOptions({
    gfm: true,
    tables: false,
    breaks: false,
    pedantic: false,
    sanitize: false,
    smartLists: false
  });
  if (options.marked) {
    marked.setOptions(options.marked);
  }
  bench('marked (gfm)', files, marked);

  // Pedantic
  marked.setOptions({
    gfm: false,
    tables: false,
    breaks: false,
    pedantic: true,
    sanitize: false,
    smartLists: false
  });
  if (options.marked) {
    marked.setOptions(options.marked);
  }
  bench('marked (pedantic)', files, marked);

  try {
    bench('commonmark', files, (() => {
      const commonmark = require('commonmark');
      const parser = new commonmark.Parser();
      const writer = new commonmark.HtmlRenderer();
      return function (text) {
        return writer.render(parser.parse(text));
      };
    })());
  } catch (e) {
    console.log('Could not bench commonmark. (Error: %s)', e.message);
  }

  try {
    bench('markdown-it', files, (() => {
      const MarkdownIt = require('markdown-it');
      const md = new MarkdownIt();
      return md.render.bind(md);
    })());
  } catch (e) {
    console.log('Could not bench markdown-it. (Error: %s)', e.message);
  }

  try {
    bench('markdown.js', files, (() => {
      const markdown = require('markdown').markdown;
      return markdown.toHTML.bind(markdown);
    })());
  } catch (e) {
    console.log('Could not bench markdown.js. (Error: %s)', e.message);
  }

  return true;
}

/**
 * A simple one-time benchmark
 */

function time(options) {
  options = options || {};
  const files = load(options);
  if (options.marked) {
    marked.setOptions(options.marked);
  }
  bench('marked', files, marked);

  return true;
}

/**
 * Markdown Test Suite Fixer
 *   This function is responsible for "fixing"
 *   the markdown test suite. There are
 *   certain aspects of the suite that
 *   are strange or might make tests
 *   fail for reasons unrelated to
 *   conformance.
 */

function fix() {
  ['compiled_tests', 'original', 'new', 'redos'].forEach(dir => {
    try {
      fs.mkdirSync(path.resolve(__dirname, dir));
    } catch (e) {
      // directory already exists
    }
  });

  // rm -rf tests
  fs.readdirSync(path.resolve(__dirname, 'compiled_tests')).forEach(file => {
    fs.unlinkSync(path.resolve(__dirname, 'compiled_tests', file));
  });

  // cp -r original tests
  fs.readdirSync(path.resolve(__dirname, 'original')).forEach(file => {
    let text = fs.readFileSync(path.resolve(__dirname, 'original', file), 'utf8');

    if (path.extname(file) === '.md') {
      if (fm.test(text)) {
        text = fm(text);
        text = `---\n${text.frontmatter}\ngfm: false\n---\n${text.body}`;
      } else {
        text = `---\ngfm: false\n---\n${text}`;
      }
    }

    fs.writeFileSync(path.resolve(__dirname, 'compiled_tests', file), text);
  });

  // node fix.js
  const dir = path.join(__dirname, 'compiled_tests');

  fs.readdirSync(dir).filter(file => {
    return path.extname(file) === '.html';
  }).forEach(file => {
    file = path.join(dir, file);
    let html = fs.readFileSync(file, 'utf8');

    // fix unencoded quotes
    html = html
      .replace(/='([^\n']*)'(?=[^<>\n]*>)/g, '=&__APOS__;$1&__APOS__;')
      .replace(/="([^\n"]*)"(?=[^<>\n]*>)/g, '=&__QUOT__;$1&__QUOT__;')
      .replace(/"/g, '&quot;')
      .replace(/'/g, '&#39;')
      .replace(/&__QUOT__;/g, '"')
      .replace(/&__APOS__;/g, '\'');

    fs.writeFileSync(file, html);
  });

  // turn <hr /> into <hr>
  fs.readdirSync(dir).forEach(file => {
    file = path.join(dir, file);
    let text = fs.readFileSync(file, 'utf8');

    text = text.replace(/(<|&lt;)hr\s*\/(>|&gt;)/g, '$1hr$2');

    fs.writeFileSync(file, text);
  });

  // markdown does some strange things.
  // it does not encode naked `>`, marked does.
  {
    const file = `${dir}/amps_and_angles_encoding.html`;
    const html = fs.readFileSync(file, 'utf8')
      .replace('6 > 5.', '6 &gt; 5.');

    fs.writeFileSync(file, html);
  }

  // cp new/* tests/
  fs.readdirSync(path.resolve(__dirname, 'new')).forEach(file => {
    fs.writeFileSync(path.resolve(__dirname, 'compiled_tests', file),
      fs.readFileSync(path.resolve(__dirname, 'new', file)));
  });

  // cp redos/* tests/
  fs.readdirSync(path.resolve(__dirname, 'redos')).forEach(file => {
    fs.writeFileSync(path.resolve(__dirname, 'compiled_tests', file),
      fs.readFileSync(path.resolve(__dirname, 'redos', file)));
  });
}

/**
 * Argument Parsing
 */

function parseArg(argv) {
  argv = argv.slice(2);

  const options = {};
  const orphans = [];

  function getarg() {
    let arg = argv.shift();

    if (arg.indexOf('--') === 0) {
      // e.g. --opt
      arg = arg.split('=');
      if (arg.length > 1) {
        // e.g. --opt=val
        argv.unshift(arg.slice(1).join('='));
      }
      arg = arg[0];
    } else if (arg[0] === '-') {
      if (arg.length > 2) {
        // e.g. -abc
        argv = arg.substring(1).split('').map(ch => {
          return `-${ch}`;
        }).concat(argv);
        arg = argv.shift();
      } else {
        // e.g. -a
      }
    } else {
      // e.g. foo
    }

    return arg;
  }

  while (argv.length) {
    let arg = getarg();
    switch (arg) {
      case '-f':
      case '--fix':
      case 'fix':
        if (options.fix !== false) {
          options.fix = true;
        }
        break;
      case '--no-fix':
      case 'no-fix':
        options.fix = false;
        break;
      case '-b':
      case '--bench':
        options.bench = true;
        break;
      case '-s':
      case '--stop':
        options.stop = true;
        break;
      case '-t':
      case '--time':
        options.time = true;
        break;
      case '-m':
      case '--minified':
        options.minified = true;
        break;
      case '--glob':
        arg = argv.shift();
        options.glob = arg.replace(/^=/, '');
        break;
      default:
        if (arg.indexOf('--') === 0) {
          const opt = camelize(arg.replace(/^--(no-)?/, ''));
          if (!marked.defaults.hasOwnProperty(opt)) {
            continue;
          }
          options.marked = options.marked || {};
          if (arg.indexOf('--no-') === 0) {
            options.marked[opt] = typeof marked.defaults[opt] !== 'boolean'
              ? null
              : false;
          } else {
            options.marked[opt] = typeof marked.defaults[opt] !== 'boolean'
              ? argv.shift()
              : true;
          }
        } else {
          orphans.push(arg);
        }
        break;
    }
  }

  return options;
}

/**
 * Helpers
 */

function camelize(text) {
  return text.replace(/(\w)-(\w)/g, (_, a, b) => a + b.toUpperCase());
}

/**
 * Main
 */

function main(argv) {
  const opt = parseArg(argv);

  if (opt.fix !== false) {
    fix();
  }

  if (opt.fix) {
    // only run fix
    return;
  }

  if (opt.bench) {
    return runBench(opt);
  }

  if (opt.time) {
    return time(opt);
  }

  if (opt.minified) {
    marked = require('../marked.min.js');
  }
  return runTests(opt);
}

/**
 * Execute
 */

if (!module.parent) {
  process.title = 'marked';
  process.exit(main(process.argv.slice()) ? 0 : 1);
} else {
  exports = main;
  exports.main = main;
  exports.runTests = runTests;
  exports.testFile = testFile;
  exports.runBench = runBench;
  exports.load = load;
  exports.bench = bench;
  module.exports = exports;
}

// returns time to millisecond granularity
function prettyElapsedTime(hrtimeElapsed) {
  const seconds = hrtimeElapsed[0];
  const frac = Math.round(hrtimeElapsed[1] / 1e3) / 1e3;
  return seconds * 1e3 + frac;
}