Skip to content

Commit fd7d4e0

Browse files
committed
feat: add disqus plugin, closed #123
1 parent 025bc5c commit fd7d4e0

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

build/build.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ var plugins = [
4848
{ name: 'external-script', entry: 'external-script.js' },
4949
{ name: 'front-matter', entry: 'front-matter/index.js' },
5050
{ name: 'zoom-image', entry: 'zoom-image.js' },
51-
{ name: 'codesponsor', entry: 'codesponsor.js' }
51+
{ name: 'codesponsor', entry: 'codesponsor.js' },
52+
{ name: 'disqus', entry: 'disqus.js' }
5253
]
5354

5455
plugins.forEach(item => {

docs/plugins.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,17 @@ window.$docsify = {
147147
```
148148

149149
See [here](https://github.com/jperasmus/docsify-copy-code/blob/master/README.md) for more details.
150+
151+
152+
## Disqus
153+
154+
Disqus comments. https://disqus.com/
155+
156+
```html
157+
<script>
158+
window.$docsify = {
159+
disqus: 'shortname'
160+
}
161+
</script>
162+
<script src="//unpkg.com/docsify/lib/plugins/disqus.min.js"></script>
163+
```

src/plugins/disqus.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
location.href = location.href.replace('/-/', '/#/')
2+
3+
function install (hook, vm) {
4+
const dom = Docsify.dom
5+
const disqus = vm.config.disqus
6+
if (!disqus) {
7+
throw Error('$docsify.disqus is required')
8+
}
9+
10+
hook.init(_ => {
11+
const script = dom.create('script')
12+
13+
script.async = true
14+
script.src = `https://${disqus}.disqus.com/embed.js`
15+
script.setAttribute('data-timestamp', +new Date())
16+
dom.appendTo(dom.body, script)
17+
})
18+
19+
hook.mounted(_ => {
20+
const div = dom.create('div')
21+
div.id = 'disqus_thread'
22+
const main = dom.getNode('#main')
23+
div.style = `width: ${main.clientWidth}px; margin: 0 auto 20px;`
24+
dom.appendTo(dom.find('.content'), div)
25+
26+
window.disqus_config = function () {
27+
this.page.url = location.origin + '/-' + vm.route.path
28+
this.page.identifier = vm.route.path
29+
this.page.title = document.title
30+
}
31+
})
32+
33+
hook.doneEach(_ => {
34+
if (typeof window.DISQUS !== 'undefined') {
35+
DISQUS.reset({
36+
reload: true,
37+
config: function () {
38+
this.page.url = location.origin + '/-' + vm.route.path
39+
this.page.identifier = vm.route.path
40+
this.page.title = document.title
41+
}
42+
})
43+
}
44+
})
45+
}
46+
47+
$docsify.plugins = [].concat(install, $docsify.plugins)

0 commit comments

Comments
 (0)