.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-parse / lib / tokenize / paragraph.js
1 'use strict';
2
3 var trim = require('trim');
4 var decimal = require('is-decimal');
5 var trimTrailingLines = require('trim-trailing-lines');
6 var interrupt = require('../util/interrupt');
7
8 module.exports = paragraph;
9
10 var C_NEWLINE = '\n';
11 var C_TAB = '\t';
12 var C_SPACE = ' ';
13
14 var TAB_SIZE = 4;
15
16 /* Tokenise paragraph. */
17 function paragraph(eat, value, silent) {
18   var self = this;
19   var settings = self.options;
20   var commonmark = settings.commonmark;
21   var gfm = settings.gfm;
22   var tokenizers = self.blockTokenizers;
23   var interruptors = self.interruptParagraph;
24   var index = value.indexOf(C_NEWLINE);
25   var length = value.length;
26   var position;
27   var subvalue;
28   var character;
29   var size;
30   var now;
31
32   while (index < length) {
33     /* Eat everything if there’s no following newline. */
34     if (index === -1) {
35       index = length;
36       break;
37     }
38
39     /* Stop if the next character is NEWLINE. */
40     if (value.charAt(index + 1) === C_NEWLINE) {
41       break;
42     }
43
44     /* In commonmark-mode, following indented lines
45      * are part of the paragraph. */
46     if (commonmark) {
47       size = 0;
48       position = index + 1;
49
50       while (position < length) {
51         character = value.charAt(position);
52
53         if (character === C_TAB) {
54           size = TAB_SIZE;
55           break;
56         } else if (character === C_SPACE) {
57           size++;
58         } else {
59           break;
60         }
61
62         position++;
63       }
64
65       if (size >= TAB_SIZE) {
66         index = value.indexOf(C_NEWLINE, index + 1);
67         continue;
68       }
69     }
70
71     subvalue = value.slice(index + 1);
72
73     /* Check if the following code contains a possible
74      * block. */
75     if (interrupt(interruptors, tokenizers, self, [eat, subvalue, true])) {
76       break;
77     }
78
79     /* Break if the following line starts a list, when
80      * already in a list, or when in commonmark, or when
81      * in gfm mode and the bullet is *not* numeric. */
82     if (
83       tokenizers.list.call(self, eat, subvalue, true) &&
84       (
85         self.inList ||
86         commonmark ||
87         (gfm && !decimal(trim.left(subvalue).charAt(0)))
88       )
89     ) {
90       break;
91     }
92
93     position = index;
94     index = value.indexOf(C_NEWLINE, index + 1);
95
96     if (index !== -1 && trim(value.slice(position, index)) === '') {
97       index = position;
98       break;
99     }
100   }
101
102   subvalue = value.slice(0, index);
103
104   if (trim(subvalue) === '') {
105     eat(subvalue);
106
107     return null;
108   }
109
110   /* istanbul ignore if - never used (yet) */
111   if (silent) {
112     return true;
113   }
114
115   now = eat.now();
116   subvalue = trimTrailingLines(subvalue);
117
118   return eat(subvalue)({
119     type: 'paragraph',
120     children: self.tokenizeInline(subvalue, now)
121   });
122 }