Skip to content

Commit a761316

Browse files
authored
Merge pull request #1726 from UziTech/show-rules
Add `npm run rules`
2 parents 982b57e + f8193ed commit a761316

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

docs/CONTRIBUTING.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ To see time comparisons between Marked and other popular Markdown libraries:
8282
npm run bench
8383
```
8484

85+
To see the compiled rules from `src/rules.js`:
86+
87+
```bash
88+
npm run rules
89+
```
90+
91+
You can specify one or more `rule path`s to only show certain rules:
92+
93+
```bash
94+
npm run rules -- block.gfm.item inline.pedantic.br
95+
96+
{
97+
block: {
98+
gfm: {
99+
item: /^( *)((?:[*+-]|\\d{1,9}\\.)) ?[^\\n]*(?:\\n(?!\\1(?:[*+-]|\\d{1,9}\\.) ?)[^\\n]*)*/gm
100+
}
101+
},
102+
inline: {
103+
pedantic: {
104+
br: /^( {2,}|\\\\)\\n(?!\\s*$)/
105+
}
106+
}
107+
}
108+
```
109+
85110
To check for (and fix) standardized syntax (lint):
86111

87112
```bash

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"test:lint": "eslint bin/marked .",
6363
"test:redos": "node test/vuln-regex.js",
6464
"test:update": "node test/update-specs.js",
65+
"rules": "node test/rules.js",
6566
"bench": "npm run rollup && node test/bench.js",
6667
"lint": "eslint --fix bin/marked .",
6768
"build:reset": "git checkout upstream/master lib/marked.js lib/marked.esm.js marked.min.js",

test/rules.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const rules = require('../src/rules.js');
2+
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+
12+
fgBlack: '\x1b[30m',
13+
fgRed: '\x1b[31m',
14+
fgGreen: '\x1b[32m',
15+
fgYellow: '\x1b[33m',
16+
fgBlue: '\x1b[34m',
17+
fgMagenta: '\x1b[35m',
18+
fgCyan: '\x1b[36m',
19+
fgWhite: '\x1b[37m',
20+
21+
bgBlack: '\x1b[40m',
22+
bgRed: '\x1b[41m',
23+
bgGreen: '\x1b[42m',
24+
bgYellow: '\x1b[43m',
25+
bgBlue: '\x1b[44m',
26+
bgMagenta: '\x1b[45m',
27+
bgCyan: '\x1b[46m',
28+
bgWhite: '\x1b[47m'
29+
};
30+
31+
function propsToString(obj) {
32+
if (obj === null) {
33+
return null;
34+
}
35+
if (obj.constructor.name === 'Object') {
36+
if (obj.exec && obj.exec.name === 'noopTest') {
37+
return null;
38+
}
39+
for (const prop in obj) {
40+
obj[prop] = propsToString(obj[prop]);
41+
}
42+
return obj;
43+
}
44+
return obj.toString();
45+
}
46+
47+
let rulesObj = {};
48+
if (process.argv.length > 2) {
49+
for (let i = 2; i < process.argv.length; i++) {
50+
const rulePath = process.argv[i].split('.');
51+
let rulesList = rulesObj;
52+
let rule = rules;
53+
while (rulePath.length > 1) {
54+
const prop = rulePath.shift();
55+
if (!rulesList[prop]) {
56+
rulesList[prop] = {};
57+
rulesList = rulesList[prop];
58+
}
59+
if (rule) {
60+
rule = rule[prop];
61+
}
62+
}
63+
rulesList[rulePath[0]] = rule && rule[rulePath[0]] ? rule[rulePath[0]] : null;
64+
}
65+
} else {
66+
rulesObj = rules;
67+
}
68+
69+
rulesObj = propsToString(rulesObj);
70+
let output = JSON.stringify(rulesObj, null, 2);
71+
output = output.replace(/^(\s*)"(.*)": null,?$/gm, `$1${COLOR.fgGreen}$2${COLOR.reset}: undefined`);
72+
output = output.replace(/^(\s*)"(.*)": {$/gm, `$1${COLOR.fgGreen}$2${COLOR.reset}: {`);
73+
output = output.replace(/^(\s*)"(.*)": "(.*)",?$/gm, `$1${COLOR.fgGreen}$2${COLOR.reset}: ${COLOR.fgRed}$3${COLOR.reset}`);
74+
console.log(output, COLOR.reset);

0 commit comments

Comments
 (0)