commit 541d5f104c668b424fea39c21b5e49fe5287b6ec Author: 18218461270@163.com <18218461270@163.com> Date: Wed Aug 20 19:36:38 2025 +0800 init diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..860c8df --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..bbc35c0 --- /dev/null +++ b/README.md @@ -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 + + +``` + + \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..9648c62 --- /dev/null +++ b/index.js @@ -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', ``); +} \ No newline at end of file diff --git a/lib/filter.js b/lib/filter.js new file mode 100644 index 0000000..7e2ab9f --- /dev/null +++ b/lib/filter.js @@ -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); +}; \ No newline at end of file diff --git a/lib/katex.js b/lib/katex.js new file mode 100644 index 0000000..35c1a03 --- /dev/null +++ b/lib/katex.js @@ -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; \ No newline at end of file diff --git a/lib/message.js b/lib/message.js new file mode 100644 index 0000000..d6e57a0 --- /dev/null +++ b/lib/message.js @@ -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); + } +} \ No newline at end of file diff --git a/lib/renderer.js b/lib/renderer.js new file mode 100644 index 0000000..768c8bb --- /dev/null +++ b/lib/renderer.js @@ -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; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a9c58b6 --- /dev/null +++ b/package-lock.json @@ -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" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..d7e0ae2 --- /dev/null +++ b/package.json @@ -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" + } +}