.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-parse / lib / tokenize / footnote-definition.js
1 'use strict';
2
3 var whitespace = require('is-whitespace-character');
4 var normalize = require('../util/normalize');
5
6 module.exports = footnoteDefinition;
7 footnoteDefinition.notInList = true;
8 footnoteDefinition.notInBlock = true;
9
10 var C_BACKSLASH = '\\';
11 var C_NEWLINE = '\n';
12 var C_TAB = '\t';
13 var C_SPACE = ' ';
14 var C_BRACKET_OPEN = '[';
15 var C_BRACKET_CLOSE = ']';
16 var C_CARET = '^';
17 var C_COLON = ':';
18
19 var EXPRESSION_INITIAL_TAB = /^( {4}|\t)?/gm;
20
21 function footnoteDefinition(eat, value, silent) {
22   var self = this;
23   var offsets = self.offset;
24   var index;
25   var length;
26   var subvalue;
27   var now;
28   var currentLine;
29   var content;
30   var queue;
31   var subqueue;
32   var character;
33   var identifier;
34   var add;
35   var exit;
36
37   if (!self.options.footnotes) {
38     return;
39   }
40
41   index = 0;
42   length = value.length;
43   subvalue = '';
44   now = eat.now();
45   currentLine = now.line;
46
47   while (index < length) {
48     character = value.charAt(index);
49
50     if (!whitespace(character)) {
51       break;
52     }
53
54     subvalue += character;
55     index++;
56   }
57
58   if (
59     value.charAt(index) !== C_BRACKET_OPEN ||
60     value.charAt(index + 1) !== C_CARET
61   ) {
62     return;
63   }
64
65   subvalue += C_BRACKET_OPEN + C_CARET;
66   index = subvalue.length;
67   queue = '';
68
69   while (index < length) {
70     character = value.charAt(index);
71
72     if (character === C_BRACKET_CLOSE) {
73       break;
74     } else if (character === C_BACKSLASH) {
75       queue += character;
76       index++;
77       character = value.charAt(index);
78     }
79
80     queue += character;
81     index++;
82   }
83
84   if (
85     !queue ||
86     value.charAt(index) !== C_BRACKET_CLOSE ||
87     value.charAt(index + 1) !== C_COLON
88   ) {
89     return;
90   }
91
92   if (silent) {
93     return true;
94   }
95
96   identifier = normalize(queue);
97   subvalue += queue + C_BRACKET_CLOSE + C_COLON;
98   index = subvalue.length;
99
100   while (index < length) {
101     character = value.charAt(index);
102
103     if (character !== C_TAB && character !== C_SPACE) {
104       break;
105     }
106
107     subvalue += character;
108     index++;
109   }
110
111   now.column += subvalue.length;
112   now.offset += subvalue.length;
113   queue = '';
114   content = '';
115   subqueue = '';
116
117   while (index < length) {
118     character = value.charAt(index);
119
120     if (character === C_NEWLINE) {
121       subqueue = character;
122       index++;
123
124       while (index < length) {
125         character = value.charAt(index);
126
127         if (character !== C_NEWLINE) {
128           break;
129         }
130
131         subqueue += character;
132         index++;
133       }
134
135       queue += subqueue;
136       subqueue = '';
137
138       while (index < length) {
139         character = value.charAt(index);
140
141         if (character !== C_SPACE) {
142           break;
143         }
144
145         subqueue += character;
146         index++;
147       }
148
149       if (subqueue.length === 0) {
150         break;
151       }
152
153       queue += subqueue;
154     }
155
156     if (queue) {
157       content += queue;
158       queue = '';
159     }
160
161     content += character;
162     index++;
163   }
164
165   subvalue += content;
166
167   content = content.replace(EXPRESSION_INITIAL_TAB, function (line) {
168     offsets[currentLine] = (offsets[currentLine] || 0) + line.length;
169     currentLine++;
170
171     return '';
172   });
173
174   add = eat(subvalue);
175
176   exit = self.enterBlock();
177   content = self.tokenizeBlock(content, now);
178   exit();
179
180   return add({
181     type: 'footnoteDefinition',
182     identifier: identifier,
183     children: content
184   });
185 }