All files / src latex.ts

12.5% Statements 5/40
0% Branches 0/11
0% Functions 0/12
12.5% Lines 5/40

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 1021x 1x 1x   1x                                               1x                                                                                                                                                  
import { spawn } from "child_process";
import * as fs from "fs";
import * as path from "path";
 
import * as PDF from "./pdf";
 
function cleanUpFiles(texFilePath: string) {
  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;
        });
      }
    });
  });
}
 
export function toSVGMarkdown(
  texFilePath: string,
  {
    latexEngine = "pdflatex",
    svgDirectoryPath,
    markdownDirectoryPath,
    svgZoom,
    svgWidth,
    svgHeight,
  }: {
    latexEngine: string;
    svgDirectoryPath?: string;
    markdownDirectoryPath: string;
    svgZoom?: string;
    svgWidth?: string;
    svgHeight?: string;
  },
): Promise<string> {
  return new Promise<string>((resolve, reject) => {
    const task = 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();
  });
}