forked from jonschlinkert/remarkable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
references.js
95 lines (74 loc) · 2.78 KB
/
references.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import StateInline from '../rules_inline/state_inline';
import parseLinkLabel from '../helpers/parse_link_label';
import parseLinkDestination from '../helpers/parse_link_destination';
import parseLinkTitle from '../helpers/parse_link_title';
import normalizeReference from '../helpers/normalize_reference';
function parseReference(str, parser, options, env) {
var state, labelEnd, pos, max, code, start, href, title, label;
if (str.charCodeAt(0) !== 0x5B/* [ */) { return -1; }
if (str.indexOf(']:') === -1) { return -1; }
state = new StateInline(str, parser, options, env, []);
labelEnd = parseLinkLabel(state, 0);
if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; }
max = state.posMax;
// [label]: destination 'title'
// ^^^ skip optional whitespace here
for (pos = labelEnd + 2; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (code !== 0x20 && code !== 0x0A) { break; }
}
// [label]: destination 'title'
// ^^^^^^^^^^^ parse this
if (!parseLinkDestination(state, pos)) { return -1; }
href = state.linkContent;
pos = state.pos;
// [label]: destination 'title'
// ^^^ skipping those spaces
start = pos;
for (pos = pos + 1; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (code !== 0x20 && code !== 0x0A) { break; }
}
// [label]: destination 'title'
// ^^^^^^^ parse this
if (pos < max && start !== pos && parseLinkTitle(state, pos)) {
title = state.linkContent;
pos = state.pos;
} else {
title = '';
pos = start;
}
// ensure that the end of the line is empty
while (pos < max && state.src.charCodeAt(pos) === 0x20/* space */) { pos++; }
if (pos < max && state.src.charCodeAt(pos) !== 0x0A) { return -1; }
label = normalizeReference(str.slice(1, labelEnd));
if (typeof env.references[label] === 'undefined') {
env.references[label] = { title: title, href: href };
}
return pos;
}
export default function references(state) {
var tokens = state.tokens, i, l, content, pos;
state.env.references = state.env.references || {};
if (state.inlineMode) {
return;
}
// Scan definitions in paragraph inlines
for (i = 1, l = tokens.length - 1; i < l; i++) {
if (tokens[i].type === 'inline' &&
tokens[i - 1].type === 'paragraph_open' &&
tokens[i + 1].type === 'paragraph_close') {
content = tokens[i].content;
while (content.length) {
pos = parseReference(content, state.inline, state.options, state.env);
if (pos < 0) { break; }
content = content.slice(pos).trim();
}
tokens[i].content = content;
if (!content.length) {
tokens[i - 1].tight = true;
tokens[i + 1].tight = true;
}
}
}
};