diff --git a/build/build.js b/build/build.js index 7cffe6aa7..4057d27c3 100644 --- a/build/build.js +++ b/build/build.js @@ -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 => { diff --git a/docs/plugins.md b/docs/plugins.md index 72d0274ac..fdca5bb35 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -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 + + +``` diff --git a/src/plugins/disqus.js b/src/plugins/disqus.js new file mode 100644 index 000000000..0ebda57a4 --- /dev/null +++ b/src/plugins/disqus.js @@ -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)