Templater: Invalid characters in the name of a note

Things I have tried

This code copies and pastes information from a Youtube video.

<%*
let url = await tp.system.clipboard();
let page = await tp.obsidian.request({url});
let p = new DOMParser();
let doc = p. parseFromString(page, “text/html”);
let $ = s => doc.querySelector(s);
let qcFileName = $(“meta[property=‘og:title’]”).content;
titleName = “:clapper: " + qcFileName;
await tp.file.move(”/004_Videos/" + titleName);
let duration2 = $(“meta[itemprop=‘duration’]”).content.slice(2);
let duration1 = duration2.replace(/M/gi, " Min ") ;
let duration = duration1.replace(/S/gi, " Sec ") ;
%>

  • Title: *<%$(“meta[property=‘og:title’]”).content %>
  • Type: [[+]]
  • URL: <%$(“link[rel=‘shortLinkUrl’]”).href %>
  • Channel/Host: <%$(“link[itemprop=‘name’]”).getAttribute(“content”) %>
  • Publish Date: <%$(“meta[itemprop=‘uploadDate’]”).content.slice(0, 4) %>
  • Duration: <% duration %>

<% qcFileName %>

When I apply it through Templater it works very well… but when the title of a video has a special character the following console error appears: File name cannot contain any of the following characters: * " \ / < > : | ?

What additional line of code can help to exclude these characters?

What I’m trying to do

The only thing I could do is omit the variable that contains the original title; This is the only way I can skip the error; I would like to know what lines of code can be inserted so that everything Works automatically.

<%*
let url = await tp.system.clipboard();
let page = await tp.obsidian.request({url});
let p = new DOMParser();
let doc = p. parseFromString(page, “text/html”);
let $ = s => doc.querySelector(s);
let qcFileName = $(“meta[property=‘og:title’]”).content;
titleName = “:clapper: Change Title Manually”; :smiley: <— The Error Gone----<<
await tp.file.move(“/004_Videos/” + titleName);
let duration2 = $(“meta[itemprop=‘duration’]”).content.slice(2);
let duration1 = duration2.replace(/M/gi, " Min ") ;
let duration = duration1.replace(/S/gi, " Sec ") ;
%>

<% qcFileName %>

> Blockquote

The error you’re getting is not an error before you create the file, so before the tp.file.move() you need to check for and replace all illegal characters.

Either opt for exchanging them with legal characters, or simply remove them.

I appreciate your help, I understand that invalid characters should be filtered, but I don’t know how to do it, I’m not a programmer, I copied the code from another post on github.

I Added the Following line of code and Now Working Fine

qcFileName = qcFileName.replace(/[^\w\s]/gi, ‘_’);

let qcFileName = $(“meta[property=‘og:title’]”).content;
qcFileName = qcFileName.replace(/[^\w\s]/gi, ‘_’); Added this line
titleName = ":clapper: " + qcFileName;

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