All files / src create.ts

93.61% Statements 44/47
92.94% Branches 79/85
100% Functions 5/5
93.47% Lines 43/46

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                                                        16x 16x         72x 6x 6x               4515x 11x         4515x 16x 32x 16x     4515x 9x 9x 2x   7x 14x       4515x     4515x 40x 40x 40x 40x     4515x 16x 16x   4515x 28x 28x   4515x 31x         16x 11x 11x 2x     11x 2x     11x 7x     11x 7x     11x     16x  
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 } from './nodes/github-corners.js';
import { githubCornersFork } from './nodes/github-corners-fork.js';
import { octiconLink } from './nodes/octiconLink.js';
import { imgBase64 as toBase64 } from './nodes/imgBase64.js';
import { markdownStyle } from './nodes/markdown-style.js';
import { copyElement, copyStyle, 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 (darkModeTheme) {
        }
      }
      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 === 'markdown-style') {
        node.children.push(copyStyle());
        node.children.push(copyScript());
      }
      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];
    }
 
    mdOptions.rehypePlugins.unshift([rehypeDocument, documentOptions]);
  }
 
  return markdown(string || '', mdOptions) as string;
}