2020-11-20 18:04:47 +02:00
|
|
|
import htmlUtils from './htmlUtils';
|
|
|
|
import linkReplacement from './MdToHtml/linkReplacement';
|
|
|
|
import utils from './utils';
|
2020-11-05 18:58:23 +02:00
|
|
|
|
|
|
|
// TODO: fix
|
2020-11-07 17:59:37 +02:00
|
|
|
// const Setting = require('@joplin/lib/models/Setting').default;
|
|
|
|
// const { themeStyle } = require('@joplin/lib/theme');
|
2020-11-05 18:58:23 +02:00
|
|
|
const InMemoryCache = require('./InMemoryCache').default;
|
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);
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
interface FsDriver {
|
|
|
|
writeFile: Function;
|
|
|
|
exists: Function;
|
|
|
|
cacheCssToFile: Function;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Options {
|
|
|
|
ResourceModel: any;
|
|
|
|
resourceBaseUrl?: string;
|
|
|
|
fsDriver?: FsDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RenderOptions {
|
|
|
|
splitted: boolean;
|
|
|
|
bodyOnly: boolean;
|
|
|
|
externalAssetsOnly: boolean;
|
|
|
|
resources: any;
|
|
|
|
postMessageSyntax: string;
|
|
|
|
enableLongPress: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RenderResult {
|
|
|
|
html: string;
|
|
|
|
pluginAssets: any[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class HtmlToHtml {
|
|
|
|
|
|
|
|
private resourceBaseUrl_;
|
|
|
|
private ResourceModel_;
|
|
|
|
private cache_;
|
|
|
|
private fsDriver_: any;
|
|
|
|
|
|
|
|
constructor(options: Options = null) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fsDriver() {
|
|
|
|
return this.fsDriver_;
|
|
|
|
}
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
splitHtml(html: string) {
|
2020-03-10 01:24:57 +02:00
|
|
|
const trimmedHtml = html.trimStart();
|
2020-05-02 17:41:07 +02:00
|
|
|
if (trimmedHtml.indexOf('<style>') !== 0) return { html: html, css: '' };
|
2020-03-10 01:24:57 +02:00
|
|
|
|
|
|
|
const closingIndex = trimmedHtml.indexOf('</style>');
|
2020-05-02 17:41:07 +02:00
|
|
|
if (closingIndex < 0) return { html: html, css: '' };
|
2020-03-10 01:24:57 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
html: trimmedHtml.substr(closingIndex + 8),
|
|
|
|
css: trimmedHtml.substr(7, closingIndex),
|
|
|
|
};
|
2020-01-30 23:05:23 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 18:04:47 +02:00
|
|
|
async allAssets(/* theme*/): Promise<any[]> {
|
2020-03-23 02:47:25 +02:00
|
|
|
return []; // TODO
|
|
|
|
}
|
|
|
|
|
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
|
2020-11-20 18:04:47 +02:00
|
|
|
async render(markup: string, _theme: any, options: RenderOptions): Promise<RenderResult> {
|
|
|
|
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) {
|
|
|
|
html = htmlUtils.sanitizeHtml(markup);
|
|
|
|
|
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;
|
|
|
|
|
2020-08-18 22:52:00 +02:00
|
|
|
const r = utils.imageReplacement(this.ResourceModel_, data.src, options.resources, this.resourceBaseUrl_);
|
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: [],
|
|
|
|
};
|
|
|
|
}
|
2020-02-14 01:59:23 +02:00
|
|
|
|
2020-11-05 18:58:23 +02:00
|
|
|
// const lightTheme = themeStyle(Setting.THEME_LIGHT);
|
|
|
|
// let cssStrings = noteStyle(lightTheme);
|
2020-11-20 18:04:47 +02:00
|
|
|
let cssStrings: string[] = [];
|
2020-03-10 01:24:57 +02:00
|
|
|
|
|
|
|
if (options.splitted) {
|
|
|
|
const splitted = this.splitHtml(html);
|
|
|
|
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: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
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: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|