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, ``);
});
}
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('