.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-parse / lib / tokenize / definition.js
1 'use strict';
2
3 var whitespace = require('is-whitespace-character');
4 var normalize = require('../util/normalize');
5
6 module.exports = definition;
7 definition.notInList = true;
8 definition.notInBlock = true;
9
10 var C_DOUBLE_QUOTE = '"';
11 var C_SINGLE_QUOTE = '\'';
12 var C_BACKSLASH = '\\';
13 var C_NEWLINE = '\n';
14 var C_TAB = '\t';
15 var C_SPACE = ' ';
16 var C_BRACKET_OPEN = '[';
17 var C_BRACKET_CLOSE = ']';
18 var C_PAREN_OPEN = '(';
19 var C_PAREN_CLOSE = ')';
20 var C_COLON = ':';
21 var C_LT = '<';
22 var C_GT = '>';
23
24 function definition(eat, value, silent) {
25   var self = this;
26   var commonmark = self.options.commonmark;
27   var index = 0;
28   var length = value.length;
29   var subvalue = '';
30   var beforeURL;
31   var beforeTitle;
32   var queue;
33   var character;
34   var test;
35   var identifier;
36   var url;
37   var title;
38
39   while (index < length) {
40     character = value.charAt(index);
41
42     if (character !== C_SPACE && character !== C_TAB) {
43       break;
44     }
45
46     subvalue += character;
47     index++;
48   }
49
50   character = value.charAt(index);
51
52   if (character !== C_BRACKET_OPEN) {
53     return;
54   }
55
56   index++;
57   subvalue += character;
58   queue = '';
59
60   while (index < length) {
61     character = value.charAt(index);
62
63     if (character === C_BRACKET_CLOSE) {
64       break;
65     } else if (character === C_BACKSLASH) {
66       queue += character;
67       index++;
68       character = value.charAt(index);
69     }
70
71     queue += character;
72     index++;
73   }
74
75   if (
76     !queue ||
77     value.charAt(index) !== C_BRACKET_CLOSE ||
78     value.charAt(index + 1) !== C_COLON
79   ) {
80     return;
81   }
82
83   identifier = queue;
84   subvalue += queue + C_BRACKET_CLOSE + C_COLON;
85   index = subvalue.length;
86   queue = '';
87
88   while (index < length) {
89     character = value.charAt(index);
90
91     if (
92       character !== C_TAB &&
93       character !== C_SPACE &&
94       character !== C_NEWLINE
95     ) {
96       break;
97     }
98
99     subvalue += character;
100     index++;
101   }
102
103   character = value.charAt(index);
104   queue = '';
105   beforeURL = subvalue;
106
107   if (character === C_LT) {
108     index++;
109
110     while (index < length) {
111       character = value.charAt(index);
112
113       if (!isEnclosedURLCharacter(character)) {
114         break;
115       }
116
117       queue += character;
118       index++;
119     }
120
121     character = value.charAt(index);
122
123     if (character === isEnclosedURLCharacter.delimiter) {
124       subvalue += C_LT + queue + character;
125       index++;
126     } else {
127       if (commonmark) {
128         return;
129       }
130
131       index -= queue.length + 1;
132       queue = '';
133     }
134   }
135
136   if (!queue) {
137     while (index < length) {
138       character = value.charAt(index);
139
140       if (!isUnclosedURLCharacter(character)) {
141         break;
142       }
143
144       queue += character;
145       index++;
146     }
147
148     subvalue += queue;
149   }
150
151   if (!queue) {
152     return;
153   }
154
155   url = queue;
156   queue = '';
157
158   while (index < length) {
159     character = value.charAt(index);
160
161     if (
162       character !== C_TAB &&
163       character !== C_SPACE &&
164       character !== C_NEWLINE
165     ) {
166       break;
167     }
168
169     queue += character;
170     index++;
171   }
172
173   character = value.charAt(index);
174   test = null;
175
176   if (character === C_DOUBLE_QUOTE) {
177     test = C_DOUBLE_QUOTE;
178   } else if (character === C_SINGLE_QUOTE) {
179     test = C_SINGLE_QUOTE;
180   } else if (character === C_PAREN_OPEN) {
181     test = C_PAREN_CLOSE;
182   }
183
184   if (!test) {
185     queue = '';
186     index = subvalue.length;
187   } else if (queue) {
188     subvalue += queue + character;
189     index = subvalue.length;
190     queue = '';
191
192     while (index < length) {
193       character = value.charAt(index);
194
195       if (character === test) {
196         break;
197       }
198
199       if (character === C_NEWLINE) {
200         index++;
201         character = value.charAt(index);
202
203         if (character === C_NEWLINE || character === test) {
204           return;
205         }
206
207         queue += C_NEWLINE;
208       }
209
210       queue += character;
211       index++;
212     }
213
214     character = value.charAt(index);
215
216     if (character !== test) {
217       return;
218     }
219
220     beforeTitle = subvalue;
221     subvalue += queue + character;
222     index++;
223     title = queue;
224     queue = '';
225   } else {
226     return;
227   }
228
229   while (index < length) {
230     character = value.charAt(index);
231
232     if (character !== C_TAB && character !== C_SPACE) {
233       break;
234     }
235
236     subvalue += character;
237     index++;
238   }
239
240   character = value.charAt(index);
241
242   if (!character || character === C_NEWLINE) {
243     if (silent) {
244       return true;
245     }
246
247     beforeURL = eat(beforeURL).test().end;
248     url = self.decode.raw(self.unescape(url), beforeURL);
249
250     if (title) {
251       beforeTitle = eat(beforeTitle).test().end;
252       title = self.decode.raw(self.unescape(title), beforeTitle);
253     }
254
255     return eat(subvalue)({
256       type: 'definition',
257       identifier: normalize(identifier),
258       title: title || null,
259       url: url
260     });
261   }
262 }
263
264 /* Check if `character` can be inside an enclosed URI. */
265 function isEnclosedURLCharacter(character) {
266   return character !== C_GT &&
267     character !== C_BRACKET_OPEN &&
268     character !== C_BRACKET_CLOSE;
269 }
270
271 isEnclosedURLCharacter.delimiter = C_GT;
272
273 /* Check if `character` can be inside an unclosed URI. */
274 function isUnclosedURLCharacter(character) {
275   return character !== C_BRACKET_OPEN &&
276     character !== C_BRACKET_CLOSE &&
277     !whitespace(character);
278 }