Mirror of Awesome Self Hosted
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
5 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. // Accepts input of any filename, ie. node test.js README.md
  2. const fs = require('fs');
  3. let log = '{\n';
  4. let issuelog = ' "message": "#### Syntax Issues\\n\\n Name | Entry\\n----|----------------------\\n';
  5. let fails = ''
  6. const file = fs.readFileSync(process.argv[2], 'utf8'); // Reads argv into var file
  7. function entryFilter(md) { // Function to find lines with entries
  8. const linepatt = /^\s{0,2}-\s\[.*`/;
  9. return linepatt.test(md);
  10. }
  11. function split(text) { // Function to split lines into array
  12. return text.split(/\r?\n/);
  13. }
  14. function findPattern(text) { // All entries should match this pattern. If matches pattern returns true.
  15. const patt = /^\s{0,2}-\s\[.*?\]\(.*?\) (`⚠` )?- .{0,249}?\.( \(\[(Demo|Source Code|Clients)\]\([^)]*\)(, \[(Source Code|Clients)\]\([^)]*\))?(, \[(Source Code|Clients)\]\([^)]*\))*\))? \`.*?\` \`.*?\`$/;
  16. if (patt.test(text) === true) {
  17. return true;
  18. }
  19. return false;
  20. }
  21. function entryErrorCheck(md) {
  22. const namepatt = /^\s{0,2}-\s\[(.*?)\]/; // regex pattern to find name of entryArray
  23. const entries = split(md); // Inserts each line into the entries array
  24. let totalFail = 0;
  25. let totalPass = 0;
  26. let total = 0;
  27. const entryArray = [];
  28. if (entries[0] === "") {
  29. console.log("0 Entries")
  30. process.exit(0)
  31. }
  32. for (let i = 0, len = entries.length; i < len; i += 1) { // Loop to create array of objects
  33. entryArray[i] = new Object;
  34. entryArray[i].raw = entries[i];
  35. if (entryFilter(entries[i]) === true) { // filter out lines that don't start with * [)
  36. total += 1;
  37. entryArray[i].name = namepatt.exec(entries[i])[1]; // Parses name of entry
  38. entryArray[i].pass = findPattern(entries[i]); // Tests against known patterns
  39. if (entryArray[i].pass === true) { // If entry passes increment totalPass counter
  40. totalPass += 1;
  41. } else {
  42. console.log(`${entryArray[i].name} Failed.`); // If entry fails increment totalFail counter and append error to issuelog
  43. // entryArray[i].error = findError(entries[i]) //WIP
  44. totalFail += 1;
  45. issuelog += `${entryArray[i].name} | ${entries[i]} \\n`;
  46. fails += `${entries[i]} \n\n`;
  47. }
  48. }
  49. }
  50. if (totalFail > 0) { // Logs # passed & failed to console, and failures to syntaxcheck.json
  51. console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total}\n-----------------------------`);
  52. console.log(fails)
  53. log += ` "error": true,\n "title": "Found ${totalFail} entries with syntax error(s).",\n`;
  54. fs.writeFileSync('syntaxcheck.json', `${log} ${issuelog} "\n}`);
  55. process.exit(1);
  56. } else { // Logs # of entries passed to console and error: false to syntaxcheck.json
  57. console.log(`${totalFail} Failed, ${totalPass} Passed, of ${total} \n`);
  58. log += ' "error": false\n}';
  59. fs.writeFileSync('syntaxcheck.json', log);
  60. process.exit(0);
  61. }
  62. }
  63. entryErrorCheck(file);