56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
'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; |