'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.toString()); throw result.error; } const error_msg = 'pandoc exited with code ' + result.status + (result.stderr ? ': ' + result.stderr : '.'); printMessage('ERROR', filePath, error_msg); 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;