Templater - How to add information to YAML frontmatter

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