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
- Templater and Note Refactor plugins must be installed and enabled in Obsidian.
- 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
- Create a new note in your vault, containing the frontmatter structure and content.
- Use Note Refactor to extract a section of your note into a new note.
- 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!