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';
|
2023-07-19 14:09:03 +02:00
|
|
|
import { AllHtmlEntities } from 'html-entities';
|
2024-01-18 13:20:10 +02:00
|
|
|
import { FsDriver, MarkupRenderer, MarkupToHtmlConverter, OptionsResourceModel, RenderResult } from './types';
|
|
|
|
import defaultResourceModel from './defaultResourceModel';
|
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-04-24 20:23:33 +02:00
|
|
|
export interface Options {
|
|
|
|
isSafeMode?: boolean;
|
2021-05-19 15:00:16 +02:00
|
|
|
ResourceModel?: OptionsResourceModel;
|
|
|
|
customCss?: string;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2021-05-19 15:00:16 +02:00
|
|
|
extraRendererRules?: any[];
|
|
|
|
resourceBaseUrl?: string;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-06-01 13:02:36 +02:00
|
|
|
pluginOptions?: any; // Not sure if needed
|
|
|
|
tempDir?: string; // Not sure if needed
|
2024-01-18 13:20:10 +02:00
|
|
|
fsDriver?: FsDriver; // Not sure if needed
|
2021-04-24 20:23:33 +02:00
|
|
|
}
|
|
|
|
|
2024-01-18 13:20:10 +02:00
|
|
|
export default class MarkupToHtml implements MarkupToHtmlConverter {
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static MARKUP_LANGUAGE_MARKDOWN: number = MarkupLanguage.Markdown;
|
|
|
|
public static MARKUP_LANGUAGE_HTML: number = MarkupLanguage.Html;
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2024-01-18 13:20:10 +02:00
|
|
|
private renderers_: Record<string, MarkupRenderer> = {};
|
2021-04-24 20:23:33 +02:00
|
|
|
private options_: Options;
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
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_ = {
|
2024-01-18 13:20:10 +02:00
|
|
|
ResourceModel: defaultResourceModel,
|
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}`);
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
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];
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
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 '';
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
options = { collapseWhiteSpaces: false, ...options };
|
2020-07-15 00:27:12 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
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) {
|
2023-07-19 14:09:03 +02:00
|
|
|
const htmlentities = new AllHtmlEntities();
|
2021-04-24 20:23:33 +02:00
|
|
|
return {
|
2023-07-19 14:09:03 +02:00
|
|
|
html: `<pre>${htmlentities.encode(markup)}</pre>`,
|
2021-04-24 20:23:33 +02:00
|
|
|
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
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
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
|
|
|
}
|