getSpecs.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. fetch('https://github.github.com/gfm/')
  7. .then(res => res.text())
  8. .then(html => cheerio.load(html))
  9. .then($ => {
  10. const version = $('.version').text().match(/\d+\.\d+/)[0];
  11. if (!version) {
  12. throw new Error('No version found');
  13. }
  14. const specs = [];
  15. $('.extension').each((i, ext) => {
  16. const section = $('.definition', ext).text().trim().replace(/^\d+\.\d+(.*?) \(extension\)[\s\S]*$/, '$1');
  17. $('.example', ext).each((j, exa) => {
  18. const example = +$(exa).attr('id').replace(/\D/g, '');
  19. const markdown = $('.language-markdown', exa).text().trim();
  20. const html = $('.language-html', exa).text().trim();
  21. specs.push({
  22. section,
  23. html,
  24. markdown,
  25. example
  26. });
  27. });
  28. });
  29. return [version, specs];
  30. })
  31. .then(([version, specs]) => {
  32. specs.forEach(spec => {
  33. const html = marked(spec.markdown, {gfm: true});
  34. if (!htmlDiffer.isEqual(html, spec.html)) {
  35. spec.shouldFail = true;
  36. }
  37. });
  38. fs.writeFileSync(`gfm.${version}.json`, JSON.stringify(specs, null, 2) + '\n');
  39. })
  40. .catch((err) => {
  41. console.error(err);
  42. });