Skip to content

Commit

Permalink
feat: align the sanitize method with the onedrive support
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Aug 10, 2022
1 parent fafc43d commit 20113e7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 34 deletions.
23 changes: 1 addition & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"rehype-parse": "8.0.3",
"rehype-remark": "9.1.2",
"remark-stringify": "10.0.2",
"sanitize-filename": "1.6.3",
"unified": "10.1.1"
}
}
14 changes: 5 additions & 9 deletions src/utils/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@
* governing permissions and limitations under the License.
*/

import sanitize from 'sanitize-filename';

export default class FileUtils {
static sanitizeFilename(name) {
if (!name) return '';
return sanitize(decodeURIComponent(name))
.trim()
.toLowerCase()
.replace(/\./gm, '')
.replace(/&/gm, '')
.replace(/\s/g, '-')
.replace(/-{2,}/g, '-');
return decodeURIComponent(name).toLowerCase()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '');
}

static sanitizePath(path) {
Expand Down
5 changes: 3 additions & 2 deletions test/utils/FileUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ describe('FileUtils tests', () => {
});

it('FileUtils#sanitizeFilename sanitize', () => {
strictEqual(FileUtils.sanitizeFilename('UPPERCASE---with spaces and%20encoded.dotalso'), 'uppercase-with-spaces-and-encodeddotalso');
strictEqual(FileUtils.sanitizeFilename('UPPERCASE---with spaces and%20encoded.dotalso_nounderscore'), 'uppercase-with-spaces-and-encoded-dotalso-nounderscore');
strictEqual(FileUtils.sanitizeFilename('f@rbidd&n-®-25'), 'f-rbidd-n-25');
});

it('FileUtils#sanitizePath empty', () => {
Expand All @@ -39,6 +40,6 @@ describe('FileUtils tests', () => {
});

it('FileUtils#sanitizePath sanitize', () => {
strictEqual(FileUtils.sanitizePath('//should/BE/1/paTh/to sanitize/but.still.keep.extension'), '/should/be/1/path/to-sanitize/butstillkeep.extension');
strictEqual(FileUtils.sanitizePath('//should/BE/1/p@Th/to%20sanitize/but.still.keep.extension'), '/should/be/1/p-th/to-sanitize/but-still-keep.extension');
});
});

0 comments on commit 20113e7

Please sign in to comment.