// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import * as TextFormatting from './text_formatting.jsx';
import * as Utils from './utils.jsx';
import * as syntaxHightlighting from './syntax_hightlighting.jsx';
import marked from 'marked';
import katex from 'katex';
import 'katex/dist/katex.min.css';
function markdownImageLoaded(image) {
image.style.height = 'auto';
}
window.markdownImageLoaded = markdownImageLoaded;
class MattermostInlineLexer extends marked.InlineLexer {
constructor(links, options) {
super(links, options);
this.rules = Object.assign({}, this.rules);
// modified version of the regex that allows for links starting with www and those surrounded by parentheses
// the original is /^[\s\S]+?(?=[\\ starting with www.
// the original is /^<([^ >]+(@|:\/)[^ >]+)>/
this.rules.autolink = /^<((?:[^ >]+(@|:\/)|www\.)[^ >]+)>/;
}
}
class MattermostParser extends marked.Parser {
parse(src) {
this.inline = new MattermostInlineLexer(src.links, this.options, this.renderer);
this.tokens = src.reverse();
var out = '';
while (this.next()) {
out += this.tok();
}
return out;
}
}
class MattermostMarkdownRenderer extends marked.Renderer {
constructor(options, formattingOptions = {}) {
super(options);
this.heading = this.heading.bind(this);
this.paragraph = this.paragraph.bind(this);
this.text = this.text.bind(this);
this.formattingOptions = formattingOptions;
}
code(code, language) {
let usedLanguage = language || '';
usedLanguage = usedLanguage.toLowerCase();
if (usedLanguage === 'tex' || usedLanguage === 'latex') {
try {
const html = katex.renderToString(code, {throwOnError: false, displayMode: true});
return '
' + html + '
';
} catch (e) {
// fall through if latex parsing fails and handle below
}
}
// treat html as xml to prevent injection attacks
if (usedLanguage === 'html') {
usedLanguage = 'xml';
}
return syntaxHightlighting.formatCode(usedLanguage, code);
}
codespan(text) {
return '' + super.codespan(text) + '';
}
br() {
if (this.formattingOptions.singleline) {
return ' ';
}
return super.br();
}
image(href, title, text) {
let out = '' : '>';
return out;
}
heading(text, level, raw) {
const id = `${this.options.headerPrefix}${raw.toLowerCase().replace(/[^\w]+/g, '-')}`;
return `${text}`;
}
link(href, title, text) {
let outHref = href;
let outText = text;
let prefix = '';
let suffix = '';
// some links like https://en.wikipedia.org/wiki/Rendering_(computer_graphics) contain brackets
// and we try our best to differentiate those from ones just wrapped in brackets when autolinking
if (outHref.startsWith('(') && outHref.endsWith(')') && text === outHref) {
prefix = '(';
suffix = ')';
outText = text.substring(1, text.length - 1);
outHref = outHref.substring(1, outHref.length - 1);
}
try {
const unescaped = decodeURIComponent(unescape(href)).replace(/[^\w:]/g, '').toLowerCase();
if (unescaped.indexOf('javascript:') === 0 || unescaped.indexOf('vbscript:') === 0) { // eslint-disable-line no-script-url
return '';
}
} catch (e) {
return '';
}
if (!(/[a-z+.-]+:/i).test(outHref)) {
outHref = `http://${outHref}`;
}
let output = '';
} else {
output += ' target="_blank">';
}
output += outText + '';
return prefix + output + suffix;
}
paragraph(text) {
if (this.formattingOptions.singleline) {
return `