2020-11-16 13:03:44 +02:00
|
|
|
import MdToHtml from './MdToHtml';
|
2020-11-20 18:04:47 +02:00
|
|
|
import HtmlToHtml from './HtmlToHtml';
|
|
|
|
import htmlUtils from './htmlUtils';
|
2020-07-15 00:27:12 +02:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
export interface RenderResultPluginAsset {
|
|
|
|
name: string;
|
|
|
|
mime: string;
|
2021-02-05 23:27:19 +02:00
|
|
|
path: string;
|
|
|
|
|
|
|
|
// For built-in Mardown-it plugins, the asset path is relative (and can be
|
|
|
|
// found inside the @joplin/renderer package), while for external plugins
|
|
|
|
// (content scripts), the path is absolute. We use this property to tell if
|
|
|
|
// it's relative or absolute, as that will inform how it's loaded in various
|
|
|
|
// places.
|
|
|
|
pathIsAbsolute: boolean;
|
2021-01-29 20:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface RenderResult {
|
|
|
|
html: string;
|
|
|
|
pluginAssets: RenderResultPluginAsset[];
|
|
|
|
cssStrings: string[];
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
export default class MarkupToHtml {
|
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
static MARKUP_LANGUAGE_MARKDOWN: number = MarkupLanguage.Markdown;
|
|
|
|
static MARKUP_LANGUAGE_HTML: number = MarkupLanguage.Html;
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
private renderers_: any = {};
|
|
|
|
private options_: any;
|
|
|
|
private rawMarkdownIt_: any;
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
constructor(options: any) {
|
2020-01-30 23:05:23 +02:00
|
|
|
this.options_ = Object.assign({}, {
|
|
|
|
ResourceModel: {
|
|
|
|
isResourceUrl: () => false,
|
|
|
|
},
|
|
|
|
}, options);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:13:28 +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-11-12 21:13:28 +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-11-12 21:13:28 +02:00
|
|
|
clearCache(markupLanguage: MarkupLanguage) {
|
2020-10-16 17:26:19 +02:00
|
|
|
const r = this.renderer(markupLanguage);
|
|
|
|
if (r.clearCache) r.clearCache();
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
async render(markupLanguage: MarkupLanguage, markup: string, theme: any, options: any): Promise<RenderResult> {
|
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-11-12 21:13:28 +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
|
|
|
}
|