Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
ikeq committed Jun 24, 2019
1 parent 72a4df0 commit fa1b7a3
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
package-lock.json
.DS_Store
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test/
tmp/
.travis.yml
.github/
.DS_Store
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# hexo-filter-mathjax
Pre-render MathJax

Pre-render MathJax into svg using [mathjax-node].

## Installation

``` bash
$ npm install hexo-filter-mathjax --save
```

## Options

You can configure this plugin in `_config.yml`.

``` yaml
mathjax:
inlineQuote: ['\$', '\$']
blockQuote: ['\$\$', '\$\$']
MathJax:
SVG:
font: Gyre-Pagella
```
- **inlineQuote** - Similar to `MathJax.inlineMath`, but only support one pair.
- **blockQuote** - Similar to `MathJax.blockMath`, but only support one pair.
- **MathJax** - MathJax configuration, see [svg processor][svg-processor].

## License

Licensed under [MIT](LICENSE).

[mathjax-node]: https://github.com/mathjax/MathJax-node
[svg-processor]: http://docs.mathjax.org/en/latest/options/output-processors/SVG.html#the-svg-output-processor
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* global hexo */

'use strict';

const mathjax = require('./lib/mathjax');

mathjax.config(hexo.config.mathjax);

hexo.extend.filter.register('before_post_render', mathjax.beforeRender);
hexo.extend.filter.register('after_post_render', mathjax.render);
90 changes: 90 additions & 0 deletions lib/mathjax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const mathJax = require("mathjax-node");
const crypto = require('crypto');
const getMarker = getUid('MATHJAX');
const mathList = [];

let inline_mathjax_regex, block_mathjax_regex;

// markdown is easier to handle
exports.beforeRender = function (data) {
data.content = data.content
.replace(block_mathjax_regex, function (a, math) {
const marker = getMarker();
mathList.push({ math, block: true, marker });
return marker;
})
.replace(inline_mathjax_regex, function (a, math) {
const marker = getMarker();
mathList.push({ math, marker });
return marker;
});

return data;
}

exports.render = function (data) {
if (!mathList.length) return data;

let { content } = data;

return render();

function render() {
const { math, marker, block } = mathList.shift();
return mathJax.typeset({ math, format: 'TeX', svg: true, })
.then(res => {
if (!res.errors) {
if (block)
content = content.replace(new RegExp('<p>\\s*' + marker), '<p style="text-align:center">' + res.svg);
else
content = content.replace(marker, res.svg);

if (mathList.length) return render();

data.content = content;
return data;
}

throw res.errors;
});
}
}

exports.config = function (config) {
config = config || {};

const inlineQuote = parseTag(config.inlineQuote, '\\$');
const blockQuote = parseTag(config.blockQuote, '\\$\\$');

inline_mathjax_regex = new RegExp(`${inlineQuote[0]}(.+?)${inlineQuote[1]}`, 'g');
block_mathjax_regex = new RegExp(`${blockQuote[0]}\\s*(.+?)\\s*${blockQuote[1]}`, 'g');

// TODO:
delete config.inlineQuote;
delete config.blockQuote;
mathJax.config(config);
}

function getUid(ns) {
const hash = crypto.createHash('md5').update(String(Date.now())).digest('hex').substring(0, 6).toUpperCase();
let i = 0;
return () => [ns, hash, i++].join('-');
}

function parseTag(input, defaultTag) {
if (typeof input === 'string') {
const tag = input || defaultTag;
input = [tag, tag];
}

else if (!Array.isArray(input)) {
input = [defaultTag, defaultTag];
}

else {
const tag = input[0] || input[1] || defaultTag;
input = [tag, input[1] || tag];
}

return input;
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "hexo-filter-mathjax",
"version": "0.1.0",
"description": "Pre-render MathJax",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/ike-c/hexo-filter-mathjax.git"
},
"keywords": [
"hexo",
"mathjax",
"prerender",
"mathjax-node",
"hexo-filter"
],
"author": "ike-c <9206414+ike-c@users.noreply.github.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/ike-c/hexo-filter-mathjax/issues"
},
"homepage": "https://github.com/ike-c/hexo-filter-mathjax#readme",
"dependencies": {
"mathjax-node": "^2.1.1"
}
}

0 comments on commit fa1b7a3

Please sign in to comment.