-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathparser_inline.js
166 lines (143 loc) · 3.95 KB
/
parser_inline.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
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
import Ruler from './ruler';
import StateInline from './rules_inline/state_inline';
import * as utils from './common/utils';
import text from './rules_inline/text';
import newline from './rules_inline/newline';
import escape from './rules_inline/escape';
import backticks from './rules_inline/backticks';
import del from './rules_inline/del';
import ins from './rules_inline/ins';
import mark from './rules_inline/mark';
import emphasis from './rules_inline/emphasis';
import sub from './rules_inline/sub';
import sup from './rules_inline/sup';
import links from './rules_inline/links';
import footnote_inline from './rules_inline/footnote_inline';
import footnote_ref from './rules_inline/footnote_ref';
import autolink from './rules_inline/autolink';
import htmltag from './rules_inline/htmltag';
import entity from './rules_inline/entity';
/**
* Inline Parser `rules`
*/
var _rules = [
[ 'text', text ],
[ 'newline', newline ],
[ 'escape', escape ],
[ 'backticks', backticks ],
[ 'del', del ],
[ 'ins', ins ],
[ 'mark', mark ],
[ 'emphasis', emphasis ],
[ 'sub', sub ],
[ 'sup', sup ],
[ 'links', links ],
[ 'footnote_inline', footnote_inline ],
[ 'footnote_ref', footnote_ref ],
[ 'autolink', autolink ],
[ 'htmltag', htmltag ],
[ 'entity', entity ]
];
/**
* Inline Parser class. Note that link validation is stricter
* in Remarkable than what is specified by CommonMark. If you
* want to change this you can use a custom validator.
*
* @api private
*/
export default function ParserInline() {
this.ruler = new Ruler();
for (var i = 0; i < _rules.length; i++) {
this.ruler.push(_rules[i][0], _rules[i][1]);
}
// Can be overridden with a custom validator
this.validateLink = validateLink;
}
/**
* Skip a single token by running all rules in validation mode.
* Returns `true` if any rule reports success.
*
* @param {Object} `state`
* @api privage
*/
ParserInline.prototype.skipToken = function (state) {
var rules = this.ruler.getRules('');
var len = rules.length;
var pos = state.pos;
var i, cached_pos;
if ((cached_pos = state.cacheGet(pos)) > 0) {
state.pos = cached_pos;
return;
}
for (i = 0; i < len; i++) {
if (rules[i](state, true)) {
state.cacheSet(pos, state.pos);
return;
}
}
state.pos++;
state.cacheSet(pos, state.pos);
};
/**
* Generate tokens for the given input range.
*
* @param {Object} `state`
* @api private
*/
ParserInline.prototype.tokenize = function (state) {
var rules = this.ruler.getRules('');
var len = rules.length;
var end = state.posMax;
var ok, i;
while (state.pos < end) {
// Try all possible rules.
// On success, the rule should:
//
// - update `state.pos`
// - update `state.tokens`
// - return true
for (i = 0; i < len; i++) {
ok = rules[i](state, false);
if (ok) {
break;
}
}
if (ok) {
if (state.pos >= end) { break; }
continue;
}
state.pending += state.src[state.pos++];
}
if (state.pending) {
state.pushPending();
}
};
/**
* Parse the given input string.
*
* @param {String} `str`
* @param {Object} `options`
* @param {Object} `env`
* @param {Array} `outTokens`
* @api private
*/
ParserInline.prototype.parse = function (str, options, env, outTokens) {
var state = new StateInline(str, this, options, env, outTokens);
this.tokenize(state);
};
/**
* Validate the given `url` by checking for bad protocols.
*
* @param {String} `url`
* @return {Boolean}
*/
function validateLink(url) {
var BAD_PROTOCOLS = [ 'vbscript', 'javascript', 'file', 'data' ];
var str = url.trim().toLowerCase();
// Care about digital entities "javascript:alert(1)"
str = utils.replaceEntities(str);
if (str.indexOf(':') !== -1 && BAD_PROTOCOLS.indexOf(str.split(':')[0]) !== -1) {
return false;
}
return true;
}