mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-09 10:22:55 +02:00
Issue: Stripping underscore in default filename #914
This commit is contained in:
@@ -1,5 +1,17 @@
|
||||
# Change Log
|
||||
|
||||
## [10.8.0] - 2025-02-xx
|
||||
|
||||
### ✨ New features
|
||||
|
||||
### 🎨 Enhancements
|
||||
|
||||
### ⚡️ Optimizations
|
||||
|
||||
### 🐞 Fixes
|
||||
|
||||
- [#914](https://github.com/estruyf/vscode-front-matter/issues/914): Fix sanitizing of default filenames with an `_` in it
|
||||
|
||||
## [10.7.0] - 2024-12-31 - [Release notes](https://beta.frontmatter.codes/updates/v10.7.0)
|
||||
|
||||
### 🎨 Enhancements
|
||||
|
||||
@@ -572,7 +572,7 @@ export class ArticleHelper {
|
||||
await mkdirAsync(newFolder, { recursive: true });
|
||||
newFilePath = join(
|
||||
newFolder,
|
||||
`${sanitize(contentType.defaultFileName ?? `index`)}.${
|
||||
`${sanitize(contentType.defaultFileName || `index`, { isFileName: true })}.${
|
||||
fileExtension || contentType.fileType || fileType
|
||||
}`
|
||||
);
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
const illegalRe = /[/?<>\\:*|"!.,;{}[\]()_+=~`@#$%^&]/g;
|
||||
const illegalRe = (isFileName: boolean) =>
|
||||
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 sanitize(input: string, replacement: string) {
|
||||
function sanitize(input: string, replacement: string, isFileName?: boolean) {
|
||||
if (typeof input !== 'string') {
|
||||
throw new Error('Input must be string');
|
||||
}
|
||||
|
||||
const sanitized = input
|
||||
.replace(illegalRe, replacement)
|
||||
.replace(illegalRe(isFileName || false), replacement)
|
||||
.replace(controlRe, replacement)
|
||||
.replace(reservedRe, replacement)
|
||||
.replace(windowsReservedRe, replacement)
|
||||
@@ -19,11 +20,12 @@ function sanitize(input: string, replacement: string) {
|
||||
return sanitized;
|
||||
}
|
||||
|
||||
export default function (input: string, options?: any) {
|
||||
export default function (input: string, options?: { replacement?: string; isFileName?: boolean }) {
|
||||
const replacement = (options && options.replacement) || '';
|
||||
const output = sanitize(input, replacement);
|
||||
const isFileName = options && options.isFileName;
|
||||
const output = sanitize(input, replacement, isFileName);
|
||||
if (replacement === '') {
|
||||
return output;
|
||||
}
|
||||
return sanitize(output, '');
|
||||
return sanitize(output, '', isFileName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user