2020-01-30 23:05:23 +02:00
|
|
|
const htmlUtils = require('./htmlUtils');
|
|
|
|
const utils = require('./utils');
|
|
|
|
const noteStyle = require('./noteStyle');
|
2020-02-14 01:59:23 +02:00
|
|
|
const memoryCache = require('memory-cache');
|
|
|
|
const md5 = require('md5');
|
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-02-14 01:59:23 +02:00
|
|
|
this.cache_ = new memoryCache.Cache();
|
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-02-14 01:59:23 +02:00
|
|
|
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));
|
|
|
|
let html = this.cache_.get(cacheKey);
|
|
|
|
|
|
|
|
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-03-10 01:24:57 +02:00
|
|
|
this.cache_.put(cacheKey, html, 1000 * 60 * 10);
|
|
|
|
|
2020-03-14 01:57:34 +02:00
|
|
|
if (options.bodyOnly) {
|
|
|
|
return {
|
|
|
|
html: html,
|
|
|
|
pluginAssets: [],
|
|
|
|
};
|
|
|
|
}
|
2020-02-14 01:59:23 +02:00
|
|
|
|
2020-03-23 02:47:25 +02:00
|
|
|
let cssStrings = noteStyle(theme);
|
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;
|