Skip to content

Commit d2b0228

Browse files
committed
Feat: improve filename sanitization and add normalization for special characters #921
1 parent a164a84 commit d2b0228

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### 🎨 Enhancements
88

99
- [#915](https://github.com/estruyf/vscode-front-matter/issues/915): Added a new setting `frontMatter.panel.openOnSupportedFile` which allows you to open the panel view on supported files
10+
- [#921](https://github.com/estruyf/vscode-front-matter/issues/921): Improve the filename sanitization
1011

1112
### ⚡️ Optimizations
1213

Diff for: src/helpers/Sanitize.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
const illegalRe = (isFileName: boolean) =>
2-
isFileName ? /[/?<>\\:*|"!.,;{}[\]()+=~`@#$%^&]/g : /[/?<>\\:*|"!.,;{}[\]()_+=~`@#$%^&]/g;
2+
isFileName ? /[/?<>\\:*|"!.,;{}[\]()+=~`@#$%^&']/g : /[/?<>\\:*|"!.,;{}[\]()_+=~`@#$%^&']/g;
33
// eslint-disable-next-line no-control-regex
44
const controlRe = /[\x00-\x1f\x80-\x9f]/g;
55
const reservedRe = /^\.+$/;
66
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
77
const windowsTrailingRe = /[. ]+$/;
88

9+
function normalizeSpecialChars(input: string): string {
10+
return input.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
11+
}
12+
913
function sanitize(input: string, replacement: string, isFileName?: boolean) {
1014
if (typeof input !== 'string') {
1115
throw new Error('Input must be string');
1216
}
1317

14-
const sanitized = input
18+
const normalizedInput = normalizeSpecialChars(input);
19+
20+
const sanitized = normalizedInput
1521
.replace(illegalRe(isFileName || false), replacement)
1622
.replace(controlRe, replacement)
1723
.replace(reservedRe, replacement)

0 commit comments

Comments
 (0)