rules.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { Lexer } from '../lib/marked.esm.js';
  2. const rules = Lexer.rules;
  3. const COLOR = {
  4. reset: '\x1b[0m',
  5. bright: '\x1b[1m',
  6. dim: '\x1b[2m',
  7. underscore: '\x1b[4m',
  8. blink: '\x1b[5m',
  9. reverse: '\x1b[7m',
  10. hidden: '\x1b[8m',
  11. fgBlack: '\x1b[30m',
  12. fgRed: '\x1b[31m',
  13. fgGreen: '\x1b[32m',
  14. fgYellow: '\x1b[33m',
  15. fgBlue: '\x1b[34m',
  16. fgMagenta: '\x1b[35m',
  17. fgCyan: '\x1b[36m',
  18. fgWhite: '\x1b[37m',
  19. bgBlack: '\x1b[40m',
  20. bgRed: '\x1b[41m',
  21. bgGreen: '\x1b[42m',
  22. bgYellow: '\x1b[43m',
  23. bgBlue: '\x1b[44m',
  24. bgMagenta: '\x1b[45m',
  25. bgCyan: '\x1b[46m',
  26. bgWhite: '\x1b[47m',
  27. };
  28. function propsToString(obj) {
  29. if (obj === null) {
  30. return null;
  31. }
  32. if (obj.constructor.name === 'Object') {
  33. if (obj.exec?.name === 'noopTest') {
  34. return null;
  35. }
  36. for (const prop in obj) {
  37. obj[prop] = propsToString(obj[prop]);
  38. }
  39. return obj;
  40. }
  41. return obj.toString();
  42. }
  43. let rulesObj = {};
  44. if (process.argv.length > 2) {
  45. for (let i = 2; i < process.argv.length; i++) {
  46. const rulePath = process.argv[i].split('.');
  47. let rulesList = rulesObj;
  48. let rule = rules;
  49. while (rulePath.length > 1) {
  50. const prop = rulePath.shift();
  51. if (!rulesList[prop]) {
  52. rulesList[prop] = {};
  53. rulesList = rulesList[prop];
  54. }
  55. if (rule) {
  56. rule = rule[prop];
  57. }
  58. }
  59. rulesList[rulePath[0]] = rule?.[rulePath[0]] ?? null;
  60. }
  61. } else {
  62. rulesObj = rules;
  63. }
  64. rulesObj = propsToString(rulesObj);
  65. let output = JSON.stringify(rulesObj, null, 2);
  66. output = output.replace(/^(\s*)"(.*)": null,?$/gm, `$1${COLOR.fgGreen}$2${COLOR.reset}: undefined`);
  67. output = output.replace(/^(\s*)"(.*)": {$/gm, `$1${COLOR.fgGreen}$2${COLOR.reset}: {`);
  68. output = output.replace(/^(\s*)"(.*)": "(.*)",?$/gm, (...p) => {
  69. return `${p[1]}${COLOR.fgGreen}${p[2]}${COLOR.reset}: ${COLOR.fgRed}${p[3].replace(/\\\\/g, '\\')}${COLOR.reset}`;
  70. });
  71. console.log(output, COLOR.reset);