Automatically create alias when there is an attempt to name a file with special characters like question marks

I searched and couldn’t find anything that does this. Does anyone know of anything that does or if it would be expected that a plugin can access the functionality to do this?

User Story
As a user of Obsidian,
I want the software to automatically create an alias or sanitized version of a note title when I attempt to name a file with special characters that are problematic for filesystems (like question marks, slashes, or colons),
So that I can name my notes in a way that is meaningful and useful for my later understanding and retrieval without worrying about compatibility or technical restrictions.

3 Likes

There’s the “Templater” plugin that provides note/file management via API calls using JavaScript. For example, you can make a general template that applies to all notes and simply asks you the name of the note before creating it, and with some basic JavaScript code you can sanitize the note name and then create it.

Here’s a basic guide tested on my computer:

  1. Create a template note called “Creation Template” (this will be used by Templater on each new file creation to sanitize the name)
  2. Install Templater, and make sure to check the “Trigger Templater on new file creation” option in the plugin’s settings
  3. Lower on the plugin’s settings check the “Enable Folder Templates” option, then click the “+” button and in the below fields make sure the folder is set to “/” to intercept new file creation in any location of the vault, and in the template field choose the “Creation Template” you created earlier
  4. Close the settings page and type the following code in the Creation Template you created earlier:
<%*
// ask the user for the new note's filename
// and remember old title to turn it into an alias
let title = await tp.system.prompt("Enter file name:");
let old_title = title;

// replace illegal OS characters
title = title.replace(/[:?\/<>"\|\*\\-]/gi, " ").trim();

// find out in which folder the note is being created
// and compute the full path to the new note
let folder = await tp.file.folder(true);
let path = folder.replace(/\/?$/, '/') + title + ".md";

// if the note doesn't exist, create it
// otherwise warn the user in the new note's content
if (!(await tp.file.exists(path)))
	await tp.file.rename(title);
else 
	tR += "File already exists!\n"
_%>
---
aliases: <% old_title %>
---

And that’s it, go anywhere in your vault, click the New Note button and Templater will ask you the file name, stripping any invalid characters before creating it and replacing them with a whitespace (if the sanitized file name already exists, the new note won’t be renamed and instead an error will be inserted in the new note)

P.S. I know, it’s far from perfect but it gets the message across.

[edit: I added the invalid title as an alias as I had forgotten about that part of your request]

2 Likes

Cool. Thanks!
I’ll give that a shot. Will or can it work if I create a new note from the quicklauncher or via a link that doesn’t exist ?

1 Like

It works just fine from the quick launcher, but if you try to create it via a non-existent link reference it will not work because apparently Obsidian does a file name check before creating a note via a link.

1 Like