From d49fb103ba85ace7c40dd03f1737014d7ff11a24 Mon Sep 17 00:00:00 2001 From: Kishiin <47154137+Tesselay@users.noreply.github.com> Date: Thu, 18 Apr 2024 03:05:50 +0200 Subject: [PATCH 1/3] Fix for #219 - Folder path separators are incorrectly replaced (#224) * Add separate illegal char regex for folders * Fix correct folder path separators being replaced Issue #219 --- src/__tests__/path_validation.spec.ts | 34 +++++++++++++-------------- src/main.ts | 7 +++--- src/util.ts | 11 ++++++--- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/__tests__/path_validation.spec.ts b/src/__tests__/path_validation.spec.ts index d5a3067..31277d1 100644 --- a/src/__tests__/path_validation.spec.ts +++ b/src/__tests__/path_validation.spec.ts @@ -1,7 +1,7 @@ import * as fs from 'fs' import { - ILLEGAL_CHAR_REGEX, - replaceIllegalChars, + ILLEGAL_CHAR_REGEX_FILE, + replaceIllegalCharsFile, REPLACEMENT_CHAR, } from '../util' @@ -22,56 +22,56 @@ const expectedManualIllegalChars: string[] = [ // ZERO WIDTH JOINER and SOFT HYPHEN const expectedInvisibleChars: string[] = ['­', '‍'] -describe('replaceIllegalChars() removes all expected characters', () => { +describe('replaceIllegalCharsFile() removes all expected characters', () => { test.each(expectedManualIllegalChars)( 'Illegal character "%s" is removed', (character) => { const input = `this${character}string` - const output = replaceIllegalChars(input) + const output = replaceIllegalCharsFile(input) expect(output).not.toContain(character) }, ) }) -describe('replaceIllegalChars() function replaces illegal characters with replacement char', () => { +describe('replaceIllegalCharsFile() function replaces illegal characters with replacement char', () => { test.each(expectedManualIllegalChars)( "Illegal character '%s' is replaced", (char) => { const input = `this${char}string` const expectedOutput = `this${REPLACEMENT_CHAR}string` - const output = replaceIllegalChars(input) + const output = replaceIllegalCharsFile(input) expect(output).toEqual(expectedOutput) }, ) }) -describe('replaceIllegalChars() function does not modify string without illegal characters', () => { +describe('replaceIllegalCharsFile() function does not modify string without illegal characters', () => { test.each(['this_is_a_valid_string', 'this is a valid string'])( "String '%s' is not modified", (input) => { - const output = replaceIllegalChars(input) + const output = replaceIllegalCharsFile(input) expect(output).toEqual(input) }, ) }) -describe('replaceIllegalChars() function handles empty string', () => { +describe('replaceIllegalCharsFile() function handles empty string', () => { test('Empty string is not modified', () => { const input = '' - const output = replaceIllegalChars(input) + const output = replaceIllegalCharsFile(input) expect(output).toEqual(input) }) }) -describe('replaceIllegalChars() function replaces all occurrences of illegal characters', () => { +describe('replaceIllegalCharsFile() function replaces all occurrences of illegal characters', () => { test.each(expectedManualIllegalChars)( "Illegal character '%s' is replaced", (char) => { const input = `${char}foo${char}bar` const expectedOutput = `${REPLACEMENT_CHAR}foo${REPLACEMENT_CHAR}bar` - const output = replaceIllegalChars(input) + const output = replaceIllegalCharsFile(input) expect(output).toEqual(expectedOutput) - expect(output.match(ILLEGAL_CHAR_REGEX)).toBeNull() + expect(output.match(ILLEGAL_CHAR_REGEX_FILE)).toBeNull() }, ) }) @@ -82,7 +82,7 @@ describe('file system behavior with non-alphanumeric characters not in the illeg (_, i) => String.fromCharCode(i + 32), ) .filter((char) => !/^[a-zA-Z0-9]+$/.test(char)) - .map(replaceIllegalChars) + .map(replaceIllegalCharsFile) test.each(nonAlphanumericCharactersWithoutIllegal)( "File system allows creation of file with character '%s'", @@ -101,15 +101,15 @@ describe('file system behavior with non-alphanumeric characters not in the illeg ) }) -describe('replaceIllegalChars() function removes all occurrences of invisible characters', () => { +describe('replaceIllegalCharsFile() function removes all occurrences of invisible characters', () => { test.each(expectedInvisibleChars)( "Invisible character '%s' is replaced", (char) => { const input = `${char}foo${char}bar` const expectedOutput = 'foobar' - const output = replaceIllegalChars(input) + const output = replaceIllegalCharsFile(input) expect(output).toEqual(expectedOutput) - expect(output.match(ILLEGAL_CHAR_REGEX)).toBeNull() + expect(output.match(ILLEGAL_CHAR_REGEX_FILE)).toBeNull() }, ) }) diff --git a/src/main.ts b/src/main.ts index 4f1e028..8bbd097 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,7 +23,8 @@ import { parseDateTime, parseFrontMatterFromContent, removeFrontMatterFromContent, - replaceIllegalChars, + replaceIllegalCharsFile, + replaceIllegalCharsFolder, setOrUpdateHighlightColors, } from './util' import { OmnivoreSettingTab } from './settingsTab' @@ -247,7 +248,7 @@ export default class OmnivorePlugin extends Plugin { ) for (const item of items) { - const folderName = replaceIllegalChars( + const folderName = replaceIllegalCharsFolder( normalizePath(render(item, folder, this.settings.folderDateFormat)), ) const omnivoreFolder = @@ -274,7 +275,7 @@ export default class OmnivorePlugin extends Plugin { fileAttachment, ) // use the custom filename - const customFilename = replaceIllegalChars( + const customFilename = replaceIllegalCharsFile( renderFilename(item, filename, this.settings.filenameDateFormat), ) const pageName = `${folderName}/${customFilename}.md` diff --git a/src/util.ts b/src/util.ts index adad48b..99c34ed 100644 --- a/src/util.ts +++ b/src/util.ts @@ -12,7 +12,8 @@ export const REPLACEMENT_CHAR = '-' // On Unix-like systems / is reserved and <>:"/\|?* as well as non-printable characters \u0000-\u001F on Windows // credit: https://github.com/sindresorhus/filename-reserved-regex // eslint-disable-next-line no-control-regex -export const ILLEGAL_CHAR_REGEX = /[<>:"/\\|?*\u0000-\u001F]/g +export const ILLEGAL_CHAR_REGEX_FILE = /[<>:"/\\|?*\u0000-\u001F]/g +export const ILLEGAL_CHAR_REGEX_FOLDER = /[<>"\\|?*\u0000-\u001F]/g export interface HighlightPoint { left: number @@ -103,8 +104,12 @@ export const unicodeSlug = (str: string, savedAt: string) => { ) } -export const replaceIllegalChars = (str: string): string => { - return removeInvisibleChars(str.replace(ILLEGAL_CHAR_REGEX, REPLACEMENT_CHAR)) +export const replaceIllegalCharsFile = (str: string): string => { + return removeInvisibleChars(str.replace(ILLEGAL_CHAR_REGEX_FILE, REPLACEMENT_CHAR)) +} + +export const replaceIllegalCharsFolder = (str: string): string => { + return removeInvisibleChars(str.replace(ILLEGAL_CHAR_REGEX_FOLDER, REPLACEMENT_CHAR)) } export function formatDate(date: string, format: string): string { From da17fdf94105507c1e78efcae5f7b2af5955eb7e Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 18 Apr 2024 09:16:11 +0800 Subject: [PATCH 2/3] fix lint --- .eslintrc | 3 ++- src/util.ts | 8 ++++++-- tsconfig.json | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.eslintrc b/.eslintrc index bab550a..a8d6e4b 100644 --- a/.eslintrc +++ b/.eslintrc @@ -18,6 +18,7 @@ "no-prototype-builtins": "off", "@typescript-eslint/no-empty-function": "off", "@typescript-eslint/no-extra-semi": "off", - "semi": [2, "never"] + "semi": [2, "never"], + "no-control-regex": 0 } } diff --git a/src/util.ts b/src/util.ts index 99c34ed..5bb2066 100644 --- a/src/util.ts +++ b/src/util.ts @@ -105,11 +105,15 @@ export const unicodeSlug = (str: string, savedAt: string) => { } export const replaceIllegalCharsFile = (str: string): string => { - return removeInvisibleChars(str.replace(ILLEGAL_CHAR_REGEX_FILE, REPLACEMENT_CHAR)) + return removeInvisibleChars( + str.replace(ILLEGAL_CHAR_REGEX_FILE, REPLACEMENT_CHAR), + ) } export const replaceIllegalCharsFolder = (str: string): string => { - return removeInvisibleChars(str.replace(ILLEGAL_CHAR_REGEX_FOLDER, REPLACEMENT_CHAR)) + return removeInvisibleChars( + str.replace(ILLEGAL_CHAR_REGEX_FOLDER, REPLACEMENT_CHAR), + ) } export function formatDate(date: string, format: string): string { diff --git a/tsconfig.json b/tsconfig.json index f335682..2f78845 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,7 @@ "isolatedModules": true, "strictNullChecks": true, "esModuleInterop": true, - "lib": ["ES2021","ES2021.String", "DOM"] + "lib": ["ES2021", "ES2021.String", "DOM"] }, "include": ["src/**/*.ts"] } From 5d09b9f0cb7374fcfc42e91795926858df515df3 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 18 Apr 2024 10:37:40 +0800 Subject: [PATCH 3/3] add tests --- src/__tests__/path_validation.spec.ts | 27 +++++++++++++++++++++++++-- src/util.ts | 2 +- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/__tests__/path_validation.spec.ts b/src/__tests__/path_validation.spec.ts index 31277d1..e6fdec1 100644 --- a/src/__tests__/path_validation.spec.ts +++ b/src/__tests__/path_validation.spec.ts @@ -2,11 +2,11 @@ import * as fs from 'fs' import { ILLEGAL_CHAR_REGEX_FILE, replaceIllegalCharsFile, + replaceIllegalCharsFolder, REPLACEMENT_CHAR, } from '../util' -const expectedManualIllegalChars: string[] = [ - '/', +const expectedManualIllegalCharsInFolderName: string[] = [ '\\', '?', '*', @@ -19,9 +19,32 @@ const expectedManualIllegalChars: string[] = [ '\u001F', ] +// Adding forward slash too which is not allowed in file names +const expectedManualIllegalChars = + expectedManualIllegalCharsInFolderName.concat(['/']) + // ZERO WIDTH JOINER and SOFT HYPHEN const expectedInvisibleChars: string[] = ['­', '‍'] +describe('replaceIllegalCharsFolder() does not replace forward slash', () => { + test('Forward slash is not replaced', () => { + const input = 'this/that' + const output = replaceIllegalCharsFolder(input) + expect(output).toEqual(input) + }) +}) + +describe('replaceIllegalCharsFolder() removes all expected characters', () => { + test.each(expectedManualIllegalCharsInFolderName)( + 'Illegal character "%s" is removed', + (character) => { + const input = `this${character}string` + const output = replaceIllegalCharsFolder(input) + expect(output).not.toContain(character) + }, + ) +}) + describe('replaceIllegalCharsFile() removes all expected characters', () => { test.each(expectedManualIllegalChars)( 'Illegal character "%s" is removed', diff --git a/src/util.ts b/src/util.ts index 5bb2066..1f1e29d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -13,7 +13,7 @@ export const REPLACEMENT_CHAR = '-' // credit: https://github.com/sindresorhus/filename-reserved-regex // eslint-disable-next-line no-control-regex export const ILLEGAL_CHAR_REGEX_FILE = /[<>:"/\\|?*\u0000-\u001F]/g -export const ILLEGAL_CHAR_REGEX_FOLDER = /[<>"\\|?*\u0000-\u001F]/g +export const ILLEGAL_CHAR_REGEX_FOLDER = /[<>:"\\|?*\u0000-\u001F]/g export interface HighlightPoint { left: number