2020-11-20 18:04:47 +02:00
|
|
|
import htmlUtils from './htmlUtils';
|
|
|
|
import linkReplacement from './MdToHtml/linkReplacement';
|
2021-01-29 20:45:11 +02:00
|
|
|
import utils, { ItemIdToUrlHandler } from './utils';
|
2021-01-22 19:41:11 +02:00
|
|
|
import InMemoryCache from './InMemoryCache';
|
2021-05-19 15:00:16 +02:00
|
|
|
import { RenderResult } from './MarkupToHtml';
|
2023-12-29 18:08:09 +02:00
|
|
|
import noteStyle, { whiteBackgroundNoteStyle } from './noteStyle';
|
|
|
|
import { Options as NoteStyleOptions } from './noteStyle';
|
2020-02-14 01:59:23 +02:00
|
|
|
const md5 = require('md5');
|
2020-01-30 23:05:23 +02:00
|
|
|
|
2020-10-16 17:26:19 +02:00
|
|
|
// Renderered notes can potentially be quite large (for example
|
|
|
|
// when they come from the clipper) so keep the cache size
|
|
|
|
// relatively small.
|
|
|
|
const inMemoryCache = new InMemoryCache(10);
|
|
|
|
|
2023-07-26 18:36:21 +02:00
|
|
|
export interface SplittedHtml {
|
|
|
|
html: string;
|
|
|
|
css: string;
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
interface FsDriver {
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-20 18:04:47 +02:00
|
|
|
writeFile: Function;
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-20 18:04:47 +02:00
|
|
|
exists: Function;
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2020-11-20 18:04:47 +02:00
|
|
|
cacheCssToFile: Function;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Options {
|
|
|
|
ResourceModel: any;
|
|
|
|
resourceBaseUrl?: string;
|
|
|
|
fsDriver?: FsDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RenderOptions {
|
|
|
|
splitted: boolean;
|
|
|
|
bodyOnly: boolean;
|
|
|
|
externalAssetsOnly: boolean;
|
|
|
|
resources: any;
|
|
|
|
postMessageSyntax: string;
|
|
|
|
enableLongPress: boolean;
|
2021-01-29 20:45:11 +02:00
|
|
|
itemIdToUrl?: ItemIdToUrlHandler;
|
2023-12-06 21:17:16 +02:00
|
|
|
allowedFilePrefixes?: string[];
|
2023-12-29 18:08:09 +02:00
|
|
|
whiteBackgroundNoteRendering?: boolean;
|
2020-11-20 18:04:47 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
// https://github.com/es-shims/String.prototype.trimStart/blob/main/implementation.js
|
|
|
|
function trimStart(s: string): string {
|
|
|
|
// eslint-disable-next-line no-control-regex
|
|
|
|
const startWhitespace = /^[\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF]*/;
|
|
|
|
return s.replace(startWhitespace, '');
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
export default class HtmlToHtml {
|
|
|
|
|
|
|
|
private resourceBaseUrl_;
|
|
|
|
private ResourceModel_;
|
|
|
|
private cache_;
|
|
|
|
private fsDriver_: any;
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public constructor(options: Options = null) {
|
2020-11-20 18:04:47 +02:00
|
|
|
options = {
|
|
|
|
ResourceModel: null,
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
2020-01-30 23:05:23 +02:00
|
|
|
this.resourceBaseUrl_ = 'resourceBaseUrl' in options ? options.resourceBaseUrl : null;
|
|
|
|
this.ResourceModel_ = options.ResourceModel;
|
2020-10-16 17:26:19 +02:00
|
|
|
this.cache_ = inMemoryCache;
|
2020-03-10 01:24:57 +02:00
|
|
|
this.fsDriver_ = {
|
|
|
|
writeFile: (/* path, content, encoding = 'base64'*/) => { throw new Error('writeFile not set'); },
|
|
|
|
exists: (/* path*/) => { throw new Error('exists not set'); },
|
|
|
|
cacheCssToFile: (/* cssStrings*/) => { throw new Error('cacheCssToFile not set'); },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.fsDriver) {
|
|
|
|
if (options.fsDriver.writeFile) this.fsDriver_.writeFile = options.fsDriver.writeFile;
|
|
|
|
if (options.fsDriver.exists) this.fsDriver_.exists = options.fsDriver.exists;
|
|
|
|
if (options.fsDriver.cacheCssToFile) this.fsDriver_.cacheCssToFile = options.fsDriver.cacheCssToFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public fsDriver() {
|
2020-03-10 01:24:57 +02:00
|
|
|
return this.fsDriver_;
|
|
|
|
}
|
|
|
|
|
2023-12-29 18:08:09 +02:00
|
|
|
public async allAssets(theme: any, noteStyleOptions: NoteStyleOptions = null) {
|
|
|
|
let cssStrings: string[] = [];
|
|
|
|
|
|
|
|
if (noteStyleOptions.whiteBackgroundNoteRendering) {
|
|
|
|
cssStrings = [whiteBackgroundNoteStyle()];
|
|
|
|
} else {
|
|
|
|
cssStrings = [noteStyle(theme, noteStyleOptions).join('\n')];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [await this.fsDriver().cacheCssToFile(cssStrings)];
|
2020-03-23 02:47:25 +02:00
|
|
|
}
|
|
|
|
|
2020-09-21 17:41:24 +02:00
|
|
|
// Note: the "theme" variable is ignored and instead the light theme is
|
|
|
|
// always used for HTML notes.
|
|
|
|
// See: https://github.com/laurent22/joplin/issues/3698
|
2023-12-29 18:08:09 +02:00
|
|
|
public async render(markup: string, theme: any, options: RenderOptions): Promise<RenderResult> {
|
2020-11-20 18:04:47 +02:00
|
|
|
options = {
|
2020-03-10 01:24:57 +02:00
|
|
|
splitted: false,
|
2020-11-20 18:04:47 +02:00
|
|
|
postMessageSyntax: 'postMessage',
|
|
|
|
enableLongPress: false,
|
|
|
|
...options,
|
|
|
|
};
|
2020-03-10 01:24:57 +02:00
|
|
|
|
2020-02-14 01:59:23 +02:00
|
|
|
const cacheKey = md5(escape(markup));
|
2020-10-16 17:26:19 +02:00
|
|
|
let html = this.cache_.value(cacheKey);
|
2020-02-14 01:59:23 +02:00
|
|
|
|
|
|
|
if (!html) {
|
2023-12-06 21:17:16 +02:00
|
|
|
html = htmlUtils.sanitizeHtml(markup, {
|
|
|
|
allowedFilePrefixes: options.allowedFilePrefixes,
|
|
|
|
});
|
2020-02-14 01:59:23 +02:00
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
html = htmlUtils.processImageTags(html, (data: any) => {
|
2020-02-14 01:59:23 +02:00
|
|
|
if (!data.src) return null;
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
const r = utils.imageReplacement(this.ResourceModel_, data.src, options.resources, this.resourceBaseUrl_, options.itemIdToUrl);
|
2020-02-14 01:59:23 +02:00
|
|
|
if (!r) return null;
|
|
|
|
|
|
|
|
if (typeof r === 'string') {
|
|
|
|
return {
|
|
|
|
type: 'replaceElement',
|
|
|
|
html: r,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type: 'setAttributes',
|
|
|
|
attrs: r,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
2020-11-20 18:04:47 +02:00
|
|
|
|
|
|
|
html = htmlUtils.processAnchorTags(html, (data: any) => {
|
|
|
|
if (!data.href) return null;
|
|
|
|
|
|
|
|
const r = linkReplacement(data.href, {
|
|
|
|
resources: options.resources,
|
|
|
|
ResourceModel: this.ResourceModel_,
|
|
|
|
postMessageSyntax: options.postMessageSyntax,
|
|
|
|
enableLongPress: options.enableLongPress,
|
|
|
|
});
|
|
|
|
|
2020-12-09 23:30:51 +02:00
|
|
|
if (!r.html) return null;
|
2020-11-20 18:04:47 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'replaceElement',
|
2020-12-09 23:30:51 +02:00
|
|
|
html: r.html,
|
2020-11-20 18:04:47 +02:00
|
|
|
};
|
|
|
|
});
|
2020-02-14 01:59:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 17:26:19 +02:00
|
|
|
this.cache_.setValue(cacheKey, html, 1000 * 60 * 10);
|
2020-03-10 01:24:57 +02:00
|
|
|
|
2020-03-14 01:57:34 +02:00
|
|
|
if (options.bodyOnly) {
|
|
|
|
return {
|
|
|
|
html: html,
|
|
|
|
pluginAssets: [],
|
2021-08-12 18:23:49 +02:00
|
|
|
cssStrings: [],
|
2020-03-14 01:57:34 +02:00
|
|
|
};
|
|
|
|
}
|
2020-02-14 01:59:23 +02:00
|
|
|
|
2023-12-29 18:08:09 +02:00
|
|
|
let cssStrings = options.whiteBackgroundNoteRendering ? [whiteBackgroundNoteStyle()] : noteStyle(theme);
|
2020-03-10 01:24:57 +02:00
|
|
|
|
|
|
|
if (options.splitted) {
|
2023-07-26 18:36:21 +02:00
|
|
|
const splitted = splitHtml(html);
|
2020-03-10 01:24:57 +02:00
|
|
|
cssStrings = [splitted.css].concat(cssStrings);
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
const output: RenderResult = {
|
2020-03-10 01:24:57 +02:00
|
|
|
html: splitted.html,
|
|
|
|
pluginAssets: [],
|
2021-08-12 18:23:49 +02:00
|
|
|
cssStrings: [],
|
2020-03-10 01:24:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (options.externalAssetsOnly) {
|
|
|
|
output.pluginAssets.push(await this.fsDriver().cacheCssToFile(cssStrings));
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
2020-01-30 23:05:23 +02:00
|
|
|
|
|
|
|
const styleHtml = `<style>${cssStrings.join('\n')}</style>`;
|
|
|
|
|
|
|
|
return {
|
|
|
|
html: styleHtml + html,
|
|
|
|
pluginAssets: [],
|
2021-08-12 18:23:49 +02:00
|
|
|
cssStrings: [],
|
2020-01-30 23:05:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-07-26 18:36:21 +02:00
|
|
|
|
|
|
|
const splitHtmlRegex = /^<style>([\s\S]*)<\/style>([\s\S]*)$/i;
|
|
|
|
|
|
|
|
// This function is designed to handle the narrow case of HTML generated by the
|
|
|
|
// HtmlToHtml class and used by the Rich Text editor, and that's with the STYLE
|
|
|
|
// tag at the top, followed by the HTML code. If it's anything else, we don't
|
|
|
|
// try to handle it and return the whole HTML code.
|
|
|
|
export const splitHtml = (html: string): SplittedHtml => {
|
|
|
|
const trimmedHtml = trimStart(html);
|
|
|
|
const result = trimmedHtml.match(splitHtmlRegex);
|
|
|
|
if (!result) return { html, css: '' };
|
|
|
|
return { html: result[2], css: result[1] };
|
|
|
|
};
|