forked from markedjs/marked
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-specs.js
86 lines (80 loc) · 2.92 KB
/
update-specs.js
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
import { readdirSync, unlinkSync, writeFileSync } from 'node:fs';
import { join, resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { load } from 'cheerio';
import { htmlIsEqual } from '@markedjs/testutils';
import { Marked } from '../lib/marked.esm.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
function removeFiles(dir) {
readdirSync(dir).forEach(file => {
unlinkSync(join(dir, file));
});
}
async function updateCommonmark(dir, options) {
try {
const res = await fetch('https://raw.githubusercontent.com/commonmark/commonmark.js/master/package.json');
const pkg = await res.json();
const { version } = pkg;
const res2 = await fetch(`https://spec.commonmark.org/${version}/spec.json`);
const json = await res2.json();
const specs = await Promise.all(json.map(async(spec) => {
const marked = new Marked();
const html = marked.parse(spec.markdown, options);
const isEqual = await htmlIsEqual(html, spec.html);
if (!isEqual) {
spec.shouldFail = true;
}
return spec;
}));
writeFileSync(resolve(dir, `./commonmark.${version}.json`), JSON.stringify(specs, null, 2) + '\n');
console.log(`Saved CommonMark v${version} specs`);
} catch (ex) {
console.log(ex);
}
}
async function updateGfm(dir) {
try {
const res = await fetch('https://github.github.com/gfm/');
const html = await res.text();
const $ = load(html);
const version = $('.version').text().match(/\d+\.\d+/)[0];
if (!version) {
throw new Error('No version found');
}
let specs = [];
$('.extension').each((i, ext) => {
const section = $('.definition', ext).text().trim().replace(/^\d+\.\d+(.*?) \(extension\)[\s\S]*$/, '$1');
$('.example', ext).each((j, exa) => {
const example = +$(exa).attr('id').replace(/\D/g, '');
const markdown = $('.language-markdown', exa).text().trim();
const html = $('.language-html', exa).text().trim();
specs.push({
section: `[extension] ${section}`,
html,
markdown,
example,
});
});
});
specs = await Promise.all(specs.map(async(spec) => {
const marked = new Marked();
const html = marked.parse(spec.markdown, { gfm: true, pedantic: false });
const isEqual = await htmlIsEqual(html, spec.html);
if (!isEqual) {
spec.shouldFail = true;
}
return spec;
}));
writeFileSync(resolve(dir, `./gfm.${version}.json`), JSON.stringify(specs, null, 2) + '\n');
console.log(`Saved GFM v${version} specs.`);
} catch (ex) {
console.log(ex);
}
}
const commonmarkDir = resolve(__dirname, './specs/commonmark');
const gfmDir = resolve(__dirname, './specs/gfm');
removeFiles(commonmarkDir);
removeFiles(gfmDir);
updateCommonmark(commonmarkDir, { gfm: false, pedantic: false });
updateCommonmark(gfmDir, { gfm: true, pedantic: false });
updateGfm(gfmDir);