|
| 1 | +const fetch = require('node-fetch'); |
| 2 | +const cheerio = require('cheerio'); |
| 3 | +const marked = require('../../../'); |
| 4 | +const htmlDiffer = require('../../helpers/html-differ.js'); |
| 5 | +const fs = require('fs'); |
| 6 | +const path = require('path'); |
| 7 | + |
| 8 | +function removeFiles(dir) { |
| 9 | + fs.readdirSync(dir).forEach(file => { |
| 10 | + fs.unlinkSync(path.join(dir, file)); |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | +function updateCommonmark(dir) { |
| 15 | + return fetch('https://raw.githubusercontent.com/commonmark/commonmark.js/master/package.json') |
| 16 | + .then(res => res.json()) |
| 17 | + .then(pkg => pkg.version.replace(/^(\d+\.\d+).*$/, '$1')) |
| 18 | + .then(version => |
| 19 | + fetch(`https://spec.commonmark.org/${version}/spec.json`) |
| 20 | + .then(res => res.json()) |
| 21 | + .then(specs => { |
| 22 | + specs.forEach(spec => { |
| 23 | + const html = marked(spec.markdown, {headerIds: false}); |
| 24 | + if (!htmlDiffer.isEqual(html, spec.html)) { |
| 25 | + spec.shouldFail = true; |
| 26 | + } |
| 27 | + }); |
| 28 | + removeFiles(dir); |
| 29 | + fs.writeFileSync(path.resolve(dir, `./commonmark.${version}.json`), JSON.stringify(specs, null, 2) + '\n'); |
| 30 | + }) |
| 31 | + ) |
| 32 | + .catch((err) => { |
| 33 | + console.error(err); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +function updateGfm(dir) { |
| 38 | + return fetch('https://github.github.com/gfm/') |
| 39 | + .then(res => res.text()) |
| 40 | + .then(html => cheerio.load(html)) |
| 41 | + .then($ => { |
| 42 | + const version = $('.version').text().match(/\d+\.\d+/)[0]; |
| 43 | + if (!version) { |
| 44 | + throw new Error('No version found'); |
| 45 | + } |
| 46 | + const specs = []; |
| 47 | + $('.extension').each((i, ext) => { |
| 48 | + const section = $('.definition', ext).text().trim().replace(/^\d+\.\d+(.*?) \(extension\)[\s\S]*$/, '$1'); |
| 49 | + $('.example', ext).each((j, exa) => { |
| 50 | + const example = +$(exa).attr('id').replace(/\D/g, ''); |
| 51 | + const markdown = $('.language-markdown', exa).text().trim(); |
| 52 | + const html = $('.language-html', exa).text().trim(); |
| 53 | + specs.push({ |
| 54 | + section, |
| 55 | + html, |
| 56 | + markdown, |
| 57 | + example |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + return [version, specs]; |
| 63 | + }) |
| 64 | + .then(([version, specs]) => { |
| 65 | + specs.forEach(spec => { |
| 66 | + const html = marked(spec.markdown, {gfm: true}); |
| 67 | + if (!htmlDiffer.isEqual(html, spec.html)) { |
| 68 | + spec.shouldFail = true; |
| 69 | + } |
| 70 | + }); |
| 71 | + removeFiles(dir); |
| 72 | + fs.writeFileSync(path.resolve(dir, `./gfm.${version}.json`), JSON.stringify(specs, null, 2) + '\n'); |
| 73 | + }) |
| 74 | + .catch((err) => { |
| 75 | + console.error(err); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +updateCommonmark(path.resolve(__dirname, './specs/commonmark')); |
| 80 | +updateGfm(path.resolve(__dirname, './specs/gfm')); |
0 commit comments