3 var whitespace = require('is-whitespace-character');
4 var normalize = require('../util/normalize');
6 module.exports = definition;
7 definition.notInList = true;
8 definition.notInBlock = true;
10 var C_DOUBLE_QUOTE = '"';
11 var C_SINGLE_QUOTE = '\'';
12 var C_BACKSLASH = '\\';
16 var C_BRACKET_OPEN = '[';
17 var C_BRACKET_CLOSE = ']';
18 var C_PAREN_OPEN = '(';
19 var C_PAREN_CLOSE = ')';
24 function definition(eat, value, silent) {
26 var commonmark = self.options.commonmark;
28 var length = value.length;
39 while (index < length) {
40 character = value.charAt(index);
42 if (character !== C_SPACE && character !== C_TAB) {
46 subvalue += character;
50 character = value.charAt(index);
52 if (character !== C_BRACKET_OPEN) {
57 subvalue += character;
60 while (index < length) {
61 character = value.charAt(index);
63 if (character === C_BRACKET_CLOSE) {
65 } else if (character === C_BACKSLASH) {
68 character = value.charAt(index);
77 value.charAt(index) !== C_BRACKET_CLOSE ||
78 value.charAt(index + 1) !== C_COLON
84 subvalue += queue + C_BRACKET_CLOSE + C_COLON;
85 index = subvalue.length;
88 while (index < length) {
89 character = value.charAt(index);
92 character !== C_TAB &&
93 character !== C_SPACE &&
94 character !== C_NEWLINE
99 subvalue += character;
103 character = value.charAt(index);
105 beforeURL = subvalue;
107 if (character === C_LT) {
110 while (index < length) {
111 character = value.charAt(index);
113 if (!isEnclosedURLCharacter(character)) {
121 character = value.charAt(index);
123 if (character === isEnclosedURLCharacter.delimiter) {
124 subvalue += C_LT + queue + character;
131 index -= queue.length + 1;
137 while (index < length) {
138 character = value.charAt(index);
140 if (!isUnclosedURLCharacter(character)) {
158 while (index < length) {
159 character = value.charAt(index);
162 character !== C_TAB &&
163 character !== C_SPACE &&
164 character !== C_NEWLINE
173 character = value.charAt(index);
176 if (character === C_DOUBLE_QUOTE) {
177 test = C_DOUBLE_QUOTE;
178 } else if (character === C_SINGLE_QUOTE) {
179 test = C_SINGLE_QUOTE;
180 } else if (character === C_PAREN_OPEN) {
181 test = C_PAREN_CLOSE;
186 index = subvalue.length;
188 subvalue += queue + character;
189 index = subvalue.length;
192 while (index < length) {
193 character = value.charAt(index);
195 if (character === test) {
199 if (character === C_NEWLINE) {
201 character = value.charAt(index);
203 if (character === C_NEWLINE || character === test) {
214 character = value.charAt(index);
216 if (character !== test) {
220 beforeTitle = subvalue;
221 subvalue += queue + character;
229 while (index < length) {
230 character = value.charAt(index);
232 if (character !== C_TAB && character !== C_SPACE) {
236 subvalue += character;
240 character = value.charAt(index);
242 if (!character || character === C_NEWLINE) {
247 beforeURL = eat(beforeURL).test().end;
248 url = self.decode.raw(self.unescape(url), beforeURL);
251 beforeTitle = eat(beforeTitle).test().end;
252 title = self.decode.raw(self.unescape(title), beforeTitle);
255 return eat(subvalue)({
257 identifier: normalize(identifier),
258 title: title || null,
264 /* Check if `character` can be inside an enclosed URI. */
265 function isEnclosedURLCharacter(character) {
266 return character !== C_GT &&
267 character !== C_BRACKET_OPEN &&
268 character !== C_BRACKET_CLOSE;
271 isEnclosedURLCharacter.delimiter = C_GT;
273 /* Check if `character` can be inside an unclosed URI. */
274 function isUnclosedURLCharacter(character) {
275 return character !== C_BRACKET_OPEN &&
276 character !== C_BRACKET_CLOSE &&
277 !whitespace(character);