@adobe/helix-importer 1.3.0 → 1.5.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 +21 -0
- package/package.json +2 -2
- package/src/importer/PageImporter.js +5 -8
- package/src/importer/PageImporterParams.js +2 -0
- package/src/utils/DOMUtils.js +10 -2
- package/src/utils/MDUtils.js +21 -0
- package/test/importers/PageImporter.spec.js +26 -0
- package/test/importers/fixtures/custom-styles.xml +1084 -0
- package/test/utils/DOMUtils.spec.js +34 -15
- package/test/utils/MDUtils.spec.js +54 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
# [1.5.0](https://github.com/adobe/helix-importer/compare/v1.4.1...v1.5.0) (2022-02-24)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* helper method to get an image from background image style ([#17](https://github.com/adobe/helix-importer/issues/17)) ([63a384d](https://github.com/adobe/helix-importer/commit/63a384dceb75e6815115ffd1c37c321fc7476ab6))
|
|
7
|
+
|
|
8
|
+
## [1.4.1](https://github.com/adobe/helix-importer/compare/v1.4.0...v1.4.1) (2022-02-24)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* some images come with wrong src in md ([#16](https://github.com/adobe/helix-importer/issues/16)) ([7f27432](https://github.com/adobe/helix-importer/commit/7f274326683313cd11c2a92bb1c01c1960de5fcd))
|
|
14
|
+
|
|
15
|
+
# [1.4.0](https://github.com/adobe/helix-importer/compare/v1.3.0...v1.4.0) (2022-02-02)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* add docxStylesXML param support ([1a10784](https://github.com/adobe/helix-importer/commit/1a1078453f1f5d761cf7f64addc013f8b73dc225))
|
|
21
|
+
|
|
1
22
|
# [1.3.0](https://github.com/adobe/helix-importer/compare/v1.2.2...v1.3.0) (2022-01-31)
|
|
2
23
|
|
|
3
24
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/helix-importer",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Helix Importer tool: create md / docx from html",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"author": "",
|
|
48
48
|
"license": "Apache-2.0",
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@adobe/helix-md2docx": "1.
|
|
50
|
+
"@adobe/helix-md2docx": "1.3.0",
|
|
51
51
|
"form-data": "4.0.0",
|
|
52
52
|
"fs-extra": "10.0.0",
|
|
53
53
|
"hast-util-to-html": "8.0.3",
|
|
@@ -25,6 +25,7 @@ import { md2docx } from '@adobe/helix-md2docx';
|
|
|
25
25
|
import Utils from '../utils/Utils.js';
|
|
26
26
|
import DOMUtils from '../utils/DOMUtils.js';
|
|
27
27
|
import FileUtils from '../utils/FileUtils.js';
|
|
28
|
+
import MDUtils from '../utils/MDUtils.js';
|
|
28
29
|
|
|
29
30
|
export default class PageImporter {
|
|
30
31
|
params;
|
|
@@ -41,7 +42,7 @@ export default class PageImporter {
|
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
async convertToDocx(docxPath, content) {
|
|
44
|
-
const buffer = await md2docx(content, this.logger);
|
|
45
|
+
const buffer = await md2docx(content, this.logger, this.params.docxStylesXML);
|
|
45
46
|
return this.params.storageHandler.put(docxPath, buffer);
|
|
46
47
|
}
|
|
47
48
|
|
|
@@ -112,7 +113,7 @@ export default class PageImporter {
|
|
|
112
113
|
});
|
|
113
114
|
}
|
|
114
115
|
} catch (error) {
|
|
115
|
-
this.logger.warn(`Invalid link in the page: ${href}
|
|
116
|
+
this.logger.warn(`Invalid link in the page: ${href}`, error);
|
|
116
117
|
}
|
|
117
118
|
}
|
|
118
119
|
});
|
|
@@ -138,19 +139,15 @@ export default class PageImporter {
|
|
|
138
139
|
});
|
|
139
140
|
}
|
|
140
141
|
} catch (error) {
|
|
141
|
-
this.logger.warn(`Invalid video in the page: ${src}
|
|
142
|
+
this.logger.warn(`Invalid video in the page: ${src}`, error);
|
|
142
143
|
}
|
|
143
144
|
}
|
|
144
145
|
});
|
|
145
146
|
|
|
146
|
-
const patchSrcInContent = (c, oldSrc, newSrc) => contents
|
|
147
|
-
.replace(new RegExp(`${oldSrc.replace('.', '\\.').replace('?', '\\?')}`, 'gm'), newSrc)
|
|
148
|
-
.replace(new RegExp(`${decodeURI(oldSrc).replace('.', '\\.')}`, 'gm'), newSrc);
|
|
149
|
-
|
|
150
147
|
// adjust assets url (from relative to absolute)
|
|
151
148
|
assets.forEach((asset) => {
|
|
152
149
|
const u = new URL(decodeURI(asset.url), url);
|
|
153
|
-
contents =
|
|
150
|
+
contents = MDUtils.replaceSrcInMarkdown(contents, asset.url, u.toString());
|
|
154
151
|
});
|
|
155
152
|
|
|
156
153
|
if (resource.prepend) {
|
package/src/utils/DOMUtils.js
CHANGED
|
@@ -231,14 +231,22 @@ export default class DOMUtils {
|
|
|
231
231
|
}
|
|
232
232
|
|
|
233
233
|
static replaceBackgroundByImg(element, document) {
|
|
234
|
+
const img = DOMUtils.getImgFromBackground(element, document);
|
|
235
|
+
if (img) {
|
|
236
|
+
element.replaceWith(img);
|
|
237
|
+
return img;
|
|
238
|
+
}
|
|
239
|
+
return element;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
static getImgFromBackground(element, document) {
|
|
234
243
|
const url = element?.style?.['background-image'];
|
|
235
244
|
if (url) {
|
|
236
245
|
const src = url.replace(/url\(/gm, '').replace(/'/gm, '').replace(/\)/gm, '');
|
|
237
246
|
const img = document.createElement('img');
|
|
238
247
|
img.src = src;
|
|
239
|
-
element.replaceWith(img);
|
|
240
248
|
return img;
|
|
241
249
|
}
|
|
242
|
-
return
|
|
250
|
+
return null;
|
|
243
251
|
}
|
|
244
252
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2022 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
|
+
|
|
13
|
+
export default class MDUtils {
|
|
14
|
+
static replaceSrcInMarkdown = (md, oldSrc, newSrc) => {
|
|
15
|
+
if (decodeURI(oldSrc) !== oldSrc) {
|
|
16
|
+
return md.replace(new RegExp(`${decodeURI(oldSrc).replace('.', '\\.')}`, 'gm'), newSrc);
|
|
17
|
+
} else {
|
|
18
|
+
return md.replace(new RegExp(`${oldSrc.replace('.', '\\.').replace('?', '\\?')}`, 'gm'), newSrc);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -89,6 +89,32 @@ describe('PageImporter tests - various options', () => {
|
|
|
89
89
|
});
|
|
90
90
|
strictEqual(md, test, 'valid backward conversion');
|
|
91
91
|
});
|
|
92
|
+
|
|
93
|
+
it('import - can provide a custom styles.xml', async () => {
|
|
94
|
+
const docxStylesXML = await fs.readFile(path.resolve(__dirname, 'fixtures', 'custom-styles.xml'), 'utf-8');
|
|
95
|
+
const storageHandler = new MemoryHandler(logger);
|
|
96
|
+
const config = {
|
|
97
|
+
storageHandler,
|
|
98
|
+
logger,
|
|
99
|
+
docxStylesXML,
|
|
100
|
+
};
|
|
101
|
+
const se = new Test(config);
|
|
102
|
+
const results = await se.import('/someurl');
|
|
103
|
+
|
|
104
|
+
strictEqual(results.length, 1, 'expect no result');
|
|
105
|
+
|
|
106
|
+
ok(await storageHandler.exists('/someurl/somecomputedpath/resource1.md'), 'md has been stored');
|
|
107
|
+
ok(await storageHandler.exists('/someurl/somecomputedpath/resource1.docx'), 'docx has been stored');
|
|
108
|
+
|
|
109
|
+
const md = await storageHandler.get('/someurl/somecomputedpath/resource1.md');
|
|
110
|
+
strictEqual(md, '# heading1\n\nparagraph\n', 'valid markdown created');
|
|
111
|
+
|
|
112
|
+
const docx = await storageHandler.get('/someurl/somecomputedpath/resource1.docx');
|
|
113
|
+
const test = await docx2md(docx, {
|
|
114
|
+
mediaHandler: new MockMediaHandler(),
|
|
115
|
+
});
|
|
116
|
+
strictEqual(md, test, 'valid backward conversion');
|
|
117
|
+
});
|
|
92
118
|
});
|
|
93
119
|
|
|
94
120
|
describe('PageImporter tests - fixtures', () => {
|