-
Notifications
You must be signed in to change notification settings - Fork 1
/
docsify-waline.js
55 lines (51 loc) · 1.69 KB
/
docsify-waline.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
$docsify.plugins = [].concat(function (hook, vm) {
let options = vm.config.waline || {};
options.el = "#waline";
if (!options.serverURL) {
console.warn("sorry, waline.serverURL must be required.");
return;
}
let language = vm.config.count.language || "english";
let suffix;
switch (language) {
case "english":
suffix = "></span> times";
break;
case "chinese":
suffix = "></span> 次阅读";
break;
default:
console.warn("unsupported language", language);
suffix = "></span> times";
}
let w = false;
let flag = false;
// 每次路由切换时数据全部加载完成后调用,没有参数。
hook.doneEach((_) => {
if (!flag) { // 没有挂载上 div
return;
}
if (w) {
w.destroy(); // waline 存在时重新加载
}
path = window.location.pathname + window.location.hash;
options.path = path.replace(/\?.*$/, "");
// console.log(options.path);
// 浏览量统计
let pageview = document.querySelector(`#main div>span`);
if (pageview) {
pageview.innerHTML += ` | <span class="waline-pageview-count" data-path="${options.path}"${suffix}`;
}
w = Waline.init(options);
Waline.pageviewCount({ serverURL: options.serverURL, path: options.path });
});
// 初始化并第一次加载完成数据后调用,只触发一次,没有参数。
hook.mounted((_) => {
let waline = document.createElement("div");
waline.id = "waline";
waline.style = "max-width: var(--content-max-width); margin: 0 auto 20px;";
let content = document.querySelector("section.content");
content.appendChild(waline);
flag = true;
});
}, $docsify.plugins);