.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-parse / lib / tokenize / reference.js
1 'use strict';
2
3 var whitespace = require('is-whitespace-character');
4 var locate = require('../locate/link');
5 var normalize = require('../util/normalize');
6
7 module.exports = reference;
8 reference.locator = locate;
9
10 var T_LINK = 'link';
11 var T_IMAGE = 'image';
12 var T_FOOTNOTE = 'footnote';
13 var REFERENCE_TYPE_SHORTCUT = 'shortcut';
14 var REFERENCE_TYPE_COLLAPSED = 'collapsed';
15 var REFERENCE_TYPE_FULL = 'full';
16 var C_CARET = '^';
17 var C_BACKSLASH = '\\';
18 var C_BRACKET_OPEN = '[';
19 var C_BRACKET_CLOSE = ']';
20
21 function reference(eat, value, silent) {
22   var self = this;
23   var character = value.charAt(0);
24   var index = 0;
25   var length = value.length;
26   var subvalue = '';
27   var intro = '';
28   var type = T_LINK;
29   var referenceType = REFERENCE_TYPE_SHORTCUT;
30   var content;
31   var identifier;
32   var now;
33   var node;
34   var exit;
35   var queue;
36   var bracketed;
37   var depth;
38
39   /* Check whether we’re eating an image. */
40   if (character === '!') {
41     type = T_IMAGE;
42     intro = character;
43     character = value.charAt(++index);
44   }
45
46   if (character !== C_BRACKET_OPEN) {
47     return;
48   }
49
50   index++;
51   intro += character;
52   queue = '';
53
54   /* Check whether we’re eating a footnote. */
55   if (
56     self.options.footnotes &&
57     type === T_LINK &&
58     value.charAt(index) === C_CARET
59   ) {
60     intro += C_CARET;
61     index++;
62     type = T_FOOTNOTE;
63   }
64
65   /* Eat the text. */
66   depth = 0;
67
68   while (index < length) {
69     character = value.charAt(index);
70
71     if (character === C_BRACKET_OPEN) {
72       bracketed = true;
73       depth++;
74     } else if (character === C_BRACKET_CLOSE) {
75       if (!depth) {
76         break;
77       }
78
79       depth--;
80     }
81
82     if (character === C_BACKSLASH) {
83       queue += C_BACKSLASH;
84       character = value.charAt(++index);
85     }
86
87     queue += character;
88     index++;
89   }
90
91   subvalue = queue;
92   content = queue;
93   character = value.charAt(index);
94
95   if (character !== C_BRACKET_CLOSE) {
96     return;
97   }
98
99   index++;
100   subvalue += character;
101   queue = '';
102
103   while (index < length) {
104     character = value.charAt(index);
105
106     if (!whitespace(character)) {
107       break;
108     }
109
110     queue += character;
111     index++;
112   }
113
114   character = value.charAt(index);
115
116   /* Inline footnotes cannot have an identifier. */
117   if (type !== T_FOOTNOTE && character === C_BRACKET_OPEN) {
118     identifier = '';
119     queue += character;
120     index++;
121
122     while (index < length) {
123       character = value.charAt(index);
124
125       if (character === C_BRACKET_OPEN || character === C_BRACKET_CLOSE) {
126         break;
127       }
128
129       if (character === C_BACKSLASH) {
130         identifier += C_BACKSLASH;
131         character = value.charAt(++index);
132       }
133
134       identifier += character;
135       index++;
136     }
137
138     character = value.charAt(index);
139
140     if (character === C_BRACKET_CLOSE) {
141       referenceType = identifier ? REFERENCE_TYPE_FULL : REFERENCE_TYPE_COLLAPSED;
142       queue += identifier + character;
143       index++;
144     } else {
145       identifier = '';
146     }
147
148     subvalue += queue;
149     queue = '';
150   } else {
151     if (!content) {
152       return;
153     }
154
155     identifier = content;
156   }
157
158   /* Brackets cannot be inside the identifier. */
159   if (referenceType !== REFERENCE_TYPE_FULL && bracketed) {
160     return;
161   }
162
163   subvalue = intro + subvalue;
164
165   if (type === T_LINK && self.inLink) {
166     return null;
167   }
168
169   /* istanbul ignore if - never used (yet) */
170   if (silent) {
171     return true;
172   }
173
174   if (type === T_FOOTNOTE && content.indexOf(' ') !== -1) {
175     return eat(subvalue)({
176       type: 'footnote',
177       children: this.tokenizeInline(content, eat.now())
178     });
179   }
180
181   now = eat.now();
182   now.column += intro.length;
183   now.offset += intro.length;
184   identifier = referenceType === REFERENCE_TYPE_FULL ? identifier : content;
185
186   node = {
187     type: type + 'Reference',
188     identifier: normalize(identifier)
189   };
190
191   if (type === T_LINK || type === T_IMAGE) {
192     node.referenceType = referenceType;
193   }
194
195   if (type === T_LINK) {
196     exit = self.enterLink();
197     node.children = self.tokenizeInline(content, now);
198     exit();
199   } else if (type === T_IMAGE) {
200     node.alt = self.decode.raw(self.unescape(content), now) || null;
201   }
202
203   return eat(subvalue)(node);
204 }