Guest User

Untitled

a guest
May 26th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. var fs = require("fs"),
  2. path = require("path");
  3.  
  4. walk('./', function(filepath, stats) {
  5. if (filepath.endsWith('.md')) {
  6. const filecontent = fs.readFileSync(filepath, { encoding: 'utf-8'});
  7. const converted = convert(filecontent);
  8. fs.writeFileSync(filepath, converted);
  9. console.log(path.basename(filepath), 'done');
  10. }
  11. });
  12.  
  13. function convert(md) {
  14. return md.replace(/((?:^\t[^\n]*\n)+)/gm, function(match, p1, offset, string) {
  15. return "\n```\n" + match + "\n```\n";
  16. });
  17. }
  18.  
  19.  
  20. function walk(dir, callback) {
  21. fs.readdir(dir, function(err, files) {
  22. if (err) throw err;
  23. files.forEach(function(file) {
  24. var filepath = path.join(dir, file);
  25. fs.stat(filepath, function(err,stats) {
  26. if (stats.isDirectory()) {
  27. walk(filepath, callback);
  28. } else if (stats.isFile()) {
  29. callback(filepath, stats);
  30. }
  31. });
  32. });
  33. });
  34. }
Add Comment
Please, Sign In to add comment