All files / src create.ts

83.33% Statements 50/60
84.53% Branches 82/97
100% Functions 5/5
83.05% Lines 49/59

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 126 127 128 129 130 131                                                        15x 15x         204x 6x 6x               4670x 11x         4670x 15x 30x   4670x 9x 9x 2x   7x 7x       4670x     4670x 40x 40x 40x 40x     4670x 26x 26x   4670x 25x         15x 11x 11x 2x     11x 2x     11x 7x     11x 7x     11x 7x 7x 6x   7x 1x   4x                 11x 11x 11x         11x     15x  
import markdown, { Options } from '@wcj/markdown-to-html';
import { getCodeString } from 'rehype-rewrite';
import rehypeDocument from 'rehype-document';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import rehypeSlug from 'rehype-slug';
import remarkGemoji from 'remark-gemoji';
// @ts-ignore
import rehypeUrls from 'rehype-urls';
import rehypeFormat from 'rehype-format';
import { githubCorners, githubCornersStyle } from './nodes/github-corners.js';
import { githubCornersFork, githubCornersForkStyle } from './nodes/github-corners-fork.js';
import { octiconLink } from './nodes/octiconLink.js';
import { imgBase64 as toBase64 } from './nodes/imgBase64.js';
import { markdownStyle, mdStyle } from './nodes/markdown-style.js';
import { copyElement, copyScript } from './nodes/copy.js';
import { darkMode } from './nodes/dark-mode.js';
import { MDToHTMLOptions } from './index.js';
 
// https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-when-using-the-experimental-modules-flag
// export const _dirname = dirname(fileURLToPath(import.meta.url));
// const filename = fileURLToPath(import.meta.url);
// const dirname = path.dirname(filename);
 
export interface CreateOptions extends MDToHTMLOptions {
  sourcePath?: string;
}
 
export function create(options: MDToHTMLOptions = {}) {
  const { markdown: string, document, 'img-base64': imgBase64 = false, corners = true, rewrite, reurls = {}, 'markdown-style-theme': markdownStyleTheme, 'dark-mode': darkModeTheme = true, 'markdown-style': wrapperStyle } = options;
  const mdOptions: Options = {
    hastNode: false,
    remarkPlugins: [remarkGemoji],
    rehypePlugins: [
      [rehypeUrls, (url: any) => {
        if (reurls[url.href]) {
          url.path = reurls[url.href];
          return url.path;
        }
      }],
      [rehypeSlug],
      [rehypeAutolinkHeadings],
      [rehypeFormat],
    ],
    rewrite: (node, index, parent) => {
      if (node.type === 'element' && node.tagName === 'html') {
        Iif (markdownStyleTheme) {
          node.properties = node.properties || {};
          node.properties['data-color-mode'] = markdownStyleTheme;
        }
      }
      if ((node.type === 'element' && node.tagName === 'body') || (!document && node.type === 'root')) {
        node.children = markdownStyle(node.children as any, markdownStyleTheme, wrapperStyle);
        darkMode(darkModeTheme, markdownStyleTheme).forEach(item => node.children.unshift(item));
      }
      if (corners && options['github-corners'] && ((document && node.type == 'element' && node.tagName === 'body') || (!document && node.type === 'root'))) {
        node.children = Array.isArray(node.children) ? node.children : [];
        if (options['github-corners-fork']) {
          node.children.unshift(githubCornersFork({ href: options['github-corners'] }));
        } else {
          githubCorners({ href: options['github-corners'] }).forEach(item => {
            node.children.unshift(item)
          });
        }
      }
      Iif (node.type == 'element' && node.tagName === 'img' && imgBase64) {
        node.properties = { ...node.properties, src: toBase64(node.properties.src as string, options.sourcePath) };
      }
      if (node.type == 'element' && /h(1|2|3|4|5|6)/.test(node.tagName) && node.children && Array.isArray(node.children) && node.children.length > 0) {
        const child = node.children[0];
        Eif (child && child.type === 'element' && child.properties) {
          child.properties = { className: 'anchor', ...child.properties };
          child.children = [octiconLink()];
        }
      }
      if (node.type == 'element' && node.tagName === 'pre') {
        const code = getCodeString(node.children);
        node.children.push(copyElement(code));
      }
      if (rewrite && typeof rewrite === 'function') {
        rewrite(node, index, parent);
      }
    }
  }
  
  if (document) {
    const documentOptions = { ...document };
    if (document.js) {
      documentOptions.js = Array.isArray(document.js) ? document.js : [document.js];
    }
 
    if (document.script) {
      documentOptions.script = Array.isArray(document.script) ? document.script : [document.script];
    }
 
    if (document.link) {
      documentOptions.link = Array.isArray(document.link) ? document.link : [document.link];
    }
 
    if (document.style) {
      documentOptions.style = Array.isArray(document.style) ? document.style : [document.style];
    }
 
    if (Array.isArray(documentOptions.style)) {
      documentOptions.style.push(mdStyle);
      if (options['github-corners']) {
        documentOptions.style.push(githubCornersStyle);
      }
      if (options['github-corners-fork']) {
        documentOptions.style.push(githubCornersForkStyle);
      }
    } else Iif (typeof documentOptions.style === 'string') {
      documentOptions.style = [documentOptions.style, mdStyle];
      if (options['github-corners']) {
        documentOptions.style.push(githubCornersStyle);
      }
      if (options['github-corners-fork']) {
        documentOptions.style.push(githubCornersForkStyle);
      }
    }
    documentOptions.script = documentOptions.script || [];
    if (Array.isArray(documentOptions.script)) {
      documentOptions.script.push(copyScript);
    } else Eif (typeof documentOptions.script === 'string') {
      documentOptions.script = [documentOptions.script, copyScript];
    }
 
    mdOptions.rehypePlugins.unshift([rehypeDocument, documentOptions]);
  }
 
  return markdown(string || '', mdOptions) as string;
}