// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import highlightJs from 'highlight.js/lib/highlight.js';
import highlightJsDiff from 'highlight.js/lib/languages/diff.js';
import highlightJsApache from 'highlight.js/lib/languages/apache.js';
import highlightJsMakefile from 'highlight.js/lib/languages/makefile.js';
import highlightJsHttp from 'highlight.js/lib/languages/http.js';
import highlightJsJson from 'highlight.js/lib/languages/json.js';
import highlightJsMarkdown from 'highlight.js/lib/languages/markdown.js';
import highlightJsJavascript from 'highlight.js/lib/languages/javascript.js';
import highlightJsCss from 'highlight.js/lib/languages/css.js';
import highlightJsNginx from 'highlight.js/lib/languages/nginx.js';
import highlightJsObjectivec from 'highlight.js/lib/languages/objectivec.js';
import highlightJsPython from 'highlight.js/lib/languages/python.js';
import highlightJsXml from 'highlight.js/lib/languages/xml.js';
import highlightJsPerl from 'highlight.js/lib/languages/perl.js';
import highlightJsBash from 'highlight.js/lib/languages/bash.js';
import highlightJsPhp from 'highlight.js/lib/languages/php.js';
import highlightJsCoffeescript from 'highlight.js/lib/languages/coffeescript.js';
import highlightJsCs from 'highlight.js/lib/languages/cs.js';
import highlightJsCpp from 'highlight.js/lib/languages/cpp.js';
import highlightJsSql from 'highlight.js/lib/languages/sql.js';
import highlightJsGo from 'highlight.js/lib/languages/go.js';
import highlightJsRuby from 'highlight.js/lib/languages/ruby.js';
import highlightJsJava from 'highlight.js/lib/languages/java.js';
import highlightJsIni from 'highlight.js/lib/languages/ini.js';
highlightJs.registerLanguage('diff', highlightJsDiff);
highlightJs.registerLanguage('apache', highlightJsApache);
highlightJs.registerLanguage('makefile', highlightJsMakefile);
highlightJs.registerLanguage('http', highlightJsHttp);
highlightJs.registerLanguage('json', highlightJsJson);
highlightJs.registerLanguage('markdown', highlightJsMarkdown);
highlightJs.registerLanguage('javascript', highlightJsJavascript);
highlightJs.registerLanguage('css', highlightJsCss);
highlightJs.registerLanguage('nginx', highlightJsNginx);
highlightJs.registerLanguage('objectivec', highlightJsObjectivec);
highlightJs.registerLanguage('python', highlightJsPython);
highlightJs.registerLanguage('xml', highlightJsXml);
highlightJs.registerLanguage('perl', highlightJsPerl);
highlightJs.registerLanguage('bash', highlightJsBash);
highlightJs.registerLanguage('php', highlightJsPhp);
highlightJs.registerLanguage('coffeescript', highlightJsCoffeescript);
highlightJs.registerLanguage('cs', highlightJsCs);
highlightJs.registerLanguage('cpp', highlightJsCpp);
highlightJs.registerLanguage('sql', highlightJsSql);
highlightJs.registerLanguage('go', highlightJsGo);
highlightJs.registerLanguage('ruby', highlightJsRuby);
highlightJs.registerLanguage('java', highlightJsJava);
highlightJs.registerLanguage('ini', highlightJsIni);
import * as TextFormatting from './text_formatting.jsx';
import * as Utils from './utils.jsx';
import marked from 'marked';
import Constants from '../utils/constants.jsx';
const HighlightedLanguages = Constants.HighlightedLanguages;
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 doesn't break up words in snake_case,
// allows for links starting with www, and allows links succounded 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, escaped) {
let usedLanguage = language || '';
usedLanguage = usedLanguage.toLowerCase();
// treat html as xml to prevent injection attacks
if (usedLanguage === 'html') {
usedLanguage = 'xml';
}
if (HighlightedLanguages[usedLanguage]) {
const parsed = highlightJs.highlight(usedLanguage, code);
return (
'