-
Notifications
You must be signed in to change notification settings - Fork 31
/
wikilinks.ts
200 lines (163 loc) · 3.91 KB
/
wikilinks.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import MarkdownIt from "markdown-it";
import StateInline from "markdown-it/lib/rules_inline/state_inline";
export function wikilinks(state: StateInline): boolean {
const max = state.posMax;
if (
state.src.charCodeAt(state.pos) !== 0x5b ||
state.src.charCodeAt(state.pos + 1) !== 0x5b /* [ */
) {
return false;
}
const wikilinkStart = state.pos + 2;
const wikiLinkEnd = findLinkEnd(state, state.pos);
if (wikiLinkEnd < 0) {
return false;
}
const { hashFragment, headerStart, headerEnd } = findLinkToHeader(
state,
state.pos,
wikiLinkEnd
);
if (hashFragment) {
state.src = replaceBetween(
state.src,
headerStart,
headerEnd,
hashFragment
);
}
const { alias, aliasStart, aliasEnd } = findAlias(
state,
state.pos,
wikiLinkEnd
);
const pageNameStart = wikilinkStart;
const pageNameEnd = Math.min(
wikiLinkEnd,
headerStart > 0 ? headerStart : wikiLinkEnd,
aliasStart > 0 ? aliasStart - 1 : wikiLinkEnd
);
const linkToPage = state.src.slice(pageNameStart, pageNameEnd);
if (alias) {
state.pos = aliasStart;
state.posMax = aliasEnd;
} else {
state.pos = wikilinkStart;
state.posMax = wikiLinkEnd;
}
const href = `wikilinks:${linkToPage}${hashFragment ?? ""}`;
let token = state.push("link_open", "a", 1);
token.attrs = [["href", href]];
state.md.inline.tokenize(state);
token = state.push("link_close", "a", -1);
state.pos = wikiLinkEnd + 3;
state.posMax = max;
return true;
}
function replaceBetween(
original: string,
start: number,
end: number,
replacement: string
) {
return original.substring(0, start) + replacement + original.substring(end);
}
function findLinkEnd(state: StateInline, start: number) {
let labelEnd = -1;
let found = false;
const max = state.posMax,
oldPos = state.pos;
state.pos = start + 2;
while (state.pos < max) {
if (
state.src.charCodeAt(state.pos - 1) === 0x5d &&
state.src.charCodeAt(state.pos) === 0x5d
) {
found = true;
break;
}
state.md.inline.skipToken(state);
}
if (found) {
labelEnd = state.pos - 1;
}
// restore old state
state.pos = oldPos;
return labelEnd;
}
function findLinkToHeader(
state: StateInline,
start: number,
max: number
): { hashFragment?: string; headerStart: number; headerEnd: number } {
let headerStart = -1,
headerEnd = -1,
found = false,
foundStart = false,
hashFragment = undefined;
const oldPos = state.pos;
state.pos = start + 2;
while (state.pos <= max) {
if (state.src.charCodeAt(state.pos) === 0x23 /* # */) {
foundStart = true;
headerStart = state.pos;
}
if (
foundStart &&
(state.src.charCodeAt(state.pos) === 0x5d /* ] (Link End) */ ||
state.src.charCodeAt(state.pos) === 0x7c) /* | (Alias Start) */
) {
found = true;
break;
}
state.pos++;
}
if (found) {
headerEnd = state.pos;
hashFragment = state.src.slice(headerStart, headerEnd);
} else {
headerStart = -1;
headerEnd = -1;
}
// restore old state
state.pos = oldPos;
const cleanHashFragment = hashFragment?.replace(" ", "-");
return { hashFragment: cleanHashFragment, headerStart, headerEnd };
}
function findAlias(
state: StateInline,
start: number,
max: number
): { alias: string | undefined; aliasStart: number; aliasEnd: number } {
let aliasStart = -1,
aliasEnd = -1,
found = false,
foundStart = false,
alias = undefined;
const oldPos = state.pos;
state.pos = start + 2;
while (state.pos <= max) {
if (state.src.charCodeAt(state.pos) === 0x7c /* # */) {
foundStart = true;
aliasStart = state.pos + 1;
}
if (
foundStart &&
state.src.charCodeAt(state.pos + 1) === 0x5d /* ] (Link End) */
) {
found = true;
break;
}
state.pos++;
}
if (found) {
aliasEnd = state.pos + 1;
alias = state.src.slice(aliasStart, aliasEnd);
}
// restore old state
state.pos = oldPos;
return { alias, aliasStart, aliasEnd };
}
export default function wikilinksPlugin(md: MarkdownIt): void {
md.inline.ruler.push("wikilinks", wikilinks);
}