update-specs.js 2.9 KB

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