UNPKG

mume-with-litvis

Version:

Fork of mume with added http://litvis.org/

87 lines (86 loc) 3.92 kB
"use strict"; // `pdf2svg` is required to be installed // http://www.cityinthesky.co.uk/opensource/pdf2svg/ // 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 temp = require("temp"); const compute_checksum_1 = require("./lib/compute-checksum"); let SVG_DIRECTORY_PATH = null; function toSVGMarkdown(pdfFilePath, { svgDirectoryPath, markdownDirectoryPath, svgZoom, svgWidth, svgHeight, }) { return new Promise((resolve, reject) => { if (!svgDirectoryPath) { if (!SVG_DIRECTORY_PATH) { SVG_DIRECTORY_PATH = temp.mkdirSync("mume_pdf"); } svgDirectoryPath = SVG_DIRECTORY_PATH; } const svgFilePrefix = (0, compute_checksum_1.default)(pdfFilePath) + "_"; const task = (0, child_process_1.spawn)("pdf2svg", [ `"${pdfFilePath}"`, `"${path.resolve(svgDirectoryPath, svgFilePrefix + "%d.svg")}"`, "all", ], { 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) { return reject(Buffer.concat(errorChunks).toString()); } else { fs.readdir(svgDirectoryPath, (error, items) => { if (error) { return reject(error.toString()); } items = items.sort((a, b) => { const offsetA = parseInt(a.match(/\_(\d+)\.svg$/)[1], 10); const offsetB = parseInt(b.match(/\_(\d+)\.svg$/)[1], 10); return offsetA - offsetB; }); let svgMarkdown = ""; const r = Math.random(); items.forEach((fileName) => { const match = fileName.match(new RegExp(`^${svgFilePrefix}(\\d+)\.svg`)); if (match) { let svgFilePath = path.relative(markdownDirectoryPath, path.resolve(svgDirectoryPath, fileName)); // nvm, the converted result looks so ugly /* const pngFilePath = svgFilePath.replace(/\.svg$/, '.png') // convert svg to png gm(svgFilePath) .noProfile() .write(pngFilePath, function(error) { console.log(error, pngFilePath) }) */ svgFilePath = svgFilePath .replace(/\.\.\\/g, "../") .replace(/\\/g, "/"); /* Windows file path issue. "..\..\blabla" doesn't work */ if (svgZoom || svgWidth || svgHeight) { svgMarkdown += `<img src=\"${svgFilePath}\" ${svgWidth ? `width="${svgWidth}"` : ""} ${svgHeight ? `height="${svgHeight}"` : ""} ${svgZoom ? `style="zoom:${svgZoom};"` : ""}>`; } else { svgMarkdown += `![](${svgFilePath}?${r})\n`; } } }); return resolve(svgMarkdown); }); } }); }); } exports.toSVGMarkdown = toSVGMarkdown; //# sourceMappingURL=pdf.js.map