Skip to content

Instantly share code, notes, and snippets.

@kuanyui
Last active April 19, 2016 15:24
Show Gist options
  • Save kuanyui/d1728c2a526a615de56c to your computer and use it in GitHub Desktop.
Save kuanyui/d1728c2a526a615de56c to your computer and use it in GitHub Desktop.
/*
footnote.js: A filter for Hexo to generate footnotes. Place this file in /scripts/footnote.js.
========================================================================
Author: kuanyui(azazabc123[at]g~~~m~~~a~~~i~~~l[dot]com)
Date: 20140518
License: WTFPL 1.0
========================================================================
The following string in article
{fn|I'm the a lot lot of content.}
Will be converted into
<sup><a id="fnr.1" class="footref" href="#fn.1" name="fnr.1">1</a></sup>
And the following block will be added in the bottom of the article.
<div id="footnotes">
<h2 class="footnotes">Footnote</h2>
<div id="text-footnotes">
<div class="footdef"><sup><a id="fn.1" name="fn.1" class="footnum" href="#fnr.1">1</a></sup> <p class="footpara">I'm the a lot lot of content.</p></div>
<div class="footdef"><sup><a id="fn.2" name="fn.2" class="footnum" href="#fnr.2">2</a></sup> <p class="footpara">I'm the second content2</p></div>
...
</div>
</div>
Adding following CSS into your site is recommended:
.footpara {
display: inline;
margin-left: 5px;
}
*/
var lang = {"en_US": "Footnotes", "zh_TW": "腳註", "ja_JP": "脚注", "zh_CN": "脚注"};
if (lang[hexo.config.language]) {
var fnName = lang[hexo.config.language];
} else {
var fnName = "Footnotes";
}
hexo.extend.filter.register('pre', function(data, callback) {
var num = 0;
var footContent = "";
var RE = new RegExp('\\{fn\\|(.+?)\\}', 'g');
data.content = data.content.replace(RE, function(x, y) {
num += 1;
footContent += '<div class="footdef"><sup><a id="fn.' + num + '" name="fn.' + num + '" class="footnum" href="#fnr.' + num + '">' + num + '</a></sup><p class="footpara">' + y + '</p></div>';
return '<sup><a id="fnr.' + num + '" class="footref" href="#fn.' + num + '" name="fnr.' + num + '">' + num + '</a></sup>';
});
if (footContent.length > 0) {
data.content = data.content + '<div id="footnotes"><h1 class="footnotes">' + fnName + '</h1><div id="text-footnotes">' + footContent + '</div></div>';
}
callback(null, data);
});
@fusion809
Copy link

This is failing (during attempted generation with hexo generate) on my 64-bit Arch Linux machine with Hexo 3.2.0, giving the error message:

INFO  Start processing
FATAL Something's wrong. Maybe you can find the solution here: http://hexo.io/docs/troubleshooting.html
TypeError: callback is not a function
    at Hexo.<anonymous> (/home/fusion809/GitHub/hexo-site/scripts/footnotes.js:56:2)
    at Hexo.tryCatcher (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/util.js:16:23)
    at Hexo.<anonymous> (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/method.js:15:34)
    at /home/fusion809/GitHub/hexo-site/node_modules/hexo/lib/extend/filter.js:68:35
    at tryCatcher (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/util.js:16:23)
    at Object.gotValue (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/reduce.js:145:18)
    at Object.gotAccum (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/reduce.js:134:25)
    at Object.tryCatcher (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/promise.js:502:31)
    at Promise._settlePromise (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/promise.js:559:18)
    at Promise._settlePromise0 (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/promise.js:604:10)
    at Promise._settlePromises (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/promise.js:683:18)
    at Async._drainQueue (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/async.js:138:16)
    at Async._drainQueues (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/async.js:148:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/async.js:17:14)
    at tryOnImmediate (timers.js:534:15)
    at processImmediate [as _immediateCallback] (timers.js:514:5)
FATAL callback is not a function
TypeError: callback is not a function
    at Hexo.<anonymous> (/home/fusion809/GitHub/hexo-site/scripts/footnotes.js:56:2)
    at Hexo.tryCatcher (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/util.js:16:23)
    at Hexo.<anonymous> (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/method.js:15:34)
    at /home/fusion809/GitHub/hexo-site/node_modules/hexo/lib/extend/filter.js:68:35
    at tryCatcher (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/util.js:16:23)
    at Object.gotValue (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/reduce.js:145:18)
    at Object.gotAccum (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/reduce.js:134:25)
    at Object.tryCatcher (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/promise.js:502:31)
    at Promise._settlePromise (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/promise.js:559:18)
    at Promise._settlePromise0 (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/promise.js:604:10)
    at Promise._settlePromises (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/promise.js:683:18)
    at Async._drainQueue (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/async.js:138:16)
    at Async._drainQueues (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/async.js:148:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/home/fusion809/GitHub/hexo-site/node_modules/hexo/node_modules/bluebird/js/release/async.js:17:14)
    at tryOnImmediate (timers.js:534:15)
    at processImmediate [as _immediateCallback] (timers.js:514:5)

where, as you may guess based on the error message I have footnotes.js placed in my sources folder (per the Hexo documentation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment