-
-
Notifications
You must be signed in to change notification settings - Fork 363
/
Copy pathlink.js
65 lines (55 loc) · 1.54 KB
/
link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
var uri = require('../util/enclose-uri')
var title = require('../util/enclose-title')
module.exports = link
var space = ' '
var leftSquareBracket = '['
var rightSquareBracket = ']'
var leftParenthesis = '('
var rightParenthesis = ')'
// Expression for a protocol:
// See <https://en.wikipedia.org/wiki/Uniform_Resource_Identifier#Generic_syntax>.
var protocol = /^[a-z][a-z+.-]+:\/?/i
// Stringify a link.
//
// When no title exists, the compiled `children` equal `url`, and `url` starts
// with a protocol, an auto link is created:
//
// ```markdown
// <http://example.com>
// ```
//
// Otherwise, is smart about enclosing `url` (see `encloseURI()`) and `title`
// (see `encloseTitle()`).
// ```
//
// ```markdown
// [foo](<foo at bar dot com> 'An "example" e-mail')
// ```
//
// Supports named entities in the `url` and `title` when in `settings.encode`
// mode.
function link(node) {
var self = this
var content = self.encode(node.url || '', node)
var exit = self.enterLink()
var escaped = self.encode(self.escape(node.url || '', node))
var value = self.all(node).join('')
exit()
if (node.title == null && protocol.test(content) && escaped === value) {
// Backslash escapes do not work in autolinks, so we do not escape.
return uri(self.encode(node.url), true)
}
content = uri(content)
if (node.title) {
content += space + title(self.encode(self.escape(node.title, node), node))
}
return (
leftSquareBracket +
value +
rightSquareBracket +
leftParenthesis +
content +
rightParenthesis
)
}