This commit is contained in:
18218461270@163.com 2025-08-20 19:36:38 +08:00
commit 541d5f104c
9 changed files with 284 additions and 0 deletions

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2025 FISHER_
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

19
README.md Normal file
View File

@ -0,0 +1,19 @@
# hexo-renderer-pandoc-plus
```yaml
pandoc:
pandocPath: 'pandoc'
timeout: 5000
mode: 'markdown' # Should be 'markdown' or 'markdown_strict'
extensions: [] # eg. '+auto_identifiers', '-native_spans'
katex: # https://katex.org/docs/options.html
enable: true
output: 'htmlAndMathml'
throwOnError: false
strict: 'warn'
cdn: 'jsdelivr' # The CDN provider for loading katex.min.css
```
<https://github.com/hexojs/hexo-renderer-pandoc/>

53
index.js Normal file
View File

@ -0,0 +1,53 @@
'use strict';
/* global hexo */
const pandocRenderer = require('./lib/renderer');
if (!("pandoc" in hexo.config)) {
hexo.config.pandoc = new Object();
}
hexo.config.pandoc.katex = Object.assign(
{
enable: true,
output: 'htmlAndMathml',
throwOnError: false,
strict: 'warn',
cdn: 'jsdelivr'
},
hexo.config.pandoc.katex
);
hexo.config.pandoc = Object.assign(
{
pandocPath: 'pandoc',
timeout: 5000,
mode: 'markdown',
extensions: []
},
hexo.config.pandoc
);
hexo.extend.renderer.register('md', 'html', pandocRenderer, true);
hexo.extend.renderer.register('markdown', 'html', pandocRenderer, true);
hexo.extend.renderer.register('mkd', 'html', pandocRenderer, true);
hexo.extend.renderer.register('mkdn', 'html', pandocRenderer, true);
hexo.extend.renderer.register('mdwn', 'html', pandocRenderer, true);
hexo.extend.renderer.register('mdtxt', 'html', pandocRenderer, true);
hexo.extend.renderer.register('mdtext', 'html', pandocRenderer, true);
if (hexo.config.pandoc.katex.enable) {
const { version } = require('katex');
const urls = {
cdnjs: `https://cdnjs.cloudflare.com/ajax/libs/katex/${ version }/katex.min.css`,
loli: `https://cdnjs.loli.net/ajax/libs/KaTeX/${ version }/katex.min.css`,
jsdelivr: `https://cdn.jsdelivr.net/npm/katex@${ version }/dist/katex.min.css`,
unpkg: `https://unpkg.com/katex@${ version }/dist/katex.min.css`
};
const cdn = hexo.config.pandoc.katex.cdn;
const url = (cdn in urls) ? urls[cdn] : cdn.replace(/\${\s*version\s*}/gi, version);
hexo.extend.injector.register('head_end', `<link rel="stylesheet" href="${ url }"/>`);
}

5
lib/filter.js Normal file
View File

@ -0,0 +1,5 @@
const pandoc = require('pandoc-filter');
module.exports = function(data, action, format) {
return pandoc.walkSync(data, action, format, data.meta || data[0].unMeta);
};

36
lib/katex.js Normal file
View File

@ -0,0 +1,36 @@
'use strict';
const katex = require('katex');
const pandoc = require('pandoc-filter');
const filterSync = require('./filter.js');
const printMessage = require('./message.js');
function katexProcess(data, config, filePath) {
function action({ t: type, c: value }, fotmat, meta) {
if (type !== 'Math') {
return null;
}
let options = config;
if (config['strict'] === 'warn') {
options['strict'] = (errorCode, errorMsg, token) => {
printMessage('WARNING', filePath, errorMsg);
};
}
if (value[0]['t'] === 'InlineMath') {
options['displayMode'] = false;
} else {
options['displayMode'] = true;
}
try {
return pandoc.RawInline('html', katex.renderToString(value[1], options));
} catch (err) {
printMessage('ERROR', filePath, err.toString());
throw err;
}
}
return JSON.stringify(filterSync(JSON.parse(data), action, ''));
}
module.exports = katexProcess;

14
lib/message.js Normal file
View File

@ -0,0 +1,14 @@
'use strict';
module.exports = function(type, filePath, details) {
const msg
= '[' + type + '][hexo-renderer-pandoc-katex] On ' + filePath
+ '\n'
+ '[' + type + '][hexo-renderer-pandoc-katex] ' + details;
if (type === 'WARNING') {
console.warn(msg);
} else {
console.error(msg);
}
}

56
lib/renderer.js Normal file
View File

@ -0,0 +1,56 @@
'use strict';
const { spawnSync } = require('node:child_process');
const katexProcess = require('./katex.js');
const printMessage = require('./message.js');
function pandocProcess(pandocPath, args, timeout, filePath, input) {
const result = spawnSync(pandocPath, args, {
cwd: process.cwd(),
env: process.env,
input: input,
encoding: 'utf8',
timeout: timeout
});
if (result.status === 0) {
if (result.stderr) {
printMessage('WARNING', filePath, result.stderr);
}
return result.stdout;
}
if (result.error) {
printMessage('ERROR', filePath, result.error);
throw result.error;
}
printMessage('ERROR', filePath, 'pandoc exited with code ' + result.status + (result.stderr ? ': ' + result.stderr : '.'));
throw Error(error_msg);
}
function pandocRenderer(data, options) {
const hexo = this;
const { pandoc: config } = hexo.config;
let from = config.mode;
for (const extension in config.extensions) {
from += extension;
}
let ast = pandocProcess(config.pandocPath,
['-f', from, '-t', 'json'],
config.timeout,
data.path,
data.text.toString());
if (config.katex.enable) {
ast = katexProcess(ast, config.katex, data.path);
}
return pandocProcess(config.pandocPath,
['-f', 'json', '-t', 'html'],
config.timeout,
data.path,
ast);
}
module.exports = pandocRenderer;

62
package-lock.json generated Normal file
View File

@ -0,0 +1,62 @@
{
"name": "hexo-renderer-pandoc-katex",
"version": "0.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hexo-renderer-pandoc-katex",
"version": "0.0.0",
"dependencies": {
"katex": "^0.16.22",
"pandoc-filter": "^2.2.0"
}
},
"node_modules/commander": {
"version": "8.3.0",
"resolved": "https://registry.npmmirror.com/commander/-/commander-8.3.0.tgz",
"integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==",
"license": "MIT",
"engines": {
"node": ">= 12"
}
},
"node_modules/get-stdin": {
"version": "7.0.0",
"resolved": "https://registry.npmmirror.com/get-stdin/-/get-stdin-7.0.0.tgz",
"integrity": "sha512-zRKcywvrXlXsA0v0i9Io4KDRaAw7+a1ZpjRwl9Wox8PFlVCCHra7E9c4kqXCoCM9nR5tBkaTTZRBoCm60bFqTQ==",
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/katex": {
"version": "0.16.22",
"resolved": "https://registry.npmmirror.com/katex/-/katex-0.16.22.tgz",
"integrity": "sha512-XCHRdUw4lf3SKBaJe4EvgqIuWwkPSo9XoeO8GjQW94Bp7TWv9hNhzZjZ+OH9yf1UmLygb7DIT5GSFQiyt16zYg==",
"funding": [
"https://opencollective.com/katex",
"https://github.com/sponsors/katex"
],
"license": "MIT",
"dependencies": {
"commander": "^8.3.0"
},
"bin": {
"katex": "cli.js"
}
},
"node_modules/pandoc-filter": {
"version": "2.2.0",
"resolved": "https://registry.npmmirror.com/pandoc-filter/-/pandoc-filter-2.2.0.tgz",
"integrity": "sha512-mtjZfaiyONjN8HigU1XCoGDcadMmBwYtjAL2ol0vaY4qiOWmtCaVGJIa1gglKk7VSwYgAEfuE+5nvj/G3jLRBg==",
"license": "MIT",
"dependencies": {
"get-stdin": "~7.0.0"
},
"engines": {
"node": ">=7.6.0"
}
}
}
}

30
package.json Normal file
View File

@ -0,0 +1,30 @@
{
"name": "hexo-renderer-pandoc-katex",
"version": "0.0.0",
"description": "A renderer for hexo using pandoc with katex support.",
"homepage": "https://git.gzezfisher.top/FISHER_/hexo-renderer-pandoc-katex",
"bugs": {
"url": "https://git.gzezfisher.top/FISHER_/hexo-renderer-pandoc-katex/issues"
},
"repository": {
"type": "git",
"url": "https://git.gzezfisher.top/FISHER_/hexo-renderer-pandoc-katex.git"
},
"main": "index.js",
"files": [
"lib"
],
"contributers": [],
"tags": [
"hexo",
"plugin",
"pandoc",
"renderer",
"math",
"katex"
],
"dependencies": {
"katex": "^0.16.22",
"pandoc-filter": "^2.2.0"
}
}