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';
|
2021-08-14 13:19:53 +02:00
|
|
|
import { Options as NoteStyleOptions } from './noteStyle';
|
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,
|
2022-02-24 21:35:28 +02:00
|
|
|
Any = 3,
|
2020-10-21 01:23:55 +02:00
|
|
|
}
|
|
|
|
|
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[];
|
2021-08-12 18:23:49 +02:00
|
|
|
cssStrings: string[];
|
2021-01-29 20:45:11 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 20:23:33 +02:00
|
|
|
export interface OptionsResourceModel {
|
|
|
|
isResourceUrl: (url: string)=> boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Options {
|
|
|
|
isSafeMode?: boolean;
|
2021-05-19 15:00:16 +02:00
|
|
|
ResourceModel?: OptionsResourceModel;
|
|
|
|
customCss?: string;
|
|
|
|
extraRendererRules?: any[];
|
|
|
|
resourceBaseUrl?: string;
|
2021-04-24 20:23:33 +02:00
|
|
|
}
|
|
|
|
|
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 = {};
|
2021-04-24 20:23:33 +02:00
|
|
|
private options_: Options;
|
2020-11-12 21:13:28 +02:00
|
|
|
private rawMarkdownIt_: any;
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2021-08-14 13:19:53 +02:00
|
|
|
public constructor(options: Options = null) {
|
2021-04-24 20:23:33 +02:00
|
|
|
this.options_ = {
|
2020-01-30 23:05:23 +02:00
|
|
|
ResourceModel: {
|
|
|
|
isResourceUrl: () => false,
|
|
|
|
},
|
2021-04-24 20:23:33 +02:00
|
|
|
isSafeMode: false,
|
|
|
|
...options,
|
|
|
|
};
|
2020-01-30 23:05:23 +02:00
|
|
|
}
|
|
|
|
|
2021-04-24 20:23:33 +02:00
|
|
|
private 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}`);
|
|
|
|
}
|
|
|
|
|
2021-05-19 15:00:16 +02:00
|
|
|
this.renderers_[markupLanguage] = new RendererClass(this.options_ as any);
|
2020-01-30 23:05:23 +02:00
|
|
|
return this.renderers_[markupLanguage];
|
|
|
|
}
|
|
|
|
|
2021-04-24 20:23:33 +02:00
|
|
|
public 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;
|
|
|
|
}
|
|
|
|
|
2021-04-24 20:23:33 +02:00
|
|
|
public clearCache(markupLanguage: MarkupLanguage) {
|
2020-10-16 17:26:19 +02:00
|
|
|
const r = this.renderer(markupLanguage);
|
|
|
|
if (r.clearCache) r.clearCache();
|
|
|
|
}
|
|
|
|
|
2021-04-24 20:23:33 +02:00
|
|
|
public async render(markupLanguage: MarkupLanguage, markup: string, theme: any, options: any): Promise<RenderResult> {
|
|
|
|
if (this.options_.isSafeMode) {
|
|
|
|
return {
|
|
|
|
html: `<pre>${markup}</pre>`,
|
|
|
|
cssStrings: [],
|
|
|
|
pluginAssets: [],
|
|
|
|
};
|
|
|
|
}
|
2020-01-30 23:05:23 +02:00
|
|
|
return this.renderer(markupLanguage).render(markup, theme, options);
|
|
|
|
}
|
2020-03-23 02:47:25 +02:00
|
|
|
|
2021-08-14 13:19:53 +02:00
|
|
|
public async allAssets(markupLanguage: MarkupLanguage, theme: any, noteStyleOptions: NoteStyleOptions = null) {
|
|
|
|
return this.renderer(markupLanguage).allAssets(theme, noteStyleOptions);
|
2020-03-23 02:47:25 +02:00
|
|
|
}
|
2020-01-30 23:05:23 +02:00
|
|
|
}
|