import marked from 'marked'; import highlight from 'highlight.js'; export function parseGithubFlavoredMarkdown(value: string): string { const renderer = new marked.Renderer(); const options = { renderer: renderer, gfm: true, tables: true, breaks: true, pedantic: true, sanitize: false, smartLists: false, smartypants: false, mathDelimiters: [['$', '$'], ['\\(', '\\)'], ['\\[', '\\]'], ['$$', '$$'], 'beginend'], highlight: function (code) { return highlight.highlightAuto(code).value; } }; marked.setOptions(options); return postMarkdownRenderer(marked(preMarkdownRenderer(value))); } function preMarkdownRenderer(value: string): string { 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)`); }); } return value; } function postMarkdownRenderer(value: string): string { const iframeOptions = `frameborder="0" gesture="media" webkitallowfullscreen mozallowfullscreen allowfullscreen`; const youtubeMatch = value.match(/.*<\/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('', ``) .replace('.*<\/a>/g); if (vimeoMatch) { vimeoMatch.forEach(token => { const id = token.match(/([0-9]+)/); if (id) { const videoTag = ``; value = value.replace(token, videoTag); } }); } const linkMatch = value.match(//g); if (linkMatch) { linkMatch.forEach(token => { value = value.replace(token, token.replace('