Skip to content

Commit

Permalink
feat: add disqus plugin, closed #123
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Nov 4, 2017
1 parent 025bc5c commit fd7d4e0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ var plugins = [
{ name: 'external-script', entry: 'external-script.js' },
{ name: 'front-matter', entry: 'front-matter/index.js' },
{ name: 'zoom-image', entry: 'zoom-image.js' },
{ name: 'codesponsor', entry: 'codesponsor.js' }
{ name: 'codesponsor', entry: 'codesponsor.js' },
{ name: 'disqus', entry: 'disqus.js' }
]

plugins.forEach(item => {
Expand Down
14 changes: 14 additions & 0 deletions docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ window.$docsify = {
```

See [here](https://github.com/jperasmus/docsify-copy-code/blob/master/README.md) for more details.


## Disqus

Disqus comments. https://disqus.com/

```html
<script>
window.$docsify = {
disqus: 'shortname'
}
</script>
<script src="//unpkg.com/docsify/lib/plugins/disqus.min.js"></script>
```
47 changes: 47 additions & 0 deletions src/plugins/disqus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
location.href = location.href.replace('/-/', '/#/')

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', +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') {
DISQUS.reset({
reload: true,
config: function () {
this.page.url = location.origin + '/-' + vm.route.path
this.page.identifier = vm.route.path
this.page.title = document.title
}
})
}
})
}

$docsify.plugins = [].concat(install, $docsify.plugins)

0 comments on commit fd7d4e0

Please sign in to comment.