From acf9c4490de990069b131873c55bd500e682a32d Mon Sep 17 00:00:00 2001 From: WetHat Date: Sun, 17 Nov 2024 16:58:11 +0100 Subject: [PATCH] Replace illegal characters in filenames with Unicode lookalikes to improve import fidelity. --- src/util.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/util.ts b/src/util.ts index 99c8dad5..05fbfba3 100644 --- a/src/util.ts +++ b/src/util.ts @@ -8,15 +8,31 @@ let windowsTrailingRe = /[\. ]+$/; let startsWithDotRe = /^\./; // Regular expression to match filenames starting with "." let badLinkRe = /[\[\]#|^]/g; // Regular expression to match characters that interferes with links: [ ] # | ^ +const asciiLookalikes: {[key:string]: string} = { + '/' : '╱', + '?':'❓', + '<':'<', + '>':'>', + '\\':'╱', + ':':'꞉', + '*':'✱', + '|':'∣', + '"':'\'', + '[':'⟦', + ']':'⟧', + '#':'#', + '^':'⌃' +}; + export function sanitizeFileName(name: string) { return name - .replace(illegalRe, '') + .replace(illegalRe, c => asciiLookalikes[c] ?? '') .replace(controlRe, '') .replace(reservedRe, '') .replace(windowsReservedRe, '') .replace(windowsTrailingRe, '') .replace(startsWithDotRe, '') - .replace(badLinkRe, ''); + .replace(badLinkRe, c => asciiLookalikes[c] ?? ''); } export function genUid(length: number): string {