All files / src/render-enhancers resolved-image-paths.ts

52.38% Statements 11/21
36.36% Branches 4/11
66.67% Functions 2/3
52.38% Lines 11/21

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              1x             1x 1x 1x 1x     1x 1x     1x                                                 1x     1x   1x                      
import { MarkdownEngineRenderOption } from "../markdown-engine";
 
/**
 * This function resolves image paths
 * @param $ cheerio object that we will analyze
 * @return cheerio object
 */
export default async function enhance(
  $,
  options: MarkdownEngineRenderOption,
  resolveFilePath: (path: string, useRelativeFilePath: boolean) => string,
  usePandocParser,
): Promise<void> {
  // resolve image paths
  $("img, a").each((i, imgElement) => {
    let srcTag = "src";
    Eif (imgElement.name === "a") {
      srcTag = "href";
    }
 
    const img = $(imgElement);
    const src = img.attr(srcTag);
 
    // insert anchor for scroll sync.
    Iif (
      options.isForPreview &&
      imgElement.name !== "a" &&
      img
        .parent()
        .prev()
        .hasClass("sync-line")
    ) {
      const lineNo = parseInt(
        img
          .parent()
          .prev()
          .attr("data-line"),
        10,
      );
      if (lineNo) {
        img
          .parent()
          .after(
            `<p data-line="${lineNo +
              1}" class="sync-line" style="margin:0;"></p>`,
          );
      }
    }
 
    img.attr(srcTag, resolveFilePath(src, options.useRelativeFilePath));
  });
 
  Eif (!usePandocParser) {
    // check .mume-header in order to add id and class to headers.
    $(".mume-header").each((i, e) => {
      const classes = e.attribs.class;
      const id = e.attribs.id;
      const $e = $(e);
      const $h = $e.prev();
      $h.addClass(classes);
      $h.attr("id", encodeURIComponent(id)); // encodeURIComponent to fix utf-8 header.
      $e.remove();
    });
  }
}