2020-01-30 23:05:23 +02:00
|
|
|
const htmlUtils = require('./htmlUtils');
|
|
|
|
const utils = require('./utils');
|
2020-10-21 01:23:55 +02:00
|
|
|
const noteStyle = require('./noteStyle').default;
|
2020-10-09 19:35:46 +02:00
|
|
|
const Setting = require('lib/models/Setting').default;
|
2020-09-21 17:41:24 +02:00
|
|
|
const { themeStyle } = require('lib/theme');
|
2020-10-16 17:26:19 +02:00
|
|
|
const InMemoryCache = require('lib/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-01-30 23:05:23 +02:00
|
|
|
class HtmlToHtml {
|
|
|
|
constructor(options) {
|
|
|
|
if (!options) options = {};
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
|
|
|
splitHtml(html) {
|
|
|
|
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-03-23 02:47:25 +02:00
|
|
|
async allAssets(/* theme*/) {
|
|
|
|
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
|
|
|
|
async render(markup, _theme, options) {
|
2020-03-10 01:24:57 +02:00
|
|
|
options = Object.assign({}, {
|
|
|
|
splitted: false,
|
|
|
|
}, options);
|
|
|
|
|
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-05-21 10:14:33 +02:00
|
|
|
html = htmlUtils.processImageTags(html, data => {
|
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-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-09-21 17:41:24 +02:00
|
|
|
const lightTheme = themeStyle(Setting.THEME_LIGHT);
|
|
|
|
let cssStrings = noteStyle(lightTheme);
|
2020-03-10 01:24:57 +02:00
|
|
|
|
|
|
|
if (options.splitted) {
|
|
|
|
const splitted = this.splitHtml(html);
|
|
|
|
cssStrings = [splitted.css].concat(cssStrings);
|
|
|
|
|
|
|
|
const output = {
|
|
|
|
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: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = HtmlToHtml;
|