@adobe/helix-importer 3.4.70 → 3.4.71

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,10 @@
1
+ ## [3.4.71](https://github.com/adobe/helix-importer/compare/v3.4.70...v3.4.71) (2025-03-13)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Provide the ability to know when a svg is being converted ([#508](https://github.com/adobe/helix-importer/issues/508)) ([f987b76](https://github.com/adobe/helix-importer/commit/f987b76ae544709a003926b2e38046c9c057f443)), closes [#507](https://github.com/adobe/helix-importer/issues/507)
7
+
1
8
  ## [3.4.70](https://github.com/adobe/helix-importer/compare/v3.4.69...v3.4.70) (2025-03-12)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-importer",
3
- "version": "3.4.70",
3
+ "version": "3.4.71",
4
4
  "description": "Helix Importer tool: create md / docx from html",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -10,7 +10,19 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export default function convertIcons(main, document) {
13
+ /**
14
+ * This function searches for all `img` elements within the `main` element that have a source
15
+ * ending with `.svg`. It then replaces each of these `img` elements with a `span` element
16
+ * containing the name of the SVG file in the :<name>: format. The name is derived from the
17
+ * filename of the SVG, converted to lowercase, trimmed, and non-alphanumeric characters
18
+ * are replaced with hyphens.
19
+ *
20
+ * @param {HTMLElement} main - The main element containing the `img` elements to be converted.
21
+ * @param {Document} document - The document object used to create new elements.
22
+ * @param {Function} callback - An optional callback function that is invoked with the original
23
+ * src of each converted SVG image and the name of the icon, when the conversion is complete.
24
+ */
25
+ export default function convertIcons(main, document, callback = undefined) {
14
26
  [...main.querySelectorAll('img')].forEach((img) => {
15
27
  const src = img.getAttribute('src');
16
28
  if (src && src.endsWith('.svg')) {
@@ -19,6 +31,10 @@ export default function convertIcons(main, document) {
19
31
  if (name) {
20
32
  span.innerHTML = `:${name}:`;
21
33
  img.replaceWith(span);
34
+
35
+ if (callback && typeof callback === 'function') {
36
+ callback(src, name);
37
+ }
22
38
  }
23
39
  }
24
40
  });
@@ -0,0 +1,120 @@
1
+ /*
2
+ * Copyright 2025 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import { describe, it, beforeEach } from 'mocha';
13
+ import { JSDOM } from 'jsdom';
14
+ import { strictEqual } from 'assert';
15
+ import convertIcons from '../../../../src/importer/defaults/rules/convertIcons.js';
16
+
17
+ describe('convertIcons tests', () => {
18
+ let dom;
19
+
20
+ beforeEach(() => {
21
+ dom = new JSDOM(`
22
+ <html>
23
+ <body>
24
+ <main>
25
+ <img src="icon1.svg" />
26
+ <img src="/path/icon2.svg" />
27
+ </main>
28
+ </body>
29
+ </html>
30
+ `);
31
+ });
32
+
33
+ // expect that images that do not end in svg are not converted
34
+ it('do not convert non-svg img', async () => {
35
+ dom = new JSDOM(`
36
+ <html>
37
+ <body>
38
+ <main>
39
+ <img src="icon1.jpg" alt=""/>
40
+ <img src="icon1.svg" alt=""/>
41
+ </main>
42
+ </body>
43
+ </html>
44
+ `);
45
+
46
+ const { document } = dom.window;
47
+ const main = document.querySelector('main');
48
+
49
+ convertIcons(main, document);
50
+
51
+ const spans = main.querySelectorAll('span');
52
+ strictEqual(spans.length, 1);
53
+
54
+ const imgs = main.querySelectorAll('img');
55
+ strictEqual(imgs.length, 1);
56
+ });
57
+
58
+ it('convert svg img to :format:', async () => {
59
+ const expected = [':icon1:', ':icon2:'];
60
+
61
+ const { document } = dom.window;
62
+ const main = document.querySelector('main');
63
+
64
+ convertIcons(main, document);
65
+
66
+ const spans = main.querySelectorAll('span');
67
+ spans.forEach((span) => {
68
+ strictEqual(span.textContent, expected.shift());
69
+ });
70
+ });
71
+
72
+ it('callback', async () => {
73
+ const urlToIcon = [
74
+ ['icon1.svg', 'icon1'],
75
+ ['/path/icon2.svg', 'icon2'],
76
+ ];
77
+
78
+ const { document } = dom.window;
79
+ const main = document.querySelector('main');
80
+
81
+ const callback = (src, name) => {
82
+ const expected = urlToIcon.shift();
83
+ strictEqual(src, expected[0]);
84
+ strictEqual(name, expected[1]);
85
+ };
86
+
87
+ convertIcons(main, document, callback);
88
+ });
89
+
90
+ it('verify character replacement', async () => {
91
+ const expected = [':icon1:', ':icon2:', ':icon3:', ':icon-4:', ':icon-5:', ':icon---6:', ':-:', ':--:'];
92
+
93
+ dom = new JSDOM(`
94
+ <html>
95
+ <body>
96
+ <main>
97
+ <img src="icon1.svg" alt=""/>
98
+ <img src="/path/icon2.svg" alt=""/>
99
+ <img src="ICON3.svg" alt=""/>
100
+ <img src="ICON 4.svg" alt=""/>
101
+ <img src="ICON%5.svg" alt=""/>
102
+ <img src="icon & 6.svg" alt=""/>
103
+ <img src="%.svg" />
104
+ <img src="%&.svg" />
105
+ </main>
106
+ </body>
107
+ </html>
108
+ `);
109
+
110
+ const { document } = dom.window;
111
+ const main = document.querySelector('main');
112
+
113
+ convertIcons(main, document);
114
+
115
+ const spans = main.querySelectorAll('span');
116
+ spans.forEach((span) => {
117
+ strictEqual(span.textContent, expected.shift());
118
+ });
119
+ });
120
+ });