Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | 1x 1x 1x 1x 1x 1x 1x | // `pdf2svg` is required to be installed
// http://www.cityinthesky.co.uk/opensource/pdf2svg/
//
import { spawn } from "child_process";
import * as fs from "fs";
import * as path from "path";
import * as temp from "temp";
import computeChecksum from "./lib/compute-checksum";
let SVG_DIRECTORY_PATH: string = null;
export function toSVGMarkdown(
pdfFilePath: string,
{
svgDirectoryPath,
markdownDirectoryPath,
svgZoom,
svgWidth,
svgHeight,
}: {
markdownDirectoryPath: string;
svgDirectoryPath?: string;
svgZoom?: string;
svgWidth?: string;
svgHeight?: string;
},
): Promise<string> {
return new Promise<string>((resolve, reject) => {
if (!svgDirectoryPath) {
if (!SVG_DIRECTORY_PATH) {
SVG_DIRECTORY_PATH = temp.mkdirSync("mume_pdf");
}
svgDirectoryPath = SVG_DIRECTORY_PATH;
}
const svgFilePrefix = computeChecksum(pdfFilePath) + "_";
const task = 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 += `\n`;
}
}
});
return resolve(svgMarkdown);
});
}
});
});
}
|