mume-with-litvis
Version:
Fork of mume with added http://litvis.org/
78 lines • 2.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toSVGMarkdown = void 0;
const child_process_1 = require("child_process");
const fs = require("fs");
const path = require("path");
const PDF = require("./pdf");
function cleanUpFiles(texFilePath) {
const directoryPath = path.dirname(texFilePath);
const extensionName = path.extname(texFilePath);
const filePrefix = path
.basename(texFilePath)
.replace(new RegExp(extensionName + "$"), "");
fs.readdir(directoryPath, (error, items) => {
if (error) {
return;
}
items.forEach((fileName) => {
if (fileName.startsWith(filePrefix) && !fileName.match(/\.(la)?tex/)) {
fs.unlink(path.resolve(directoryPath, fileName), () => {
return;
});
}
});
});
}
function toSVGMarkdown(texFilePath, { latexEngine = "pdflatex", svgDirectoryPath, markdownDirectoryPath, svgZoom, svgWidth, svgHeight, }) {
return new Promise((resolve, reject) => {
const task = (0, child_process_1.spawn)(latexEngine, [`"${texFilePath}"`], {
cwd: path.dirname(texFilePath),
shell: true,
});
const chunks = [];
task.stdout.on("data", (chunk) => {
chunks.push(chunk);
});
const errorChunks = [];
task.stderr.on("data", (chunk) => {
errorChunks.push(chunk);
});
task.on("error", (error) => {
errorChunks.push(Buffer.from(error.toString(), "utf-8"));
});
task.on("close", () => {
if (errorChunks.length) {
cleanUpFiles(texFilePath);
return reject(Buffer.concat(errorChunks).toString());
}
else {
const output = Buffer.concat(chunks).toString();
if (output.indexOf("LaTeX Error") >= 0) {
// meet error
cleanUpFiles(texFilePath);
return reject(output);
}
const pdfFilePath = texFilePath.replace(/\.(la)?tex$/, ".pdf");
PDF.toSVGMarkdown(pdfFilePath, {
svgDirectoryPath,
markdownDirectoryPath,
svgZoom,
svgWidth,
svgHeight,
})
.then((svgMarkdown) => {
cleanUpFiles(texFilePath);
return resolve(svgMarkdown);
})
.catch((error) => {
cleanUpFiles(texFilePath);
return reject(error);
});
}
});
task.stdin.end();
});
}
exports.toSVGMarkdown = toSVGMarkdown;
//# sourceMappingURL=latex.js.map