2015-09-19 10:33:13 -04:00
|
|
|
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
|
|
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2015-09-22 14:30:50 -04:00
|
|
|
const TextFormatting = require('./text_formatting.jsx');
|
|
|
|
|
|
2015-09-19 10:33:13 -04:00
|
|
|
const marked = require('marked');
|
|
|
|
|
|
|
|
|
|
export class MattermostMarkdownRenderer extends marked.Renderer {
|
2015-09-22 14:30:50 -04:00
|
|
|
constructor(options, formattingOptions = {}) {
|
|
|
|
|
super(options);
|
|
|
|
|
|
2015-09-22 15:23:39 -04:00
|
|
|
this.heading = this.heading.bind(this);
|
2015-09-22 14:30:50 -04:00
|
|
|
this.text = this.text.bind(this);
|
|
|
|
|
|
|
|
|
|
this.formattingOptions = formattingOptions;
|
|
|
|
|
}
|
2015-09-22 15:23:39 -04:00
|
|
|
|
2015-09-23 17:42:57 -04:00
|
|
|
br() {
|
|
|
|
|
if (this.formattingOptions.singleline) {
|
|
|
|
|
return ' ';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return super.br();
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-22 15:23:39 -04:00
|
|
|
heading(text, level, raw) {
|
|
|
|
|
const id = `${this.options.headerPrefix}${raw.toLowerCase().replace(/[^\w]+/g, '-')}`;
|
|
|
|
|
return `<h${level} id="${id}" class="markdown__heading">${text}</h${level}>`;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-19 10:33:13 -04:00
|
|
|
link(href, title, text) {
|
2015-09-19 11:08:39 -04:00
|
|
|
let outHref = href;
|
|
|
|
|
|
|
|
|
|
if (outHref.lastIndexOf('http', 0) !== 0) {
|
|
|
|
|
outHref = `http://${outHref}`;
|
2015-09-19 10:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2015-09-22 15:23:39 -04:00
|
|
|
let output = '<a class="theme markdown__link" href="' + outHref + '"';
|
2015-09-21 11:41:40 -04:00
|
|
|
if (title) {
|
|
|
|
|
output += ' title="' + title + '"';
|
|
|
|
|
}
|
|
|
|
|
output += '>' + text + '</a>';
|
|
|
|
|
|
|
|
|
|
return output;
|
2015-09-19 10:33:13 -04:00
|
|
|
}
|
2015-09-22 14:30:50 -04:00
|
|
|
|
2015-09-23 17:42:57 -04:00
|
|
|
paragraph(text) {
|
|
|
|
|
if (this.formattingOptions.singleline) {
|
|
|
|
|
return `<p class="markdown__paragraph-inline">${text}</p>`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return super.paragraph(text);
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-22 15:23:39 -04:00
|
|
|
table(header, body) {
|
|
|
|
|
return `<table class="markdown__table"><thead>${header}</thead><tbody>${body}</tbody></table>`;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-22 14:30:50 -04:00
|
|
|
text(text) {
|
|
|
|
|
return TextFormatting.doFormatText(text, this.formattingOptions);
|
|
|
|
|
}
|
2015-09-19 10:33:13 -04:00
|
|
|
}
|