Drafts tags without spaces

I’m using the “adapted” script below which meets my needs.

However, I’d like to add a tag where variable “name” would have the space between names replaced by “_”. Is this possible?

Eg. If name = John Doe ==> the variable would be John_Doe

Any help? Thanks!!

let f = () => {
	// prompt for title and tags
	let p = Prompt.create();
	p.title = "New book? :)"
	p.message = "Let's go!"
	
	p.addTextField("title", "Book's title", "");
	p.addTextField("name", "Author's name", "");
	p.addTextField("tags", "tags", "books , smartnotes");
	p.addButton("Create");
	
	if (!p.show()) { return false; }
	
	let tags = p.fieldValues["tags"].split(",").map(s => s.trim());
	let name = p.fieldValues["name"];
	let title = p.fieldValues["title"];
	
	if (tags.length == 0 || name.length == 0) {
		alert("Name and at least one tag are required to create a project.");
		return false;
	}
			
	let d = Draft.create();
	for (let tag of tags) {
		d.addTag(tag);
	}
	d.addTag(name)
	
	d.content = `# ${title}
by [[${name}]]

# `;
	
	d.isFlagged = false;
	d.update();
	
	editor.load(d);
		
	return true;
}

if (!f()) {
	context.cancel();
}

If you change

d.addTag(name)

to

d.addTag(name.replaceAll(" ", "_"));

That would change the tag being added to the new draft for an author’s name of “John Smith” from adding a value of “john smith” to a value of “john_smith”. Note the automatic lower-casing in both cases; so neither quite match what you had above where the casing is retained.

By using a replace all, should deal with names where the author uses a middle name too - e.g. John David Smith.

2 Likes

Thanks! It works great.

All in lower case is what I intended for tags anyway! :wink: