.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-parse / lib / tokenize / heading-setext.js
1 'use strict';
2
3 module.exports = setextHeading;
4
5 var C_NEWLINE = '\n';
6 var C_TAB = '\t';
7 var C_SPACE = ' ';
8 var C_EQUALS = '=';
9 var C_DASH = '-';
10
11 var MAX_HEADING_INDENT = 3;
12
13 /* Map of characters which can be used to mark setext
14  * headers, mapping to their corresponding depth. */
15 var SETEXT_MARKERS = {};
16
17 SETEXT_MARKERS[C_EQUALS] = 1;
18 SETEXT_MARKERS[C_DASH] = 2;
19
20 function setextHeading(eat, value, silent) {
21   var self = this;
22   var now = eat.now();
23   var length = value.length;
24   var index = -1;
25   var subvalue = '';
26   var content;
27   var queue;
28   var character;
29   var marker;
30   var depth;
31
32   /* Eat initial indentation. */
33   while (++index < length) {
34     character = value.charAt(index);
35
36     if (character !== C_SPACE || index >= MAX_HEADING_INDENT) {
37       index--;
38       break;
39     }
40
41     subvalue += character;
42   }
43
44   /* Eat content. */
45   content = '';
46   queue = '';
47
48   while (++index < length) {
49     character = value.charAt(index);
50
51     if (character === C_NEWLINE) {
52       index--;
53       break;
54     }
55
56     if (character === C_SPACE || character === C_TAB) {
57       queue += character;
58     } else {
59       content += queue + character;
60       queue = '';
61     }
62   }
63
64   now.column += subvalue.length;
65   now.offset += subvalue.length;
66   subvalue += content + queue;
67
68   /* Ensure the content is followed by a newline and a
69    * valid marker. */
70   character = value.charAt(++index);
71   marker = value.charAt(++index);
72
73   if (character !== C_NEWLINE || !SETEXT_MARKERS[marker]) {
74     return;
75   }
76
77   subvalue += character;
78
79   /* Eat Setext-line. */
80   queue = marker;
81   depth = SETEXT_MARKERS[marker];
82
83   while (++index < length) {
84     character = value.charAt(index);
85
86     if (character !== marker) {
87       if (character !== C_NEWLINE) {
88         return;
89       }
90
91       index--;
92       break;
93     }
94
95     queue += character;
96   }
97
98   if (silent) {
99     return true;
100   }
101
102   return eat(subvalue + queue)({
103     type: 'heading',
104     depth: depth,
105     children: self.tokenizeInline(content, now)
106   });
107 }