reformat-mdx.js 786 B

123456789101112131415161718192021222324252627282930313233343536
  1. #!/usr/bin/env node
  2. 'use strict'
  3. const fs = require('fs'),
  4. path = require('path'),
  5. glob = require('glob'),
  6. beautifyHtml = require('js-beautify').html;
  7. const docs = glob
  8. .sync(path.join(__dirname, `../docs/**/*.mdx`))
  9. docs.forEach((file, i) => {
  10. const oldContent = fs.readFileSync(file, 'utf8')
  11. // get codeblocks from markdown
  12. const content = oldContent.replace(/(```([a-z0-9]+).*?\n)(.*?)(```)/gs, (m, m1, m2, m3, m4) => {
  13. if (m2 === 'html') {
  14. let m3m = beautifyHtml(m3, {
  15. "indent_size": 2,
  16. "indent_char": " ",
  17. }).trim();
  18. // remove empty lines
  19. m3m = m3m.replace(/^\s*[\r\n]/gm, '');
  20. return m1 + m3m + "\n" + m4;
  21. }
  22. return m
  23. })
  24. if (content !== oldContent) {
  25. fs.writeFileSync(file, content, 'utf8')
  26. console.log(`Reformatted ${file}`)
  27. }
  28. })