Repository navigation
Expand file tree
/
Copy pathfuzz.ts
More file actions
199 lines (175 loc) · 4.52 KB
/
Copy pathfuzz.ts
File metadata and controls
199 lines (175 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Seeded Markdown generator and mutator for differential fuzzing against commonmark.js (core) and
// micromark + micromark-extension-gfm (GFM). Biased toward nesting, lists, emphasis, fences, and tables.
import * as commonmark from "commonmark"
import { tests as specTests } from "commonmark-spec"
import { micromark } from "micromark"
import { gfm, gfmHtml } from "micromark-extension-gfm"
import { createMarkdownStream, renderBlocksToHtml } from "../src/index"
export function rng(seed: number) {
let s = seed >>> 0 || 1
return () => {
s ^= s << 13
s >>>= 0
s ^= s >>> 17
s ^= s << 5
s >>>= 0
return s / 0x100000000
}
}
const PREFIXES = ["", "", "", "- ", "* ", "+ ", "1. ", "2) ", "10. ", "> ", " ", " ", " ", "\t", "- ", "1. "]
const LEADERS = [
"# ",
"## ",
"###### ",
"```",
"~~~",
"````js",
"```py x",
"---",
"***",
"___",
"===",
"- - -",
"<div>",
"</div>",
"<!-- c -->",
"<pre>",
"</pre>",
"[x]: /url",
'[y]: <a b> "t"',
"[Foo bar]:\n/u",
" code",
"",
]
const TABLE = ["| a | b |", "|---|---|", "| - | :-: |", "a | b", "--|--", "| x |", "|:--|", "| `c|d` | e\\|f |"]
const INLINE = [
"foo",
"bar",
"baz",
"*",
"**",
"***",
"_",
"__",
"`",
"``",
"[",
"]",
"(",
")",
"",
'[a](<b c> "t")',
"[x]",
"[x][]",
"[y][x]",
"",
" ",
" ",
"\t",
" \n",
"\\\n",
"*a*",
"**b**",
"_c_",
"__d__",
"`e`",
"a*b",
"a_b",
"*(",
")*",
'"',
"'",
"ä",
"😀",
"[ ]",
"[x]",
"---",
"1.",
"#",
"|",
]
function inlineText(r: () => number, max: number): string {
let out = ""
const n = 1 + Math.floor(r() * max)
for (let i = 0; i < n; i++) {
out += INLINE[Math.floor(r() * INLINE.length)]!
if (r() < 0.5) out += " "
}
return out
}
export function generate(r: () => number, gfmMode: boolean): string {
const lines: string[] = []
const count = 1 + Math.floor(r() * 8)
for (let i = 0; i < count; i++) {
let line = ""
const depth = Math.floor(r() * r() * 4)
for (let d = 0; d < depth; d++) line += PREFIXES[Math.floor(r() * PREFIXES.length)]!
const roll = r()
if (gfmMode && roll < 0.15) line += TABLE[Math.floor(r() * TABLE.length)]!
else if (roll < 0.4) line += LEADERS[Math.floor(r() * LEADERS.length)]! + (r() < 0.5 ? inlineText(r, 3) : "")
else if (roll < 0.5) line += ""
else line += inlineText(r, 6)
lines.push(line)
}
return lines.join("\n") + (r() < 0.7 ? "\n" : "")
}
const SPECIALS = "*_`[]()!<>&\\~|#-+=:.> \n\t1"
const specExamples: string[] = specTests.map((e) => e.markdown.replace(/→/g, "\t"))
export function mutate(r: () => number): string {
let s = specExamples[Math.floor(r() * specExamples.length)]!
const edits = 1 + Math.floor(r() * 4)
for (let e = 0; e < edits; e++) {
const at = Math.floor(r() * (s.length + 1))
const roll = r()
if (roll < 0.4) s = s.slice(0, at) + SPECIALS[Math.floor(r() * SPECIALS.length)] + s.slice(at)
else if (roll < 0.7) s = s.slice(0, at) + s.slice(at + 1)
else {
const other = specExamples[Math.floor(r() * specExamples.length)]!
const from = Math.floor(r() * other.length)
s = s.slice(0, at) + other.slice(from, from + 1 + Math.floor(r() * 12)) + s.slice(at)
}
}
return s
}
const cmParser = new commonmark.Parser()
const cmRenderer = new commonmark.HtmlRenderer()
export function referenceCore(md: string): string {
return cmRenderer.render(cmParser.parse(md))
}
export function referenceGfm(md: string): string {
return micromark(md, { allowDangerousHtml: true, extensions: [gfm()], htmlExtensions: [gfmHtml()] })
}
export function patchmark(md: string, gfmMode: boolean): string {
const opts = { gfm: gfmMode, math: false }
return renderBlocksToHtml(createMarkdownStream(opts).update(md, { streaming: false }).blocks, opts)
}
/** Normalize insignificant HTML differences between renderers (whitespace between tags, void-tag syntax, attribute order) */
export function normalizeHtml(html: string): string {
return html
.replace(/<input ([^>]*?)\s*\/?>/g, (_m, attrs: string) => `<input ${attrs.trim().split(/\s+/).sort().join(" ")}>`)
.replace(/(<input [^>]+>)\n/g, "$1 ")
.replace(/<(br|hr|img[^>]*?)\s*\/?>/g, "<$1>")
.replace(/\s+<\/li>/g, "</li>")
.replace(/>\s+</g, "><")
.replace(/\s+$/g, "")
.replace(/^\s+/g, "")
}