markdown.ts 2.4 KB
Newer Older
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
1 2 3 4
import marked from 'marked';
import highlight from 'highlight.js';

export function parseGithubFlavoredMarkdown(value: string): string {
5 6 7
  const renderer = new marked.Renderer();
  const options = {
    renderer: renderer,
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
8 9 10
    gfm: true,
    tables: true,
    breaks: true,
11 12 13 14
    pedantic: true,
    sanitize: false,
    smartLists: false,
    smartypants: false,
15
    mathDelimiters: [['$', '$'], ['\\(', '\\)'], ['\\[', '\\]'], ['$$', '$$'], 'beginend'],
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
16 17 18
    highlight: function (code) {
      return highlight.highlightAuto(code).value;
    }
19 20
  };
  marked.setOptions(options);
21 22 23 24
  return postMarkdownRenderer(marked(preMarkdownRenderer(value)));
}

function preMarkdownRenderer(value: string): string {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
25 26 27 28 29 30 31
  const emojiMatch = value.match(/:([a-z0-9_\+\-]+):/g);
  if (emojiMatch) {
    emojiMatch.forEach(token => {
      const emoji = token.replace(/:/g, '');
      value = value.replace(token, `![emoji](/assets/icons/emojis/${emoji}.png)`);
    });
  }
32
  return value;
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
33 34
}

35
function postMarkdownRenderer(value: string): string {
Christopher Mark Fullarton's avatar
Christopher Mark Fullarton committed
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
  const iframeOptions = `frameborder="0" gesture="media" webkitallowfullscreen mozallowfullscreen allowfullscreen`;

  const youtubeMatch = value.match(/<a href=".*(youtube|youtu).*">.*<\/a>/g);
  if (youtubeMatch) {
    youtubeMatch.forEach(token => {
      const originalToken = token;
      if (token.indexOf('embed') === -1) {
        // Convert to embed uri. Direct youtube urls are restricted by the sameorigin policy and cannot be embedded in iframes
        token = token.replace('watch?v=', 'embed/');
      }
      const videoTag = token.replace('<a', `<iframe`)
                            .replace('</a>', `</iframe>`)
                            .replace('<iframe', `<iframe ${iframeOptions}`)
                            .replace('href', 'src');
      value = value.replace(originalToken, videoTag);
    });
  }

  const vimeoMatch = value.match(/<a href=".*(vimeo).*">.*<\/a>/g);
  if (vimeoMatch) {
    vimeoMatch.forEach(token => {
      const id = token.match(/([0-9]+)/);
      if (id) {
        const videoTag = `<iframe src="https://player.vimeo.com/video/${id[0]}?title=0&byline=0&portrait=0" ${iframeOptions}></iframe>`;
        value = value.replace(token, videoTag);
      }
    });
  }

  const linkMatch = value.match(/<a href=".*">/g);
  if (linkMatch) {
    linkMatch.forEach(token => {
      value = value.replace(token, token.replace('<a ', '<a target=\'_blank\' '));
    });
  }

  return value;
}