forked from newcat/baklavajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiPlugin.js
34 lines (27 loc) · 1.07 KB
/
apiPlugin.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
const regex = /!!API%([^%]+)%/g;
const apiPlugin = function(hook, vm) {
hook.beforeEach(function(content) {
let m;
const toTransform = [];
while ((m = regex.exec(content)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
const data = JSON.parse(m[1]);
let url = `/baklavajs/api/index.html#${data.module.replace("/", "%2F")}/`;
if (data.type === "class") {
url += `classes/${data.name}`;
if (data.field) {
url += "#" + data.field;
}
}
toTransform.unshift({ index: m.index, length: m[0].length, value: url });
}
toTransform.forEach((t) => {
content = content.substring(0, t.index) + t.value + content.substring(t.index + t.length);
});
return content;
});
}