@adobe/helix-importer 1.14.0 → 1.15.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,10 @@
1
+ # [1.15.0](https://github.com/adobe/helix-importer/compare/v1.14.0...v1.15.0) (2022-08-10)
2
+
3
+
4
+ ### Features
5
+
6
+ * add sanitize path helper ([f0d6865](https://github.com/adobe/helix-importer/commit/f0d6865a6e2dc79ae51b017ab8da0e431d57a539))
7
+
1
8
  # [1.14.0](https://github.com/adobe/helix-importer/compare/v1.13.1...v1.14.0) (2022-07-06)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-importer",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "description": "Helix Importer tool: create md / docx from html",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -14,6 +14,7 @@ import sanitize from 'sanitize-filename';
14
14
 
15
15
  export default class FileUtils {
16
16
  static sanitizeFilename(name) {
17
+ if (!name) return '';
17
18
  return sanitize(decodeURIComponent(name))
18
19
  .trim()
19
20
  .toLowerCase()
@@ -22,4 +23,20 @@ export default class FileUtils {
22
23
  .replace(/\s/g, '-')
23
24
  .replace(/-{2,}/g, '-');
24
25
  }
26
+
27
+ static sanitizePath(path) {
28
+ if (!path) return '';
29
+ const extension = path.split('.').pop();
30
+ const pathname = extension !== path ? path.substring(0, path.lastIndexOf('.')) : path;
31
+ let sanitizedPath = '';
32
+ pathname.split('/').forEach((p) => {
33
+ if (p !== '') {
34
+ sanitizedPath += `/${FileUtils.sanitizeFilename(p)}`;
35
+ }
36
+ });
37
+ if (extension !== path) {
38
+ sanitizedPath += `.${extension}`;
39
+ }
40
+ return sanitizedPath;
41
+ }
25
42
  }
@@ -0,0 +1,44 @@
1
+ /*
2
+ * Copyright 2020 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
+ import { strictEqual } from 'assert';
14
+ import { describe, it } from 'mocha';
15
+
16
+ import FileUtils from '../../src/utils/FileUtils.js';
17
+
18
+ describe('FileUtils tests', () => {
19
+ it('FileUtils#sanitizeFilename empty', () => {
20
+ strictEqual(FileUtils.sanitizeFilename(), '');
21
+ });
22
+
23
+ it('FileUtils#sanitizeFilename no change', () => {
24
+ strictEqual(FileUtils.sanitizeFilename('simple'), 'simple');
25
+ strictEqual(FileUtils.sanitizeFilename('with-dash-and-1-number'), 'with-dash-and-1-number');
26
+ });
27
+
28
+ it('FileUtils#sanitizeFilename sanitize', () => {
29
+ strictEqual(FileUtils.sanitizeFilename('UPPERCASE---with spaces and%20encoded.dotalso'), 'uppercase-with-spaces-and-encodeddotalso');
30
+ });
31
+
32
+ it('FileUtils#sanitizePath empty', () => {
33
+ strictEqual(FileUtils.sanitizePath(), '');
34
+ });
35
+
36
+ it('FileUtils#sanitizePath no change', () => {
37
+ strictEqual(FileUtils.sanitizePath('/should/be/valid/path'), '/should/be/valid/path');
38
+ strictEqual(FileUtils.sanitizePath('/should/be/valid/path/with.extension'), '/should/be/valid/path/with.extension');
39
+ });
40
+
41
+ it('FileUtils#sanitizePath sanitize', () => {
42
+ strictEqual(FileUtils.sanitizePath('//should/BE/1/paTh/to sanitize/but.still.keep.extension'), '/should/be/1/path/to-sanitize/butstillkeep.extension');
43
+ });
44
+ });