2025-08-20 19:36:38 +08:00

36 lines
962 B
JavaScript

'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;