2020-10-21 01:23:55 +02:00
|
|
|
const MdToHtml = require('./MdToHtml').default;
|
2020-01-30 23:05:23 +02:00
|
|
|
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
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
export enum MarkupLanguage {
|
|
|
|
Markdown = 1,
|
|
|
|
Html = 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class MarkupToHtml {
|
|
|
|
|
|
|
|
static MARKUP_LANGUAGE_MARKDOWN:number = MarkupLanguage.Markdown;
|
|
|
|
static MARKUP_LANGUAGE_HTML:number = MarkupLanguage.Html;
|
|
|
|
|
|
|
|
private renderers_:any = {};
|
|
|
|
private options_:any;
|
|
|
|
private rawMarkdownIt_:any;
|
|
|
|
|
|
|
|
constructor(options:any) {
|
2020-01-30 23:05:23 +02:00
|
|
|
this.options_ = Object.assign({}, {
|
|
|
|
ResourceModel: {
|
|
|
|
isResourceUrl: () => false,
|
|
|
|
},
|
|
|
|
}, options);
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
renderer(markupLanguage:MarkupLanguage) {
|
2020-01-30 23:05:23 +02:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
stripMarkup(markupLanguage:MarkupLanguage, markup:string, options:any = null) {
|
2020-07-15 00:27:12 +02:00
|
|
|
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-21 01:23:55 +02:00
|
|
|
clearCache(markupLanguage:MarkupLanguage) {
|
2020-10-16 17:26:19 +02:00
|
|
|
const r = this.renderer(markupLanguage);
|
|
|
|
if (r.clearCache) r.clearCache();
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
async render(markupLanguage:MarkupLanguage, markup:string, theme:any, options:any) {
|
2020-01-30 23:05:23 +02:00
|
|
|
return this.renderer(markupLanguage).render(markup, theme, options);
|
|
|
|
}
|
2020-03-23 02:47:25 +02:00
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
async allAssets(markupLanguage:MarkupLanguage, theme:any) {
|
2020-03-23 02:47:25 +02:00
|
|
|
return this.renderer(markupLanguage).allAssets(theme);
|
|
|
|
}
|
2020-01-30 23:05:23 +02:00
|
|
|
}
|