Skip to content

Conversation

@Spydarlee
Copy link
Contributor

@Spydarlee Spydarlee commented Aug 30, 2025

Hi there - thanks for making such a great plugin!

I was running into some issues when trying to create markdown files based on media with titles that contain invalid filename characters (such as ":" and other punctuation).

This small change strips out most invalid characters, but replaces colons with hyphens, and replaces double quotes with single quotes, to try and retain the original intention as much as possible.

The original, unaltered, title is/can still be added to the frontmatter as per usual.

image


cleanFileName(fileName: string) {
const invalidCharsRegex = /\™||,|#|\[|\]|\||\^|\<|\>|\?|\*|\\|\//g;
return fileName.replaceAll(invalidCharsRegex, '').replaceAll(/"/g, "'").replaceAll(/:/g, ' -');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For single characters, you don't need a regexp, e.g. .replaceAll(':', ' - ')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good spot, fixed, thanks!

@mProjectsCode
Copy link
Owner

On further thought, I would prefer something like the following:

// illegal characters in the form `[illegal_character, replacement][]`
const ILLEGAL_FILENAME_CHARACTERS = [["\/", ""], ["<", ""], [">", ""], [":", " - "], ["\"", "'"], ["\\", ""], ["|", " - "], ["?", ""], ["*", ""], ["[", "("], ["]", ")"], ["^", ""], ["#", ""]];

// ...

cleanFileName(fileName: string): string {
    const cleanedFileName = ILLEGAL_FILENAME_CHARACTERS.reduce((str, char) => str.replaceAll(char[0], char[1]), fileName);
    // remove all duplicate whitespace in the file name
    return cleaneFileName.replaceAll(/ +/g, " ");
}

(Not tested)

@Spydarlee
Copy link
Contributor Author

Refactored! I tested against a bunch of movies and games with unusual characters in their titles:

[REC] 3: Genesis ➡ (REC) 3 - Genesis
Pokémon Ruby/Sapphire ➡ Pokémon Ruby-Sapphire
#Home ➡ Home

Seems to work well. 🙂

@mProjectsCode mProjectsCode merged commit 535987e into mProjectsCode:master Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants