forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisqus.js
51 lines (44 loc) · 1.38 KB
/
disqus.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
const fixedPath = location.href.replace('/-/', '/#/');
if (fixedPath !== location.href) {
location.href = fixedPath;
}
function install(hook, vm) {
const dom = Docsify.dom;
const disqus = vm.config.disqus;
if (!disqus) {
throw Error('$docsify.disqus is required');
}
hook.init(_ => {
const script = dom.create('script');
script.async = true;
script.src = `https://${disqus}.disqus.com/embed.js`;
script.setAttribute('data-timestamp', Number(new Date()));
dom.appendTo(dom.body, script);
});
hook.mounted(_ => {
const div = dom.create('div');
div.id = 'disqus_thread';
const main = dom.getNode('#main');
div.style = `width: ${main.clientWidth}px; margin: 0 auto 20px;`;
dom.appendTo(dom.find('.content'), div);
window.disqus_config = function () {
this.page.url = location.origin + '/-' + vm.route.path;
this.page.identifier = vm.route.path;
this.page.title = document.title;
};
});
hook.doneEach(_ => {
if (typeof window.DISQUS !== 'undefined') {
window.DISQUS.reset({
reload: true,
config() {
this.page.url = location.origin + '/-' + vm.route.path;
this.page.identifier = vm.route.path;
this.page.title = document.title;
},
});
}
});
}
window.$docsify = window.$docsify || {};
$docsify.plugins = [install, ...($docsify.plugins || [])];