You've already forked joplin
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:
43
ReactNativeClient/lib/renderers/HtmlToHtml.js
Normal file
43
ReactNativeClient/lib/renderers/HtmlToHtml.js
Normal 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;
|
||||
Reference in New Issue
Block a user