This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
override-link-destination-parser.js
98 lines (83 loc) · 2.27 KB
/
override-link-destination-parser.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
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
// Override the markdown-it helper rule to allow spaces in the src attributes of images and the href attributes of
// anchor elements:
// e.g., ![Gitter](https://badges.gitter.im/Join Chat.svg)
//
// This is a modified version of the stock markdown-it helper for parsing link destinations
'use strict'
function parseLinkDestination (md, str, pos, max) {
var code
var level
var lines = 0
var start = pos
var result = {
ok: false,
pos: 0,
lines: 0,
str: ''
}
var isSpace = md.utils.isSpace
var unescapeAll = md.utils.unescapeAll
if (str.charCodeAt(pos) === 0x3C /* < */) {
pos++
while (pos < max) {
code = str.charCodeAt(pos)
if (code === 0x0A /* \n */ || isSpace(code)) { return result }
if (code === 0x3E /* > */) {
result.pos = pos + 1
result.str = unescapeAll(str.slice(start + 1, pos))
result.ok = true
return result
}
if (code === 0x5C /* \ */ && pos + 1 < max) {
pos += 2
continue
}
pos++
}
// no closing '>'
return result
}
// this should be ... } else { ... branch
level = 0
while (pos < max) {
code = str.charCodeAt(pos)
if (code === 0x20) { // space
pos++ // 0x22 => double quote, 0x27 => single quote, 0x28 => '(', 0x29 => ')'
while (pos < max && code !== 0x22 && code !== 0x27 && code !== 0x28 && code !== 0x29) {
code = str.charCodeAt(pos++)
}
pos--
if (pos === max) { // end of the line
break
}
if (code === 0x22 || code === 0x27 || code === 0x28) { // single/double quotes or opening paren for title attributes
pos--
break
}
}
// ascii control characters
if (code < 0x20 || code === 0x7F) { break }
if (code === 0x5C /* \ */ && pos + 1 < max) {
pos += 2
continue
}
if (code === 0x28 /* ( */) {
level++
if (level > 1) { break }
}
if (code === 0x29 /* ) */) {
level--
if (level < 0) { break }
}
pos++
}
if (start === pos) { return result }
result.str = unescapeAll(str.slice(start, pos))
result.lines = lines
result.pos = pos
result.ok = true
return result
}
module.exports = function (md) {
md.helpers.parseLinkDestination = parseLinkDestination.bind(this, md)
}