All files / app/utils stripMarkdown.ts

78.57% Statements 11/14
66.66% Branches 6/9
100% Functions 1/1
78.57% Lines 11/14

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                  10x                           70x     70x   70x 70x 70x         70x   70x 70x                   70x                                                                   70x    
//From https://github.com/stiang/remove-markdown
 
interface StripMarkdownOptions {
  stripListLeaders?: boolean; // strip list leaders (default: true)
  listUnicodeChar?: string; // char to insert instead of stripped list leaders (default: '')
  gfm?: boolean; // support GitHub-Flavored Markdown (default: true)
  useImgAltText?: boolean; // replace images with alt-text, if present (default: true)
}
 
export default function stripMarkdown(
  md: string,
  {
    listUnicodeChar,
    stripListLeaders,
    gfm,
    useImgAltText,
  }: StripMarkdownOptions = {
    gfm: true,
    listUnicodeChar: "",
    stripListLeaders: true,
    useImgAltText: true,
  }
) {
  let output = md || "";
 
  // Remove horizontal rules (stripListHeaders conflict with this rule, which is why it has been moved to the top)
  output = output.replace(/^(-\s*?|\*\s*?|_\s*?){3,}\s*$/gm, "");
 
  try {
    if (stripListLeaders) {
      Iif (listUnicodeChar)
        output = output.replace(
          /^([\s\t]*)([*\-+]|\d+\.)\s+/gm,
          listUnicodeChar + " $1"
        );
      else output = output.replace(/^([\s\t]*)([*\-+]|\d+\.)\s+/gm, "$1");
    }
    if (gfm) {
      output = output
        // Header
        .replace(/\n={2,}/g, "\n")
        // Fenced codeblocks
        .replace(/~{3}.*\n/g, "")
        // Strikethrough
        .replace(/~~/g, "")
        // Fenced codeblocks
        .replace(/`{3}.*\n/g, "");
    }
    output = output
      // Remove HTML tags
      .replace(/<[^>]*>/g, "")
      // Remove setext-style headers
      .replace(/^[=-]{2,}\s*$/g, "")
      // Remove footnotes?
      .replace(/\[\^.+?\](: .*?$)?/g, "")
      .replace(/\s{0,2}\[.*?\]: .*?$/g, "")
      // Remove images
      .replace(/!\[(.*?)\][[(].*?[\])]/g, useImgAltText ? "$1" : "")
      // Remove inline links
      .replace(/\[(.*?)\][[(].*?[\])]/g, "$1")
      // Remove blockquotes
      .replace(/^\s{0,3}>\s?/g, "")
      // Remove reference-style links?
      .replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, "")
      // Remove atx-style headers
      .replace(
        /^(\n)?\s{0,}#{1,6}\s+| {0,}(\n)?\s{0,}#{0,} {0,}(\n)?\s{0,}$/gm,
        "$1$2$3"
      )
      // Remove emphasis (repeat the line to remove double emphasis)
      .replace(/([*_]{1,3})(\S.*?\S{0,1})\1/g, "$2")
      .replace(/([*_]{1,3})(\S.*?\S{0,1})\1/g, "$2")
      // Remove code blocks
      .replace(/(`{3,})(.*?)\1/gm, "$2")
      // Remove inline code
      .replace(/`(.+?)`/g, "$1")
      // Replace two or more newlines with exactly two? Not entirely sure this belongs here...
      .replace(/\n{2,}/g, "\n\n");
  } catch (e) {
    console.error(e);
    return md;
  }
  return output;
}