Skip to content

Commit

Permalink
Add Disqus Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Aug 29, 2016
1 parent 7005b71 commit 5fe2432
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
16 changes: 16 additions & 0 deletions data/layout.pug
@@ -1,3 +1,16 @@
mixin disqus(shortname)
#disqus_thread
script.
var disqus_shortname = '#{shortname}';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
noscript
| Please enable JavaScript to view the
a(href="http://disqus.com/?ref_noscript") comments powered by Disqus.

//- Menu:
//- renders the menu item recursively
Expand Down Expand Up @@ -104,4 +117,7 @@ html
+header-nav
.markdown-body
!= contents
- if (meta.disqus)
+disqus(meta.disqus)

+footer-nav(prev, next)
30 changes: 22 additions & 8 deletions index.js
Expand Up @@ -6,12 +6,14 @@ const pug = require('pug')
const join = require('path').join
const assign = Object.assign

const hash = require('./lib/hash')
const buildJs = require('./lib/build_js')
const buildCss = require('./lib/build_css')
const hash = require('./lib/hash')
const eachCons = require('./lib/helpers/each_cons')
const toArray = require('./lib/helpers/to_array')

const memoize = require('./lib/memoize')
const noop = require('./lib/helpers/noop')
const toArray = require('./lib/helpers/to_array')
const eachCons = require('./lib/helpers/each_cons')
const useCache = require('./lib/helpers/use_cache')

/**
Expand Down Expand Up @@ -65,7 +67,7 @@ function sortJs (files, ms, done) {
const list = toArray(ms.metadata().js)
const add = addAsset.bind(this, this.scripts, 'js', files)

list.forEach((item) => { add(item) })
list.forEach((item) => add(item))
done()
}

Expand Down Expand Up @@ -96,15 +98,15 @@ function addAsset (list, what, files, item) {
function addCss (files, ms, done) {
const callback = (err, contents) => {
if (err) return done(err)
files['assets/style.css'] = { contents }
files['assets/style.css'] = {contents}
this.styles.unshift('assets/style.css?t=' + hash(contents))
done()
}

const cacheable = (this.stylusImports.length === 0)

;(cacheable && useCache('cache/style.css', callback)) ||
buildCss({ imports: this.stylusImports }, callback)
buildCss({ imports: this.stylusImports }, callback)
}

/**
Expand All @@ -114,7 +116,7 @@ function addCss (files, ms, done) {
function addJs (files, ms, done) {
const callback = (err, contents) => {
if (err) return done(err)
files['assets/script.js'] = { contents }
files['assets/script.js'] = {contents}
this.scripts.push('assets/script.js?t=' +
hash(files['assets/script.js'].contents))

Expand All @@ -135,7 +137,7 @@ function addJs (files, ms, done) {
}

useCache('cache/script.js', callback) ||
buildJs({}, callback)
buildJs({}, callback)
}

/**
Expand All @@ -160,11 +162,21 @@ function addJs (files, ms, done) {
* * `docs`
*/

function injectDisqus (disqus) {
if (!disqus) return noop
var exclude = new RegExp(disqus.exclude || 'index')
return function addDisqus (fname, meta) {
if (exclude.test(fname)) return
meta.disqus = disqus.shortname
}
}

function relayout (files, ms, done) {
const toc = files['_docpress.json'].toc
const index = files['_docpress.json'].index
const meta = ms.metadata()

const addDisqus = injectDisqus(meta.disqus)
const pugData = fs.readFileSync(join(__dirname, 'data/layout.pug'), 'utf-8')
const layout = memoize(['pug', pugData], () => {
return pug.compile(pugData, { pretty: true })
Expand All @@ -184,6 +196,8 @@ function relayout (files, ms, done) {
active: fname
}

addDisqus(fname, meta)

const key = [ pugData, locals, file ]

file.contents = memoize(['pugdata', key], () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/helpers/noop.js
@@ -0,0 +1,4 @@
function noop () {
}

module.exports = noop

0 comments on commit 5fe2432

Please sign in to comment.