Replacing Space Character with Dash - Templater

What I’m trying to do

I have a template that I run on a newly created file that brings up a prompt to type in a file name. I’m looking for a way to convert name (with spaces) I type into the dialog box with name (with dashes). I.E “Some Name” with “Some-Name” The reason why I want to do this is I have always had problems with space characters when performing file operations outside of the application so I want all my files to have names with dashes in between each word.

Also I was wondering if there was a way to convert Upper Case to Lower Case for the file name as well.

Below is my code:

<%*
  let title = tp.file.title
  if (title.startsWith("Untitled")) {
    title = await tp.system.prompt("Title");
  } 
  
  tR += "---"
%>
title:  <%* tR += title %>
created: <% tp.date.now("YYYY-MM-DD HH:mm") %>
author:
categories: 
tags:
---

<% await tp.file.rename(title) %>

Untested code follows:

<%*
  let title = tp.file.title
  if (title.startsWith("Untitled")) {
    title = await tp.system.prompt("Title");  
  } 
  const modTitle = title.replace(/ /g, "-").toLowerCase()

  await tp.file.rename(modTitle)
_%>
---
title:  <% modTitle %>
created: <% tp.date.now("YYYY-MM-DD HH:mm") %>
author:
categories: 
tags:
---

This adds another variable, modTitle, where the title should replace spaces with dash (although I would recommend using _, and not dashes), and then it lowercases the result. This can be retrieved later on in the script through the usage of <% modTitle %>. If you want to use the original title, in your title field, then change that to be <% title %>.

Also note that I moved the rename of the file into the first block, as it kind of belongs in there, and that I use _%> to allow for the entire javascript block to be placed before the actual frontmatter section. That _ will gobble up any newlines so it should work nicely, and looks good!

A post was merged into an existing topic: Alternative bullet color for nested bullets

Thank you, thats close but I wanted the other way around. The file name title to have the dashes, and the yaml title to have the the text captured from the prompt. Also why use underscores instead of dashes?

Obsidian shouldn’t really show the “File Name” as the title of the document up above because the real title should be the YAML header…

Settings > Appearance > Show inline title

Holy shit. Thank you. :-\
Now is there a way to show the yaml title as the file name in the files/folder view?

Never mind there was a carry over from some code and I modified it to fit my needs. I changed the yaml title line to with the prompt variable. Thanks for your help


<%*
  let title = tp.file.title
  if (title.startsWith("Untitled")) {
    title = await tp.system.prompt("Title");  
  } 
  const modTitle = title.replace(/ /g, "-").toLowerCase()

  await tp.file.rename(modTitle)
_%>
---
title:  <%* tR += title %>
created: <% tp.date.now("YYYY-MM-DD HH:mm") %>
author:
categories: 
tags:
---

It sounds like something there might be a plugin for (I don’t know if there is).

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.