mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-03-28 17:42:40 +01:00
Feat: improve filename sanitization and add normalization for special characters #921
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
### 🎨 Enhancements
|
### 🎨 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
|
- [#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
|
### ⚡️ Optimizations
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,23 @@
|
|||||||
const illegalRe = (isFileName: boolean) =>
|
const illegalRe = (isFileName: boolean) =>
|
||||||
isFileName ? /[/?<>\\:*|"!.,;{}[\]()+=~`@#$%^&]/g : /[/?<>\\:*|"!.,;{}[\]()_+=~`@#$%^&]/g;
|
isFileName ? /[/?<>\\:*|"!.,;{}[\]()+=~`@#$%^&']/g : /[/?<>\\:*|"!.,;{}[\]()_+=~`@#$%^&']/g;
|
||||||
// eslint-disable-next-line no-control-regex
|
// eslint-disable-next-line no-control-regex
|
||||||
const controlRe = /[\x00-\x1f\x80-\x9f]/g;
|
const controlRe = /[\x00-\x1f\x80-\x9f]/g;
|
||||||
const reservedRe = /^\.+$/;
|
const reservedRe = /^\.+$/;
|
||||||
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
|
const windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
|
||||||
const windowsTrailingRe = /[. ]+$/;
|
const windowsTrailingRe = /[. ]+$/;
|
||||||
|
|
||||||
|
function normalizeSpecialChars(input: string): string {
|
||||||
|
return input.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
|
||||||
|
}
|
||||||
|
|
||||||
function sanitize(input: string, replacement: string, isFileName?: boolean) {
|
function sanitize(input: string, replacement: string, isFileName?: boolean) {
|
||||||
if (typeof input !== 'string') {
|
if (typeof input !== 'string') {
|
||||||
throw new Error('Input must be string');
|
throw new Error('Input must be string');
|
||||||
}
|
}
|
||||||
|
|
||||||
const sanitized = input
|
const normalizedInput = normalizeSpecialChars(input);
|
||||||
|
|
||||||
|
const sanitized = normalizedInput
|
||||||
.replace(illegalRe(isFileName || false), replacement)
|
.replace(illegalRe(isFileName || false), replacement)
|
||||||
.replace(controlRe, replacement)
|
.replace(controlRe, replacement)
|
||||||
.replace(reservedRe, replacement)
|
.replace(reservedRe, replacement)
|
||||||
|
|||||||
Reference in New Issue
Block a user