Templater - How to add information to YAML frontmatter

I found coding which allows me to rename a note with the title “Untitled” and pasted it into the template. I want to also be able to add information to some frontmatter fields in the same way when creating a note from a template. I am just too new to this to figure it out on my own, so I am hoping someone will/can help me.

This is my template:
<%*
let title = tp.file.title
if (title.startsWith(“Untitled”)) {
title = await tp.system.prompt(“Title”);
await tp.file.rename(title);
}
tR += “—”
%>
title: <%* tR += title %>
created: <% tp.date.now(“YYYMMDDHHmm”) %>
tags:
source:
author:
type:
quote:
paraphrase:
<%* tR +="—" %>

<%* tR += title %>

The items I would to which I would like to add information when I am changing the name of the title are: tags, author, type, quote (eg. “Y”,“N”,“M”), and paraphrase (eg. “Y”,“N”,“M”). I wouldn’t always be adding information to all of them , so I need to be able to skip fields.

Thank you for any help you can give me.

2 Likes

Don’t know if you got any further with this but I’m working on something similar in the gist below.
For some reason, it doesn’t want to preview the whole thing from my gist.

module.exports = (tp, attributes) => {
	if (typeof attributes !== "object") {
		throw new Error("attributes must be an object");
	}
	const { position, ...frontmatter } =
		tp.frontmatter && typeof tp.frontmatter === "object" ? tp.frontmatter : {};
	for (let key in attributes) {
		if (Array.isArray(frontmatter[key]) || Array.isArray(attributes[key])) {
			if (Array.isArray(frontmatter[key]) && Array.isArray(attributes[key])) {
				frontmatter[key] = frontmatter[key].concat(attributes[key]);
			} else if (
				!Array.isArray(frontmatter[key]) &&
				Array.isArray(attributes[key])
			) {
				frontmatter[key] = attributes[key].concat([frontmatter[key]]);
			} else if (
				Array.isArray(frontmatter[key]) &&
				!Array.isArray(attributes[key])
			) {
				frontmatter[key] = frontmatter[key].concat([attributes[key]]);
			} else {
				frontmatter[key] = { ...frontmatter[key], ...attributes[key] };
			}
			frontmatter[key] = Array.from(new Set(frontmatter[key].filter(val => val)));
		} else if (
			typeof frontmatter[key] === "object" &&
			typeof attributes[key] === "object"
		) {
			frontmatter[key] = { ...frontmatter[key], ...attributes[key] };
		} else {
			frontmatter[key] = attributes[key];
		}
	}

	return tp.user.formatted_frontmatter(frontmatter);
};

For this initial frontmatter:

---
page-title: "2022-06-05"
aliases:
  - "2022-06-05"
date: 2022/06/05 13:12:00
tags:
  - 2022/06/05
object_a: {
  a: "A",
  b: "B"
}
not_object: "Z"
---

The following templates produce the following results

<% tp.user.mutate_frontmatter(tp, {tags: "blah"}) %>
---
page-title: "2022-06-05"
aliases:
  - "2022-06-05"
date: "2022/06/05 13:12:00"
tags:
  - "2022/06/05"
  - "blah"
object_a:
  a: "A"
  b: "B"
not_object: "Z"

---
<% tp.user.mutate_frontmatter(tp, {tags: ["blah"]}) %>
---
page-title: "2022-06-05"
aliases:
  - "2022-06-05"
date: "2022/06/05 13:12:00"
tags:
  - "2022/06/05"
  - "blah"
object_a:
  a: "A"
  b: "B"
not_object: "Z"

---
<% tp.user.mutate_frontmatter(tp, {blags: ["blah"]}) %>
---
page-title: "2022-06-05"
aliases:
  - "2022-06-05"
date: "2022/06/05 13:12:00"
tags:
  - "2022/06/05"
object_a:
  a: "A"
  b: "B"
not_object: "Z"
blags:
  - "blah"

---
<% tp.user.mutate_frontmatter(tp, {object_a: {a:"c"}}) %>
---
page-title: "2022-06-05"
aliases:
  - "2022-06-05"
date: "2022/06/05 13:12:00"
tags:
  - "2022/06/05"
object_a:
  a: "c"
  b: "B"
not_object: "Z"

---
<% tp.user.mutate_frontmatter(tp, {object_a: {c:"c"}}) %>
---
page-title: "2022-06-05"
aliases:
  - "2022-06-05"
date: "2022/06/05 13:12:00"
tags:
  - "2022/06/05"
object_a:
  a: "A"
  b: "B"
  c: "c"
not_object: "Z"

---
<% tp.user.mutate_frontmatter(tp, {object_a: "d"}) %>
---
page-title: "2022-06-05"
aliases:
  - "2022-06-05"
date: "2022/06/05 13:12:00"
tags:
  - "2022/06/05"
object_a: "d"
not_object: "Z"

---
<% tp.user.mutate_frontmatter(tp, {not_object: {value: "object now"}}) %>
---
page-title: "2022-06-05"
aliases:
  - "2022-06-05"
date: "2022/06/05 13:12:00"
tags:
  - "2022/06/05"
object_a:
  a: "A"
  b: "B"
not_object:
  value: "object now"

---
1 Like

This is a super-helpful request - if you get it working please post !! I’ve been trying for the same…

The following will work with my script

<% tp.user.mutate_frontmatter(tp, {new_information: "Some information in here"}) %>
---
page-title: "2022-06-05"
aliases:
  - "2022-06-05"
date: "2022/06/05 13:12:00"
tags:
  - "2022/06/05"
new_information: "Some information in here"
---

This should work as a simplified version:

module.exports = (tp, newAttrs) => {
	if (typeof newAttrs !== "object") {
		throw new Error("attributes must be an object");
	}
	const { position, ...frontmatter } = tp.frontmatter && typeof tp.frontmatter === "object" ? tp.frontmatter : {};
	frontmatter = { ...frontmatter, ...newAttrs };
	return frontmatter
};

Total newbie question - do all of the scripts (eg mutute.frontmatter.js) go in the same template ?? Do they reside in a different file ? And i assume each script is preceeded by the ```dataviewjs characters…
Thanks and much appreciated !

Not quite. These are “user scripts” for Templater, not dataviewjs, so no tick symbols or dataviewjs: instead they go in separate Javascript (.js) files. I believe that .js files should only ever have 1 module.exports in them. So the content in the post above yours would go in a file called mutate_frontmatter.js in your folder for user scripts that you’ve specified in the Templater settings tab. Note that you’ll need to create (or at least rename) this mutate_frontmatter.js file outside Obsidian so that it has the correct file extension - Obsidian puts a .md at the end of all files it creates.

The file that calls the script such as in two posts above yours is just any file in Obsidian.

Note: How did I know what name to call the .js file, with the _ and everything? The first line of the file that calls the script has it, which I’ve bolded in the quote below:

1 Like

thank you thank you !!!
would never have known that !

1 Like