1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-12-02 22:49:09 +02:00

More refactoring to easily handle multiple renderers

This commit is contained in:
Laurent Cozic
2019-07-16 19:05:47 +01:00
parent fbb0ac5892
commit 38e8a881d5
20 changed files with 115 additions and 29 deletions

View File

@@ -0,0 +1,43 @@
const Resource = require('lib/models/Resource');
const htmlUtils = require('lib/htmlUtils');
const utils = require('./utils');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
class HtmlToHtml {
constructor(options) {
this.resourceBaseUrl_ = 'resourceBaseUrl' in options ? options.resourceBaseUrl : null;
}
render(markup, theme, options) {
const dom = new JSDOM(markup);
// Replace all the image resource IDs by path to local files
const imgs = dom.window.document.getElementsByTagName('img');
for (const img of imgs) {
if (!img.src) continue;
const r = utils.imageReplacement(img.src, options.resources, this.resourceBaseUrl_);
if (!r) continue;
if (typeof r === 'string') {
img.outerHTML = r;
} else {
for (const n in r) {
img.setAttribute(n, r[n]);
}
}
}
// We need this extra style so that the images don't overflow
const extraStyle = '<style>img {max-width: 100%;height: auto;}</style>'
return {
html: extraStyle + htmlUtils.headAndBodyHtml(dom.window.document),
cssFiles: [],
}
}
}
module.exports = HtmlToHtml;