From b58c02b6d04afcb906f57c0e631c1b478b8ff674 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Thu, 6 Feb 2025 10:20:31 +0100 Subject: [PATCH] Issue: Stripping underscore in default filename #914 --- CHANGELOG.md | 12 ++++++++++++ src/helpers/ArticleHelper.ts | 2 +- src/helpers/Sanitize.ts | 14 ++++++++------ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d2debc1..603e0ae7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 25334a09..e42e4251 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -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 }` ); diff --git a/src/helpers/Sanitize.ts b/src/helpers/Sanitize.ts index 088142b7..9162b944 100644 --- a/src/helpers/Sanitize.ts +++ b/src/helpers/Sanitize.ts @@ -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); }