Inherit or pass on Metadata Values from Active File when creating New File

Sorry it took me so long, but here is a template to trigger your creative juices:

<%*
const dv = this.app.plugins.plugins["dataview"].api

const curr = tp.config.target_file
const dvCurr = dv.page( curr.path )
const baseFolder = app.vault.getAbstractFileByPath(dvCurr.file.folder)

const sel = tp.file.selection()
if ( !sel ) {
  windows.alert("Please select some text to be extracted")
  return;
}

const newFile = await tp.system.prompt("New file name")

const content = 
`---
tag: project
toBeInherited: ${ tp.frontmatter["toBeInherited"] }
---
Goal:: ${ dvCurr.goal }

${ sel }
`
/* 
console.log("Content", content)
console.log(curr)
console.log( dv.page(curr.path) )
*/

await tp.file.create_new(content, newFile, true, baseFolder)
tR = `[[${ newFile }]]`

_%>

To use it, change variables as you feel like it when entering into a template of its own. Then when you’re in a project file you’d want to extract stuff from, select the text to be extracted, and trigger the template. It’ll create a new file, fill it with content, leave a link to the new file instead of the selection, and open the new file for more editing.

The template utilises both Templater with the tp.frontmatter[ ... ], and dataview to pull values from the originating file (found in tp.template.target_file. It places the new file in the same folder as the file you’re extracting from. (If you change the true to false in the create statement, it’ll not go to the new file directly)

7 Likes