-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
package-lock.json | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
test/ | ||
tmp/ | ||
.travis.yml | ||
.github/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |