Skip to content

Commit

Permalink
feat: manage absolute images adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Jan 26, 2024
1 parent 079249a commit afa47d9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
41 changes: 31 additions & 10 deletions src/importer/defaults/rules/adjustImageUrls.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,39 @@
* governing permissions and limitations under the License.
*/

export default function adjustImageUrls(main, url) {
export default function adjustImageUrls(main, url, current) {
[...main.querySelectorAll('img')].forEach((img) => {
const src = img.getAttribute('src');
if (src && (src.startsWith('./') || src.startsWith('/') || src.startsWith('../'))) {
try {
const u = new URL(src, url);
// eslint-disable-next-line no-param-reassign
img.src = u.toString();
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Unable to adjust image URL ${img.src} - removing image`);
img.remove();
if (src) {
if (src.startsWith('./') || src.startsWith('/') || src.startsWith('../')) {
// transform relative URLs to absolute URLs
try {
const targetUrl = new URL(src, url);
// eslint-disable-next-line no-param-reassign
img.src = targetUrl.toString();
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Unable to adjust image URL ${img.src} - removing image`);
img.remove();
}
} else if (current) {
// also transform absolute URLs to current host
try {
const currentSrc = new URL(src);
const currentUrl = new URL(current);
if (currentSrc.host === currentUrl.host) {
// if current host is same than src host, switch src host with url host
// this is the case for absolutes URLs pointing to the same host
const targetUrl = new URL(url);
const newSrc = new URL(`${currentSrc.pathname}${currentSrc.search}${currentSrc.hash}`, `${targetUrl.protocol}//${targetUrl.host}`);
// eslint-disable-next-line no-param-reassign
img.src = newSrc.toString();
}
} catch (e) {
// eslint-disable-next-line no-console
console.log(`Unable to adjust image URL ${img.src} - removing image`);
img.remove();
}
}
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/importer/defaults/transformDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import transformBackgroundImages from './rules/transformBackgroundImages.js';

export default async function transformDOM({
// eslint-disable-next-line no-unused-vars
url, document, html, params,
url, document, html, params = {},
}) {
const main = document.body;

Expand All @@ -35,7 +35,7 @@ export default async function transformDOM({

createMetadata(main, document);
transformBackgroundImages(main, document);
adjustImageUrls(main, url);
adjustImageUrls(main, url, params.originalURL);
convertIcons(main, document);

return main;
Expand Down
12 changes: 9 additions & 3 deletions test/importers/defaults/fixtures/adjust-image-urls.expected.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<body>
<h1>Hello World</h1>
<img src="https://wwww.sample.com/image1.png">
<img src="https://wwww.sample.com/path/image2.png">
<img src="https://wwww.sample.com/image3.png">
<img src="https://wwww.anotherhost.com/image4.png">
<img src="https://wwww.sample.com/image2.png?p=v&amp;p1=v1">
<img src="https://wwww.sample.com/path/image3.png">
<img src="https://wwww.sample.com/path/image4.png?p=v&amp;p1=v1">
<img src="https://wwww.sample.com/go/to/image5.png">
<img src="https://wwww.sample.com/path/go/to/image6.png">
<img src="https://wwww.sample.com/image7.png">
<img src="https://wwww.sample.com/image8.png?p=v&amp;p1=v1">
<img src="https://wwww.sample.com/go/to/image9.png">
<img src="https://wwww.anotherhost.com/image10.png">
</body>
12 changes: 9 additions & 3 deletions test/importers/defaults/fixtures/adjust-image-urls.input.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
<body>
<h1>Hello World</h1>
<img src="/image1.png">
<img src="./image2.png">
<img src="https://wwww.sample.com/image3.png">
<img src="https://wwww.anotherhost.com/image4.png">
<img src="/image2.png?p=v&p1=v1">
<img src="./image3.png">
<img src="./image4.png?p=v&p1=v1">
<img src="/go/to/image5.png">
<img src="./go/to/image6.png">
<img src="https://wwww.currenthost.com/image7.png">
<img src="https://wwww.currenthost.com/image8.png?p=v&p1=v1">
<img src="https://wwww.currenthost.com/go/to/image9.png">
<img src="https://wwww.anotherhost.com/image10.png">
<img src="/\/: #brokenlink#?!$$">
</body>
</html>
3 changes: 3 additions & 0 deletions test/importers/defaults/transformDOM.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ describe('defaultTransformDOM tests', () => {
it('default transformation adjusts image urls', async () => {
await runTest('adjust-image-urls', {
url: 'https://wwww.sample.com/path/page.html',
params: {
originalURL: 'https://wwww.currenthost.com/path/page.html',
},
});
});

Expand Down

0 comments on commit afa47d9

Please sign in to comment.