.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-parse / lib / tokenize / heading-atx.js
1 'use strict';
2
3 module.exports = atxHeading;
4
5 var C_NEWLINE = '\n';
6 var C_TAB = '\t';
7 var C_SPACE = ' ';
8 var C_HASH = '#';
9
10 var MAX_ATX_COUNT = 6;
11
12 function atxHeading(eat, value, silent) {
13   var self = this;
14   var settings = self.options;
15   var length = value.length + 1;
16   var index = -1;
17   var now = eat.now();
18   var subvalue = '';
19   var content = '';
20   var character;
21   var queue;
22   var depth;
23
24   /* Eat initial spacing. */
25   while (++index < length) {
26     character = value.charAt(index);
27
28     if (character !== C_SPACE && character !== C_TAB) {
29       index--;
30       break;
31     }
32
33     subvalue += character;
34   }
35
36   /* Eat hashes. */
37   depth = 0;
38
39   while (++index <= length) {
40     character = value.charAt(index);
41
42     if (character !== C_HASH) {
43       index--;
44       break;
45     }
46
47     subvalue += character;
48     depth++;
49   }
50
51   if (depth > MAX_ATX_COUNT) {
52     return;
53   }
54
55   if (
56     !depth ||
57     (!settings.pedantic && value.charAt(index + 1) === C_HASH)
58   ) {
59     return;
60   }
61
62   length = value.length + 1;
63
64   /* Eat intermediate white-space. */
65   queue = '';
66
67   while (++index < length) {
68     character = value.charAt(index);
69
70     if (character !== C_SPACE && character !== C_TAB) {
71       index--;
72       break;
73     }
74
75     queue += character;
76   }
77
78   /* Exit when not in pedantic mode without spacing. */
79   if (
80     !settings.pedantic &&
81     queue.length === 0 &&
82     character &&
83     character !== C_NEWLINE
84   ) {
85     return;
86   }
87
88   if (silent) {
89     return true;
90   }
91
92   /* Eat content. */
93   subvalue += queue;
94   queue = '';
95   content = '';
96
97   while (++index < length) {
98     character = value.charAt(index);
99
100     if (!character || character === C_NEWLINE) {
101       break;
102     }
103
104     if (
105       character !== C_SPACE &&
106       character !== C_TAB &&
107       character !== C_HASH
108     ) {
109       content += queue + character;
110       queue = '';
111       continue;
112     }
113
114     while (character === C_SPACE || character === C_TAB) {
115       queue += character;
116       character = value.charAt(++index);
117     }
118
119     while (character === C_HASH) {
120       queue += character;
121       character = value.charAt(++index);
122     }
123
124     while (character === C_SPACE || character === C_TAB) {
125       queue += character;
126       character = value.charAt(++index);
127     }
128
129     index--;
130   }
131
132   now.column += subvalue.length;
133   now.offset += subvalue.length;
134   subvalue += content + queue;
135
136   return eat(subvalue)({
137     type: 'heading',
138     depth: depth,
139     children: self.tokenizeInline(content, now)
140   });
141 }