How to automatically transfer tags and frontmatter to child notes when I split a note?

What I’m trying to do

I need to automatically transfer tags and frontmatter fields (like tags and mocs) to child notes when I split a note using Obsidian’s Note Refactor and Templater plugins.

For instance, if my main note has this frontmatter:


tags:

  • #tag1
  • #tag2

mocs:

  • “[[MOC1]]”
  • “[[MOC2]]”

I want each new child note created during the split to include these same tags and mocs fields automatically, so I don’t have to add them manually each time.

Things I have tried

I’ve experimented with Templater syntax to pull in these fields but run into errors (I am using chat gpt, I am not a coder). I’ve also searched the forums for terms like “automatically transfer frontmatter on note split,” but I haven’t found a clear solution that allows this automatic inheritance.

Question

How can I set up Templater and Note Refactor so that tags and mocs fields are automatically transferred to child notes when I split a note?

(I am open to any insight for automatic inheritance of metadata fields)

SOLVED BY MYSELF after hours of struggle!

Here’s a tutorial on transferring metadata properties (e.g., type and tags) from a parent to a child note in Obsidian, using Note Refactor and Templater with a custom User Function. This method ensures consistent metadata inheritance by leveraging Templater’s ability to extract metadata and apply it dynamically.

Overview

By creating a Templater User Function, this setup fetches metadata directly from the parent note. We use this approach because Note Refactor does not reliably support templater functions like tp.frontmatter, possibly due to backend processing timing or metadata caching issues.

Step-by-Step Guide

1. Prepare the Parent Note Frontmatter

Set up the parent note’s frontmatter as shown below:

---
date: {{date:YYYY-MM-DD}} 
modified: 
title: "note about something"
source: "link to web article"
aliases:
  -
  -
type: "article"
priority: ["low", "high"]
project: ["web2", "web3", "other"]
tags: ["#tag1", "#tag2"]
definition: "core idea"
---

This is where your metadata (e.g., type and tags) will originate.

2. Set Up the Templater User Function

To retrieve the parent file’s metadata accurately, create a Templater User Function in JavaScript. Save it as getOriginalFilePath.js in the .obsidian/plugins/templater/scripts/ folder:

module.exports = async (title) => {
    const file = app.vault.getAbstractFileByPath(title);
    return file ? file.path : null;
};

The function getOriginalFilePath.js fetches the path of the specified note title, allowing you to retrieve metadata from any given note.

3. Create the Child Note Template

With Templater’s User Function ready, use this template for the child note. It pulls type and tags from the parent note:

<%*
const originalTitle = "{{title}}"; // Use the note's title dynamically
const originalFile = await tp.user.getOriginalFilePath(originalTitle);
const originalMetadata = app.metadataCache.getFileCache(app.vault.getAbstractFileByPath(originalFile))?.frontmatter;

const type = originalMetadata?.type || "permanent";
const tags = originalMetadata?.tags ? JSON.stringify(originalMetadata.tags).replace(/[\[\]"]/g, "") : "";
-%>

---
date: {{date:YYYY-MM-DD}} 
title: "[[{{new_note_title}}]]"
source: "{{link}}"
type: "<%= type %>"
priority: ["low", "high"]
project: ["web2", "web3", "other"]
tags: ["<%= tags %>"]
---

# original content:
{{new_note_content}}

4. Applying the Template with Note Refactor

To transfer content:

  1. Highlight the desired content in your parent note.
  2. Run Note Refactor and select this child note template.
  3. The child note will inherit type and tags from the parent frontmatter.

Tips and Warnings

  • Dynamic Titles: Make sure {{title}} accurately reflects the title of the parent note for proper metadata retrieval.
  • Metadata Caching: If you modify the parent note, refresh Obsidian’s metadata cache to apply updates.
  • Debugging: Ensure that getOriginalFilePath.js is saved in the correct path and referenced correctly. Misplaced files or incorrect paths can cause Templater errors.

This setup ensures your child notes are consistently tagged and classified without requiring manual input each time.

Here is the correct tutorial due to some mistakes:

Here’s the full tutorial on transferring YAML frontmatter properties from a parent note to a child note using Note Refactor and Templater in Obsidian.

Overview

In this tutorial, you’ll learn to transfer metadata from a parent note’s frontmatter to a child note’s frontmatter in Obsidian. This process leverages Templater User Functions and Note Refactor to reliably handle metadata fields like type and tags that might otherwise encounter issues due to caching or timing conflicts.

Prerequisites

  1. Templater and Note Refactor plugins must be installed and enabled in Obsidian.
  2. Basic understanding of YAML frontmatter and metadata in Obsidian.

Why Use a User Function?

Using Templater’s tp functions within Note Refactor can lead to inconsistent results due to caching or backend processes. Instead, we define a user function to retrieve and process metadata directly, ensuring accurate and stable transfers.

Example Frontmatter Structure

We’ll focus on transferring the type and tags fields, which are typically in the parent note’s YAML frontmatter.

Parent Note Frontmatter Example

---
date: {{date:YYYY-MM-DD}} 
title: "note about something"
source: "link to web article"
aliases:
  -
  -
type: "article"  # Type metadata to be transferred
priority: ["low", "high"]
project: ["web2", "web3", "other"]
tags: ["#tag1", "#tag2"]  # Tags to be transferred
definition: "core idea"
---

Child Note Frontmatter Template

---
date: {{date:YYYY-MM-DD}} 
title: "[[{{new_note_title}}]]"
source: "{{link}}"
type: "<%= type %>"  # Insert type if available; leave empty if not
tags:
<%* tags.forEach(tag => { tR += `  - "${tag}"\n`; }); %>  # Each tag formatted on a new line
---
  
# original content:
{{new_note_content}}

Step-by-Step Guide

Step 1: Create the Templater User Function

Save the following JavaScript code as getOriginalFilePath.js in your vault (e.g., in .obsidian/plugins/templater/scripts/):

function getOriginalFilePath(targetTitle) {
    // Retrieve all markdown files in the vault
    const files = app.vault.getMarkdownFiles();
    
    // Search for a file that matches the target title exactly
    const exactFile = files.find(f => f.basename === targetTitle);
    
    // Return the path if found; otherwise, indicate no match
    return exactFile ? exactFile.path : "Original note not found";
}

module.exports = getOriginalFilePath;

Step 2: Reference the User Function in the Template

Add the following Templater script to your child note template:

<%*
const originalTitle = "{{title}}"; // Set the title of the parent note dynamically
const originalFile = await tp.user.getOriginalFilePath(originalTitle); // Locate parent note’s path
const originalMetadata = app.metadataCache.getFileCache(app.vault.getAbstractFileByPath(originalFile))?.frontmatter; // Access parent metadata

// Get tags if defined, otherwise default to an empty array
const tags = originalMetadata && Array.isArray(originalMetadata.tags) ? originalMetadata.tags : [];

// Get type if defined, or leave empty
const type = originalMetadata?.type || "";
-%>

---
date: {{date:YYYY-MM-DD}} 
title: "[[{{new_note_title}}]]"
source: "[[{{title}}]]"
type: "<%= type %>" // Inserts 'type' from parent or leaves blank
tags:
<%* tags.forEach(tag => { tR += `  - "${tag}"\n`; }); %> // Formats each tag on a new line
---

Step 3: Use the Template with Note Refactor

  1. Create a new note in your vault, containing the frontmatter structure and content.
  2. Use Note Refactor to extract a section of your note into a new note.
  3. When prompted, select the child note template you just created. The template will automatically pull in type and tags from the parent note’s frontmatter.

Tips and Warnings

  • Caching Delay: Be aware that app.metadataCache can sometimes require a moment to refresh. If data isn’t appearing, try reloading Obsidian or manually refreshing metadata.
  • User Function Requirement: Templater’s tp functions alone may not work due to timing issues. By defining a user function (getOriginalFilePath), metadata can be reliably retrieved from the parent note.
  • Testing: After setting up the template, test it on a sample note to verify data accuracy before wide usage.

Following these steps, you’ll have a reliable way to transfer metadata properties from parent to child notes, ensuring consistency across your vault!

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