.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-parse / lib / tokenize / code-fenced.js
1 'use strict';
2
3 var trim = require('trim-trailing-lines');
4
5 module.exports = fencedCode;
6
7 var C_NEWLINE = '\n';
8 var C_TAB = '\t';
9 var C_SPACE = ' ';
10 var C_TILDE = '~';
11 var C_TICK = '`';
12
13 var MIN_FENCE_COUNT = 3;
14 var CODE_INDENT_COUNT = 4;
15
16 function fencedCode(eat, value, silent) {
17   var self = this;
18   var settings = self.options;
19   var length = value.length + 1;
20   var index = 0;
21   var subvalue = '';
22   var fenceCount;
23   var marker;
24   var character;
25   var flag;
26   var queue;
27   var content;
28   var exdentedContent;
29   var closing;
30   var exdentedClosing;
31   var indent;
32   var now;
33
34   if (!settings.gfm) {
35     return;
36   }
37
38   /* Eat initial spacing. */
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   indent = index;
51
52   /* Eat the fence. */
53   character = value.charAt(index);
54
55   if (character !== C_TILDE && character !== C_TICK) {
56     return;
57   }
58
59   index++;
60   marker = character;
61   fenceCount = 1;
62   subvalue += character;
63
64   while (index < length) {
65     character = value.charAt(index);
66
67     if (character !== marker) {
68       break;
69     }
70
71     subvalue += character;
72     fenceCount++;
73     index++;
74   }
75
76   if (fenceCount < MIN_FENCE_COUNT) {
77     return;
78   }
79
80   /* Eat spacing before flag. */
81   while (index < length) {
82     character = value.charAt(index);
83
84     if (character !== C_SPACE && character !== C_TAB) {
85       break;
86     }
87
88     subvalue += character;
89     index++;
90   }
91
92   /* Eat flag. */
93   flag = '';
94   queue = '';
95
96   while (index < length) {
97     character = value.charAt(index);
98
99     if (
100       character === C_NEWLINE ||
101       character === C_TILDE ||
102       character === C_TICK
103     ) {
104       break;
105     }
106
107     if (character === C_SPACE || character === C_TAB) {
108       queue += character;
109     } else {
110       flag += queue + character;
111       queue = '';
112     }
113
114     index++;
115   }
116
117   character = value.charAt(index);
118
119   if (character && character !== C_NEWLINE) {
120     return;
121   }
122
123   if (silent) {
124     return true;
125   }
126
127   now = eat.now();
128   now.column += subvalue.length;
129   now.offset += subvalue.length;
130
131   subvalue += flag;
132   flag = self.decode.raw(self.unescape(flag), now);
133
134   if (queue) {
135     subvalue += queue;
136   }
137
138   queue = '';
139   closing = '';
140   exdentedClosing = '';
141   content = '';
142   exdentedContent = '';
143
144   /* Eat content. */
145   while (index < length) {
146     character = value.charAt(index);
147     content += closing;
148     exdentedContent += exdentedClosing;
149     closing = '';
150     exdentedClosing = '';
151
152     if (character !== C_NEWLINE) {
153       content += character;
154       exdentedClosing += character;
155       index++;
156       continue;
157     }
158
159     /* Add the newline to `subvalue` if its the first
160      * character.  Otherwise, add it to the `closing`
161      * queue. */
162     if (content) {
163       closing += character;
164       exdentedClosing += character;
165     } else {
166       subvalue += character;
167     }
168
169     queue = '';
170     index++;
171
172     while (index < length) {
173       character = value.charAt(index);
174
175       if (character !== C_SPACE) {
176         break;
177       }
178
179       queue += character;
180       index++;
181     }
182
183     closing += queue;
184     exdentedClosing += queue.slice(indent);
185
186     if (queue.length >= CODE_INDENT_COUNT) {
187       continue;
188     }
189
190     queue = '';
191
192     while (index < length) {
193       character = value.charAt(index);
194
195       if (character !== marker) {
196         break;
197       }
198
199       queue += character;
200       index++;
201     }
202
203     closing += queue;
204     exdentedClosing += queue;
205
206     if (queue.length < fenceCount) {
207       continue;
208     }
209
210     queue = '';
211
212     while (index < length) {
213       character = value.charAt(index);
214
215       if (character !== C_SPACE && character !== C_TAB) {
216         break;
217       }
218
219       closing += character;
220       exdentedClosing += character;
221       index++;
222     }
223
224     if (!character || character === C_NEWLINE) {
225       break;
226     }
227   }
228
229   subvalue += content + closing;
230
231   return eat(subvalue)({
232     type: 'code',
233     lang: flag || null,
234     value: trim(exdentedContent)
235   });
236 }