-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
parse-error.js
62 lines (53 loc) · 1.57 KB
/
parse-error.js
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
import fs from 'fs'
import {u} from 'unist-builder'
import {unified} from 'unified'
import parse from 'remark-parse'
import {zone} from 'mdast-zone'
import {errors} from '../packages/rehype-parse/errors.js'
const pkg = JSON.parse(fs.readFileSync('package.json'))
const repo = pkg.repository
const whatwg =
'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
const base = 'https://github.com/' + repo + '/blob/main'
const ignoreFixture = {
surrogateInInputStream: true
}
export default function remarkParseErrors() {
return transform
}
function transform(tree) {
zone(tree, 'parse-error', visit)
}
function visit(start, _, end) {
return [
start,
u(
'list',
{ordered: false, spread: false},
Object.keys(errors).map((name) => {
const info = errors[name]
const kebab = name.replace(/[A-Z]/g, ($0) => '-' + $0.toLowerCase())
const reason =
info.reason.charAt(0).toLowerCase() + info.reason.slice(1)
const head = u('inlineCode', name)
const fields = [
info.url === false ? head : u('link', {url: whatwg + kebab}, [head]),
u('text', ' — ')
].concat(unified().use(parse).parse(reason).children)
if (!ignoreFixture[name]) {
fields.push(
u('text', ' ('),
u(
'link',
{url: base + '/test/parse-error/' + kebab + '/index.html'},
[u('text', 'example')]
),
u('text', ')')
)
}
return u('listItem', [u('paragraph', fields)])
})
),
end
]
}