Refactor illegal filename character cleanup
This commit is contained in:
parent
c1b35fc895
commit
bc6b985234
2 changed files with 21 additions and 3 deletions
16
src/utils/IllegalFilenameCharactersList.ts
Normal file
16
src/utils/IllegalFilenameCharactersList.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Illegal characters in the form `[illegal_character, replacement][]`
|
||||||
|
export const ILLEGAL_FILENAME_CHARACTERS = [
|
||||||
|
['\/', '-'],
|
||||||
|
['\\', '-'],
|
||||||
|
['<', ''],
|
||||||
|
['>', ''],
|
||||||
|
[':', ' - '],
|
||||||
|
['"', "'"],
|
||||||
|
['|', ' - '],
|
||||||
|
['?', ''],
|
||||||
|
['*', ''],
|
||||||
|
['[', '('],
|
||||||
|
[']', ')'],
|
||||||
|
['^', ''],
|
||||||
|
['#', ''],
|
||||||
|
];
|
||||||
|
|
@ -12,6 +12,7 @@ import { WikiModel } from '../models/WikiModel';
|
||||||
import type { MediaDbPluginSettings } from '../settings/Settings';
|
import type { MediaDbPluginSettings } from '../settings/Settings';
|
||||||
import { MediaType } from './MediaType';
|
import { MediaType } from './MediaType';
|
||||||
import { replaceTags } from './Utils';
|
import { replaceTags } from './Utils';
|
||||||
|
import { ILLEGAL_FILENAME_CHARACTERS } from './IllegalFilenameCharactersList';
|
||||||
|
|
||||||
export const MEDIA_TYPES: MediaType[] = [
|
export const MEDIA_TYPES: MediaType[] = [
|
||||||
MediaType.Movie,
|
MediaType.Movie,
|
||||||
|
|
@ -75,9 +76,10 @@ export class MediaTypeManager {
|
||||||
return this.cleanFileName(fileName);
|
return this.cleanFileName(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanFileName(fileName: string) {
|
cleanFileName(fileName: string): string {
|
||||||
const invalidCharsRegex = /\™|\®|,|#|\[|\]|\||\^|\<|\>|\?|\*|\\|\//g;
|
const cleanedFileName = ILLEGAL_FILENAME_CHARACTERS.reduce((str, char) => str.replaceAll(char[0], char[1]), fileName);
|
||||||
return fileName.replaceAll(invalidCharsRegex, '').replaceAll('"', "'").replaceAll(':', ' -');
|
// Remove all duplicate whitespace in the file name
|
||||||
|
return cleanedFileName.replaceAll(/ +/g, ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
async getTemplate(mediaTypeModel: MediaTypeModel, app: App): Promise<string> {
|
async getTemplate(mediaTypeModel: MediaTypeModel, app: App): Promise<string> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue