2020-01-30 23:05:23 +02:00
|
|
|
const MdToHtml = require('./MdToHtml');
|
|
|
|
const HtmlToHtml = require('./HtmlToHtml');
|
2020-07-15 00:27:12 +02:00
|
|
|
const htmlUtils = require('lib/htmlUtils');
|
|
|
|
const MarkdownIt = require('markdown-it');
|
2020-01-30 23:05:23 +02:00
|
|
|
|
|
|
|
class MarkupToHtml {
|
|
|
|
constructor(options) {
|
|
|
|
this.options_ = Object.assign({}, {
|
|
|
|
ResourceModel: {
|
|
|
|
isResourceUrl: () => false,
|
|
|
|
},
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
this.renderers_ = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer(markupLanguage) {
|
|
|
|
if (this.renderers_[markupLanguage]) return this.renderers_[markupLanguage];
|
|
|
|
|
|
|
|
let RendererClass = null;
|
|
|
|
|
|
|
|
if (markupLanguage === MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN) {
|
|
|
|
RendererClass = MdToHtml;
|
|
|
|
} else if (markupLanguage === MarkupToHtml.MARKUP_LANGUAGE_HTML) {
|
|
|
|
RendererClass = HtmlToHtml;
|
|
|
|
} else {
|
|
|
|
throw new Error(`Invalid markup language: ${markupLanguage}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.renderers_[markupLanguage] = new RendererClass(this.options_);
|
|
|
|
return this.renderers_[markupLanguage];
|
|
|
|
}
|
|
|
|
|
|
|
|
injectedJavaScript() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:27:12 +02:00
|
|
|
stripMarkup(markupLanguage, markup, options = null) {
|
|
|
|
if (!markup) return '';
|
|
|
|
|
|
|
|
options = Object.assign({}, {
|
|
|
|
collapseWhiteSpaces: false,
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
let output = markup;
|
|
|
|
|
|
|
|
if (markupLanguage === MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN) {
|
|
|
|
if (!this.rawMarkdownIt_) {
|
|
|
|
// We enable HTML because we don't want it to be escaped, so
|
|
|
|
// that it can be stripped off in the stripHtml call below.
|
|
|
|
this.rawMarkdownIt_ = new MarkdownIt({ html: true });
|
|
|
|
}
|
|
|
|
output = this.rawMarkdownIt_.render(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
output = htmlUtils.stripHtml(output).trim();
|
|
|
|
|
|
|
|
if (options.collapseWhiteSpaces) {
|
|
|
|
output = output.replace(/\n+/g, ' ');
|
|
|
|
output = output.replace(/\s+/g, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2020-10-16 17:26:19 +02:00
|
|
|
clearCache(markupLanguage) {
|
|
|
|
const r = this.renderer(markupLanguage);
|
|
|
|
if (r.clearCache) r.clearCache();
|
|
|
|
}
|
|
|
|
|
2020-02-14 01:59:23 +02:00
|
|
|
async render(markupLanguage, markup, theme, options) {
|
2020-01-30 23:05:23 +02:00
|
|
|
return this.renderer(markupLanguage).render(markup, theme, options);
|
|
|
|
}
|
2020-03-23 02:47:25 +02:00
|
|
|
|
|
|
|
async allAssets(markupLanguage, theme) {
|
|
|
|
return this.renderer(markupLanguage).allAssets(theme);
|
|
|
|
}
|
2020-01-30 23:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN = 1;
|
|
|
|
MarkupToHtml.MARKUP_LANGUAGE_HTML = 2;
|
|
|
|
|
|
|
|
module.exports = MarkupToHtml;
|