// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import * as TextFormatting from './text_formatting.jsx'; import * as SyntaxHighlighting from './syntax_highlighting.jsx'; import marked from 'marked'; import katex from 'katex'; import ScrollStore from 'stores/scroll_store.jsx'; function markdownImageLoaded(image) { if (image.hasAttribute('height') && image.attributes.height.value !== 'auto') { const maxHeight = parseInt(global.getComputedStyle(image).maxHeight, 10); if (image.attributes.height.value > maxHeight) { image.style.height = maxHeight + 'px'; image.style.width = ((maxHeight * image.attributes.width.value) / image.attributes.height.value) + 'px'; } else { image.style.height = image.attributes.height.value + 'px'; } } else { image.style.height = 'auto'; } ScrollStore.emitPostScroll(); } global.markdownImageLoaded = markdownImageLoaded; 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 '
' +
searchedContent +
content +
'
' +
'' +
output +
'
' +
''
);
}
br() {
if (this.formattingOptions.singleline) {
return ' ';
}
return super.br();
}
image(href, title, text) {
let src = href;
let dimensions = [];
const parts = href.split(' ');
if (parts.length > 1) {
const lastPart = parts.pop();
src = parts.join(' ');
if (lastPart[0] === '=') {
dimensions = lastPart.substr(1).split('x');
if (dimensions.length === 2 && dimensions[1] === '') {
dimensions[1] = 'auto';
}
}
}
let out = ' 0) {
out += ' width="' + dimensions[0] + '"';
}
if (dimensions.length > 1) {
out += ' height="' + dimensions[1] + '"';
}
out += ' onload="window.markdownImageLoaded(this)" onerror="window.markdownImageLoaded(this)" class="markdown-inline-img"';
out += this.options.xhtml ? '/>' : '>';
return out;
}
heading(text, level) {
return `${text}
`; } return super.paragraph(text); } table(header, body) { return `