Feat: improve filename sanitization and add normalization for special characters #921

This commit is contained in:
Elio Struyf
2025-02-14 09:35:12 +01:00
parent a164a849da
commit d2b0228809
2 changed files with 9 additions and 2 deletions

View File

@@ -7,6 +7,7 @@
### 🎨 Enhancements
- [#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
- [#921](https://github.com/estruyf/vscode-front-matter/issues/921): Improve the filename sanitization
### ⚡️ Optimizations

View File

@@ -1,17 +1,23 @@
const illegalRe = (isFileName: boolean) =>
isFileName ? /[/?<>\\:*|"!.,;{}[\]()+=~`@#$%^&]/g : /[/?<>\\:*|"!.,;{}[\]()_+=~`@#$%^&]/g;
isFileName ? /[/?<>\\:*|"!.,;{}[\]()+=~`@#$%^&']/g : /[/?<>\\:*|"!.,;{}[\]()_+=~`@#$%^&']/g;
// eslint-disable-next-line no-control-regex
const controlRe = /[\x00-\x1f\x80-\x9f]/g;
const reservedRe = /^\.+$/;
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
const windowsTrailingRe = /[. ]+$/;
function normalizeSpecialChars(input: string): string {
return input.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}
function sanitize(input: string, replacement: string, isFileName?: boolean) {
if (typeof input !== 'string') {
throw new Error('Input must be string');
}
const sanitized = input
const normalizedInput = normalizeSpecialChars(input);
const sanitized = normalizedInput
.replace(illegalRe(isFileName || false), replacement)
.replace(controlRe, replacement)
.replace(reservedRe, replacement)