Skip to content

Commit bc6b985

Browse files
committed
Refactor illegal filename character cleanup
1 parent c1b35fc commit bc6b985

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Illegal characters in the form `[illegal_character, replacement][]`
2+
export const ILLEGAL_FILENAME_CHARACTERS = [
3+
['\/', '-'],
4+
['\\', '-'],
5+
['<', ''],
6+
['>', ''],
7+
[':', ' - '],
8+
['"', "'"],
9+
['|', ' - '],
10+
['?', ''],
11+
['*', ''],
12+
['[', '('],
13+
[']', ')'],
14+
['^', ''],
15+
['#', ''],
16+
];

src/utils/MediaTypeManager.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { WikiModel } from '../models/WikiModel';
1212
import type { MediaDbPluginSettings } from '../settings/Settings';
1313
import { MediaType } from './MediaType';
1414
import { replaceTags } from './Utils';
15+
import { ILLEGAL_FILENAME_CHARACTERS } from './IllegalFilenameCharactersList';
1516

1617
export const MEDIA_TYPES: MediaType[] = [
1718
MediaType.Movie,
@@ -75,9 +76,10 @@ export class MediaTypeManager {
7576
return this.cleanFileName(fileName);
7677
}
7778

78-
cleanFileName(fileName: string) {
79-
const invalidCharsRegex = /\™||,|#|\[|\]|\||\^|\<|\>|\?|\*|\\|\//g;
80-
return fileName.replaceAll(invalidCharsRegex, '').replaceAll('"', "'").replaceAll(':', ' -');
79+
cleanFileName(fileName: string): string {
80+
const cleanedFileName = ILLEGAL_FILENAME_CHARACTERS.reduce((str, char) => str.replaceAll(char[0], char[1]), fileName);
81+
// Remove all duplicate whitespace in the file name
82+
return cleanedFileName.replaceAll(/ +/g, ' ');
8183
}
8284

8385
async getTemplate(mediaTypeModel: MediaTypeModel, app: App): Promise<string> {

0 commit comments

Comments
 (0)