@adobe/helix-importer 3.1.3 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [3.2.0](https://github.com/adobe/helix-importer/compare/v3.1.4...v3.2.0) (2024-01-29)
2
+
3
+
4
+ ### Features
5
+
6
+ * manage absolute images adjustment ([#296](https://github.com/adobe/helix-importer/issues/296)) ([abd19ae](https://github.com/adobe/helix-importer/commit/abd19aeb59a8d00574762d477f012c38e996e869))
7
+
8
+ ## [3.1.4](https://github.com/adobe/helix-importer/compare/v3.1.3...v3.1.4) (2024-01-17)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * import comments cleanup logic ([#290](https://github.com/adobe/helix-importer/issues/290)) ([deec675](https://github.com/adobe/helix-importer/commit/deec675c6e88f0035a3d5b3d88b4068f106a8f80))
14
+
1
15
  ## [3.1.3](https://github.com/adobe/helix-importer/compare/v3.1.2...v3.1.3) (2024-01-06)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-importer",
3
- "version": "3.1.3",
3
+ "version": "3.2.0",
4
4
  "description": "Helix Importer tool: create md / docx from html",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -13,7 +13,6 @@
13
13
  "test:web": "web-test-runner test/browser/*.test.js --node-resolve",
14
14
  "test:web:watch": "web-test-runner test/browser/*.test.js --node-resolve --watch",
15
15
  "test": "c8 mocha && npm run test:web",
16
- "test-ci": "c8 mocha && npm run test:web",
17
16
  "semantic-release": "semantic-release",
18
17
  "prepare": "npx husky install"
19
18
  },
@@ -29,7 +28,7 @@
29
28
  "devDependencies": {
30
29
  "@adobe/eslint-config-helix": "2.0.5",
31
30
  "@adobe/helix-docx2md": "1.5.0",
32
- "@adobe/helix-mediahandler": "2.4.4",
31
+ "@adobe/helix-mediahandler": "2.4.7",
33
32
  "@esm-bundle/chai": "4.3.4-fix.0",
34
33
  "@semantic-release/changelog": "6.0.3",
35
34
  "@semantic-release/exec": "6.0.3",
@@ -37,12 +36,12 @@
37
36
  "@web/test-runner": "0.18.0",
38
37
  "@web/test-runner-commands": "0.9.0",
39
38
  "@web/test-runner-mocha": "0.9.0",
40
- "c8": "8.0.1",
41
- "chai": "4.4.0",
39
+ "c8": "9.1.0",
40
+ "chai": "5.0.3",
42
41
  "dirname-filename-esm": "1.1.1",
43
42
  "eslint": "8.56.0",
44
43
  "husky": "8.0.3",
45
- "jsdom": "23.1.0",
44
+ "jsdom": "24.0.0",
46
45
  "lint-staged": "15.2.0",
47
46
  "mocha": "10.2.0",
48
47
  "mocha-multi-reporters": "1.5.1",
@@ -10,14 +10,29 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export default function adjustImageUrls(main, url) {
13
+ export default function adjustImageUrls(main, url, current) {
14
14
  [...main.querySelectorAll('img')].forEach((img) => {
15
15
  const src = img.getAttribute('src');
16
- if (src && (src.startsWith('./') || src.startsWith('/') || src.startsWith('../'))) {
16
+ if (src) {
17
17
  try {
18
- const u = new URL(src, url);
19
- // eslint-disable-next-line no-param-reassign
20
- img.src = u.toString();
18
+ if (src.startsWith('./') || src.startsWith('/') || src.startsWith('../')) {
19
+ // transform relative URLs to absolute URLs
20
+ const targetUrl = new URL(src, url);
21
+ // eslint-disable-next-line no-param-reassign
22
+ img.src = targetUrl.toString();
23
+ } else if (current) {
24
+ // also transform absolute URLs to current host
25
+ const currentSrc = new URL(src);
26
+ const currentUrl = new URL(current);
27
+ if (currentSrc.host === currentUrl.host) {
28
+ // if current host is same than src host, switch src host with url host
29
+ // this is the case for absolutes URLs pointing to the same host
30
+ const targetUrl = new URL(url);
31
+ const newSrc = new URL(`${currentSrc.pathname}${currentSrc.search}${currentSrc.hash}`, `${targetUrl.protocol}//${targetUrl.host}`);
32
+ // eslint-disable-next-line no-param-reassign
33
+ img.src = newSrc.toString();
34
+ }
35
+ }
21
36
  } catch (e) {
22
37
  // eslint-disable-next-line no-console
23
38
  console.log(`Unable to adjust image URL ${img.src} - removing image`);
@@ -17,7 +17,7 @@ import transformBackgroundImages from './rules/transformBackgroundImages.js';
17
17
 
18
18
  export default async function transformDOM({
19
19
  // eslint-disable-next-line no-unused-vars
20
- url, document, html, params,
20
+ url, document, html, params = {},
21
21
  }) {
22
22
  const main = document.body;
23
23
 
@@ -35,7 +35,7 @@ export default async function transformDOM({
35
35
 
36
36
  createMetadata(main, document);
37
37
  transformBackgroundImages(main, document);
38
- adjustImageUrls(main, url);
38
+ adjustImageUrls(main, url, params.originalURL);
39
39
  convertIcons(main, document);
40
40
 
41
41
  return main;
@@ -139,7 +139,8 @@ export default class DOMUtils {
139
139
  // eslint-disable-next-line no-param-reassign
140
140
  document.body.innerHTML = document.body.innerHTML
141
141
  // remove html comments
142
- .replace(/<!--(?!>)[\S\s]*?-->/gm, '');
142
+ .replace(/><!--(?!>)[\S\s]*?-->/gm, '>')
143
+ .replace(/\n<!--(?!>)[\S\s]*?-->/gm, '\n');
143
144
  }
144
145
 
145
146
  static removeSpans(document) {
@@ -1,7 +1,13 @@
1
1
  <body>
2
2
  <h1>Hello World</h1>
3
3
  <img src="https://wwww.sample.com/image1.png">
4
- <img src="https://wwww.sample.com/path/image2.png">
5
- <img src="https://wwww.sample.com/image3.png">
6
- <img src="https://wwww.anotherhost.com/image4.png">
4
+ <img src="https://wwww.sample.com/image2.png?p=v&amp;p1=v1">
5
+ <img src="https://wwww.sample.com/path/image3.png">
6
+ <img src="https://wwww.sample.com/path/image4.png?p=v&amp;p1=v1">
7
+ <img src="https://wwww.sample.com/go/to/image5.png">
8
+ <img src="https://wwww.sample.com/path/go/to/image6.png">
9
+ <img src="https://wwww.sample.com/image7.png">
10
+ <img src="https://wwww.sample.com/image8.png?p=v&amp;p1=v1">
11
+ <img src="https://wwww.sample.com/go/to/image9.png">
12
+ <img src="https://wwww.anotherhost.com/image10.png">
7
13
  </body>
@@ -2,9 +2,15 @@
2
2
  <body>
3
3
  <h1>Hello World</h1>
4
4
  <img src="/image1.png">
5
- <img src="./image2.png">
6
- <img src="https://wwww.sample.com/image3.png">
7
- <img src="https://wwww.anotherhost.com/image4.png">
5
+ <img src="/image2.png?p=v&p1=v1">
6
+ <img src="./image3.png">
7
+ <img src="./image4.png?p=v&p1=v1">
8
+ <img src="/go/to/image5.png">
9
+ <img src="./go/to/image6.png">
10
+ <img src="https://wwww.currenthost.com/image7.png">
11
+ <img src="https://wwww.currenthost.com/image8.png?p=v&p1=v1">
12
+ <img src="https://wwww.currenthost.com/go/to/image9.png">
13
+ <img src="https://wwww.anotherhost.com/image10.png">
8
14
  <img src="/\/: #brokenlink#?!$$">
9
15
  </body>
10
16
  </html>
@@ -77,6 +77,9 @@ describe('defaultTransformDOM tests', () => {
77
77
  it('default transformation adjusts image urls', async () => {
78
78
  await runTest('adjust-image-urls', {
79
79
  url: 'https://wwww.sample.com/path/page.html',
80
+ params: {
81
+ originalURL: 'https://wwww.currenthost.com/path/page.html',
82
+ },
80
83
  });
81
84
  });
82
85
 
@@ -182,6 +182,8 @@ describe('DOMUtils#removeCommments tests', () => {
182
182
  test('<p><!-- useless comment \n multiline --></p>', '<p></p>');
183
183
  test('<p><!-- useless comment \n multiline \n multiline --></p>', '<p></p>');
184
184
  test('<!-- useless comment --><p>The content stays</p><!-- another useless comment with \n line break -->', '<p>The content stays</p>');
185
+ test('<p>This is a paragraph.</p>\n\x3C!--\n<p>Look at this cool image:</p>\n<img border="0" src="pic_trulli.jpg" alt="Trulli">\n-->\n<p>This is a paragraph too.</p>\x3C!-- same line -->\n\x3C!-- single line -->', '<p>This is a paragraph.</p>\n\n<p>This is a paragraph too.</p>\n');
186
+ test('<div some-crazy-attribute="" <!--=""><!-- useless comment --></div>', '<div some-crazy-attribute="" <!--=""></div>');
185
187
  });
186
188
  });
187
189