.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-parse / lib / tokenize / auto-link.js
1 'use strict';
2
3 var whitespace = require('is-whitespace-character');
4 var decode = require('parse-entities');
5 var locate = require('../locate/tag');
6
7 module.exports = autoLink;
8 autoLink.locator = locate;
9 autoLink.notInLink = true;
10
11 var C_LT = '<';
12 var C_GT = '>';
13 var C_AT_SIGN = '@';
14 var C_SLASH = '/';
15 var MAILTO = 'mailto:';
16 var MAILTO_LENGTH = MAILTO.length;
17
18 /* Tokenise a link. */
19 function autoLink(eat, value, silent) {
20   var self;
21   var subvalue;
22   var length;
23   var index;
24   var queue;
25   var character;
26   var hasAtCharacter;
27   var link;
28   var now;
29   var content;
30   var tokenize;
31   var exit;
32
33   if (value.charAt(0) !== C_LT) {
34     return;
35   }
36
37   self = this;
38   subvalue = '';
39   length = value.length;
40   index = 0;
41   queue = '';
42   hasAtCharacter = false;
43   link = '';
44
45   index++;
46   subvalue = C_LT;
47
48   while (index < length) {
49     character = value.charAt(index);
50
51     if (
52       whitespace(character) ||
53       character === C_GT ||
54       character === C_AT_SIGN ||
55       (character === ':' && value.charAt(index + 1) === C_SLASH)
56     ) {
57       break;
58     }
59
60     queue += character;
61     index++;
62   }
63
64   if (!queue) {
65     return;
66   }
67
68   link += queue;
69   queue = '';
70
71   character = value.charAt(index);
72   link += character;
73   index++;
74
75   if (character === C_AT_SIGN) {
76     hasAtCharacter = true;
77   } else {
78     if (
79       character !== ':' ||
80       value.charAt(index + 1) !== C_SLASH
81     ) {
82       return;
83     }
84
85     link += C_SLASH;
86     index++;
87   }
88
89   while (index < length) {
90     character = value.charAt(index);
91
92     if (whitespace(character) || character === C_GT) {
93       break;
94     }
95
96     queue += character;
97     index++;
98   }
99
100   character = value.charAt(index);
101
102   if (!queue || character !== C_GT) {
103     return;
104   }
105
106   /* istanbul ignore if - never used (yet) */
107   if (silent) {
108     return true;
109   }
110
111   link += queue;
112   content = link;
113   subvalue += link + character;
114   now = eat.now();
115   now.column++;
116   now.offset++;
117
118   if (hasAtCharacter) {
119     if (link.slice(0, MAILTO_LENGTH).toLowerCase() === MAILTO) {
120       content = content.substr(MAILTO_LENGTH);
121       now.column += MAILTO_LENGTH;
122       now.offset += MAILTO_LENGTH;
123     } else {
124       link = MAILTO + link;
125     }
126   }
127
128   /* Temporarily remove support for escapes in autolinks. */
129   tokenize = self.inlineTokenizers.escape;
130   self.inlineTokenizers.escape = null;
131   exit = self.enterLink();
132
133   content = self.tokenizeInline(content, now);
134
135   self.inlineTokenizers.escape = tokenize;
136   exit();
137
138   return eat(subvalue)({
139     type: 'link',
140     title: null,
141     url: decode(link),
142     children: content
143   });
144 }