.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / remark-stringify / lib / visitors / link.js
1 'use strict';
2
3 var uri = require('../util/enclose-uri');
4 var title = require('../util/enclose-title');
5
6 module.exports = link;
7
8 /* Expression for a protocol:
9  * http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax */
10 var PROTOCOL = /^[a-z][a-z+.-]+:\/?/i;
11
12 /* Stringify a link.
13  *
14  * When no title exists, the compiled `children` equal
15  * `url`, and `url` starts with a protocol, an auto
16  * link is created:
17  *
18  *     <http://example.com>
19  *
20  * Otherwise, is smart about enclosing `url` (see
21  * `encloseURI()`) and `title` (see `encloseTitle()`).
22  *
23  *    [foo](<foo at bar dot com> 'An "example" e-mail')
24  *
25  * Supports named entities in the `url` and `title` when
26  * in `settings.encode` mode. */
27 function link(node) {
28   var self = this;
29   var content = self.encode(node.url || '', node);
30   var exit = self.enterLink();
31   var escaped = self.encode(self.escape(node.url || '', node));
32   var value = self.all(node).join('');
33
34   exit();
35
36   if (
37     node.title == null &&
38     PROTOCOL.test(content) &&
39     (escaped === value || escaped === 'mailto:' + value)
40   ) {
41     /* Backslash escapes do not work in autolinks,
42      * so we do not escape. */
43     return uri(self.encode(node.url), true);
44   }
45
46   content = uri(content);
47
48   if (node.title) {
49     content += ' ' + title(self.encode(self.escape(node.title, node), node));
50   }
51
52   return '[' + value + '](' + content + ')';
53 }