You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-15 23:00:36 +02:00
Clipper: Adding support for clipping page as HTML
This commit is contained in:
45
ReactNativeClient/lib/htmlUtils.js
Normal file
45
ReactNativeClient/lib/htmlUtils.js
Normal file
@ -0,0 +1,45 @@
|
||||
const jsdom = require("jsdom");
|
||||
const { JSDOM } = jsdom;
|
||||
|
||||
const htmlUtils = {
|
||||
|
||||
extractImageUrls(html) {
|
||||
if (!html) return [];
|
||||
|
||||
const dom = new JSDOM(html);
|
||||
const imgs = dom.window.document.getElementsByTagName('img');
|
||||
const output = [];
|
||||
|
||||
for (const img of imgs) {
|
||||
const src = img.getAttribute('src');
|
||||
if (!src) continue;
|
||||
output.push(src);
|
||||
}
|
||||
|
||||
return output;
|
||||
},
|
||||
|
||||
replaceImageUrls(html, callback) {
|
||||
if (!html) return '';
|
||||
|
||||
const dom = new JSDOM(html);
|
||||
const doc = dom.window.document;
|
||||
const imgs = doc.getElementsByTagName('img');
|
||||
|
||||
for (const img of imgs) {
|
||||
const src = img.getAttribute('src');
|
||||
const newSrc = callback(src);
|
||||
img.setAttribute('src', newSrc);
|
||||
}
|
||||
|
||||
// This function returns the head and body but without the <head> and <body>
|
||||
// tag, which for our purpose are not needed and can cause issues when present.
|
||||
const output = [];
|
||||
if (doc.head) output.push(doc.head.innerHTML);
|
||||
if (doc.body) output.push(doc.body.innerHTML);
|
||||
return output.join('\n');
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
module.exports = htmlUtils;
|
Reference in New Issue
Block a user