When you publish notes from Obsidian to Quartz, file names matter.
A clean file name is easier to read. It is also better for URLs. For example, this file name:
how-to-use-obsidian-templater-for-seo-friendly-filenames
is better than this:
How to Use Obsidian Templater for SEO Friendly Filenames!!!
The first one is simple, lowercase, and safe to use in a URL.
This article shows how to use an Obsidian Templater script that creates a clean file name from either:
- The
titlefield in your frontmatter - The first H1 heading in your note
This is useful if you write in Obsidian and publish your notes later with Quartz.
What this script does
The script reads the title of your note and turns it into a file name.
It checks the note in this order:
- First, it looks for a
titlefield in the frontmatter. - If there is no
title, it looks for the first H1 heading. - If it finds a title, it creates a clean file name.
- Then it renames the current note.
For example, if your note has this frontmatter:
---title: How to Use Obsidian Templater for SEO-Friendly File Names---
the script will rename the file to:
how-to-use-obsidian-templater-for-seo-friendly-file-names
If your note does not have a frontmatter title, the script will use the first H1 instead:
# How to Use Obsidian Templater for SEO-Friendly File Names
This will create the same file name.
The Templater script
Here is the full script:
<%*
let title = null;
// First try to read the title from frontmatter
if (tp.frontmatter && tp.frontmatter.title) {
title = String(tp.frontmatter.title).trim();
}
// If no title found, try to read the H1 from the content
if (!title) {
let content = tp.file.content;
let h1Match = content.match(/^#\s+(.+)$/m);
if (h1Match) {
title = h1Match[1].trim();
}
}
if (!title) {
new Notice("No frontmatter title or H1 found");
return;
}
// slugify: remove symbols, replace spaces with hyphens, and convert to lowercase
let filename = title
.replace(/[^A-Za-z0-9\u4e00-\u9fff\s-]/g, "")
.replace(/\s+/g, "-")
.replace(/-+/g, "-")
.replace(/^-|-$/g, "")
.toLowerCase();
if (!filename) {
new Notice("Generated filename is empty");
return;
}
await tp.file.rename(filename);
new Notice("Renamed to: " + filename);
%>
How to install the script
First, make sure you have the Templater plugin installed in Obsidian.
Open Obsidian, then go to:
Settings → Community plugins
Search for Templater and install it.
After that, enable the plugin.
Next, create a folder for your templates. A common choice is:
Templates
Then go to:
Settings → Templater
Set your template folder location to the folder you created.
Now create a new template file. You can name it:
rename-seo-friendly.md
Paste the script into that file.
How to use the script
Open the note that you want to rename.
Make sure the note has either a title field in the frontmatter or an H1 heading.
Here is an example using frontmatter:
---title: How to Publish Obsidian Notes with Quartz---Your article starts here.
Here is an example using an H1:
# How to Publish Obsidian Notes with QuartzYour article starts here.
Then run the template with Templater.
In Obsidian, open the command palette and search for:
Templater: Open Insert Template modal
Choose your template file.
The script will read the title, create a clean file name, and rename the current note.
How the file name is created
The script uses a simple slug format.
It does four things:
.replace(/[^A-Za-z0-9\u4e00-\u9fff\s-]/g, "")
This removes most symbols. It keeps English letters, numbers, spaces, hyphens, and Chinese characters.
.replace(/\s+/g, "-")
This changes spaces into hyphens.
.replace(/-+/g, "-")
This changes repeated hyphens into one hyphen.
.replace(/^-|-$/g, "")
This removes hyphens from the start and end of the file name.
Finally, this part makes the file name lowercase:
.toLowerCase()
Example
Before running the script, your note may be named:
Untitled.md
And the note content may look like this:
---title: My Simple Guide to Quartz Publishing!---Some article content here.
After running the script, the file will be renamed to:
my-simple-guide-to-quartz-publishing
This creates a clean path for Quartz.
Depending on your Quartz setup, this may become a URL like:
https://example.com/my-simple-guide-to-quartz-publishing
Why this helps when publishing to Quartz
Quartz turns your Obsidian notes into a website. Your file names often become part of your page URLs.
Clean file names help because they are:
- Easier to read
- Easier to share
- Safer for URLs
- More consistent across your site
A clear file name also helps you keep your vault organized.
Things to know
The script renames the current file. It does not change the title inside the note.
It also does not add frontmatter or headings for you. You need to add either a title field or an H1 before running it.
If the script cannot find a title, Obsidian will show this notice:
No frontmatter title or H1 found
If the title exists but cannot create a valid file name, it will show this notice:
Generated filename is empty
Most of the time, this means the title only had symbols or characters that the script removed.
Suggested workflow
When writing a new article, start with a title in frontmatter:
---title: Your Article Title---
Then write your draft as usual.
When you are ready to publish, run the Templater script. The file will be renamed into a clean slug.
This keeps your writing flow simple. You can write with a normal title, then let the script handle the file name before publishing to Quartz.