Part of my [[base/Obsidian/Obsidian]] workflow is refactoring notes from a parent note to a child note. When doing so, I want two things to happen:
1. The parent note's link is set as the "up" property of the child note
2. The child note inherits the "topic" property of the parent note
> See [[folders in obsidian]] for more detail on how I use these properties to structure my vault.
To allow property inheritance, I wrote a simple [[JavaScript]] function that is executed by [[Templater]] when I use [[Note Refactor]] to extract highlighted text to a new note. This script will allow you to copy any property from the parent note.
First, create a `Scripts/` folder in your vault. In Templater's settings, select this as the "Script files folder location." While you're there, also turn on "Trigger Templater on new file creation".
Open the folder in Windows Explorer, then open the `Scripts/` folder in your IDE of choice (you cannot create this file in Obsidian as Obsidian only creates Markdown files).
Create a new file named `inherit_property.js` (use this exact name unless you are comfortable with Templater syntax) and paste in the below code:
```javascript
function get_prop_from_parent(tp, property, parentNoteLink) {
const fileFolderPath = tp.file.folder(true);
const parentNotePath = parentNoteLink.replace(/\[\[|\]\]/g, '').trim();
const parentNote = app.vault.getAbstractFileByPath(fileFolderPath + "/" + parentNotePath + ".md");
const defaultIfNotFound = undefined;
if(!parentNote){
console.log("Parent note not found");
return defaultIfNotFound;
}
const metadata = app.metadataCache.getFileCache(parentNote);
if (!metadata || !metadata.frontmatter) {
return defaultIfNotFound; // default value if frontmatter is not found
}
const value = metadata.frontmatter[property];
return value !== undefined ? value : defaultIfNotFound;
}
module.exports = get_prop_from_parent;
```
In Note Refactor's settings, populate the "Refactored note template" as:
```md
---
topic: undefined
parent: "{{link}}"
---
{{new_note_content}}
```
Also set the "Default location for new notes" as "Same folder as current file". This is important as the script will only work if the parent and child notes are in the same folder.
You can extend this functionality to inherit any property from the parent note
> [!Warning]
> This script constructs the path using `+ "/" +`, which may cause issues on some operating systems, but oddly seems to work on my Windows machine. Try `path.join()` instead if you have issues (although this breaks for me).
>
> In fact, `tp.file.path()` seems to produce incoherent results like `C:\Users\~\Obsidian\dev_notes/Linear Algebra/augmented matrix.md`, mixing forward and backward slashes.
> [!Tip]- Additional Resources
> - [Inherit or pass on Metadata Values from Active File when creating New...](https://forum.obsidian.md/t/inherit-or-pass-on-metadata-values-from-active-file-when-creating-new-file/60522/3): Obsidian Help Forums
> - [Creating backlinks in Templater](https://forum.obsidian.md/t/creating-backlinks-in-templater/53256/7): Obsidian Help Forums