Feel free to use these scripts in your own projects.
I am not a programmer so I can't garentee this won't break anything.
Cpage refers to the template that includes the summary header
<%* | |
// Check if this a new file. If it is not a new file then throw a error to stop the script | |
let filename = await tp.file.title | |
if(!(filename.startsWith("Untitled"))) | |
{console.log("This is a existing file! Stopping the script before it overwrites data. Use alt + n to properly create Cpages.") | |
tp.FakeModuleToCauseError.ErrorPleaseAndThxU() | |
} | |
//prompt user for title | |
const title = await tp.system.prompt("Title"); | |
const alias = title; | |
//load all of the files in the vault starting at t.path.startsWith("parent folder/") | |
//This code should be a function with args but I ain't learning proper JavaScript. | |
const folders = this.app.vault.getAllLoadedFiles().filter(i => i.children).filter(t => t.path.startsWith("5_Collections/")).map(folder => folder.path); | |
//prompt user to choose a folder from the files load | |
var folder = await tp.system.suggester(folders, folders); | |
//if user doesn't pick a folder tell them to create a folder. | |
if (folder == null) { | |
folder = await tp.system.prompt("Create new file (no / on last)"); | |
folder = ("5_Collections/" + folder); | |
} | |
// if either the title or folder is not given don't do anything. otherwise create file. | |
if (folder == null || title == null) {} else{ | |
await tp.file.rename(`${title}`); | |
await tp.file.move(`/${folder}/${title}`); | |
var newtitle = `${title}` + ' for ' + tp.file.folder(); | |
await tp.file.rename(newtitle) | |
} | |
//After creating the YAML section activate the List Summary Links template. | |
%>--- | |
aliases: | |
- <%alias%> | |
Type: Collection | |
Discipline: | |
Subdiscipline: | |
Tags: | |
cssclasses: Collection | |
--- | |
<%tp.file.include('[[List Summary Links]]')%> |
<%*
// Check if this a new file. If it is not a new file then throw a error to stop the script let filename = await tp.file.title if(!(filename.startsWith("Untitled"))) {console.log("This is a existing file! Stopping the script before it overwrites data. Use alt + n to properly create Cpages.") tp.FakeModuleToCauseError.ErrorPleaseAndThxU() }
//prompt user for title const title = await tp.system.prompt("Title"); const alias = title;
//load all of the folders in the vault that are inside the folder t.path.startsWith("parent folder/"). (Change "4_Cpages" to the desired top level folder to search). //This code should be a function with args but I ain't learning proper JavaScript. const folders = this.app.vault.getAllLoadedFiles().filter(i => i.children).filter(t => t.path.startsWith("4_Cpages/")).map(folder => folder.path);
//prompt user to choose a folder from the folders loaded var folder = await tp.system.suggester(folders, folders);
//if user doesn't pick a folder tell them to create a folder. And then place that file in the 4_Cpages folder.
if (folder == null) {
folder = await tp.system.prompt("Create new folder (no / on start or last)");
folder = ("4_Cpages/" + folder);
}
// if either the title or folder is not given don't do anything. otherwise create file and add for "Parent folder" to the end of the title.
if (folder == null || title == null) {} else{
await tp.file.rename(${title}
);
await tp.file.move(/${folder}/${title}
);
var newtitle = ${title}
+ ' for ' + tp.file.folder();
await tp.file.rename(newtitle)
}
%>--- aliases:
cssclasses: Cpage
This is the embedded display body. Everything here will be shown when you embed using
![[<% newtitle %>#<% newtitle %> B <% alias %>]]
.
This is the main body of the page. This is not shown in the embedding
Table without id file.link as "in collections"
FROM [[#]] AND "5_Collections"
<%* // Initialize loop console.log('Starting loop for the list summary links template.') var endscript = 'false' var ListLinks = [] while(endscript == 'false'){ if(endscript == 'false'){ // Ask the user to pick a file from the vault. if null is returned then catch the error and stop the script. try { var file = (await tp.system.suggester((item) => item.basename, app.vault.getMarkdownFiles())).basename; if(file == null){ throw new Error('User cancelled'); } } catch (error) { console.log('An error occurred:', error); endscript = 'true'; console.log('End the loop. The above error can be ignored.') }
// If endscript is false then execute the code. if(endscript =='false'){ //Get the frontmatter data for this file. And save the index which points to Type: var file_frontmatter = this.app.metadataCache.getFileCache( tp.file.find_tfile(file) )?.frontmatter || {} var Ary = Object.entries(file_frontmatter) Ary = Ary[1].toString()
// If the file is a Cpage than Link to its Summary Header. Otherwise Just link the entire file. if(Ary == ['Type', 'Cpage'].toString()) { var file_frontmatter = this.app.metadataCache.getFileCache( tp.file.find_tfile(file) )?.frontmatter || {} var Ary1 = Object.entries(file_frontmatter) Ary1 = Ary1[0].toString() alias = Ary1.split(',')[1]
//Prompt user to write display title for the link. If they hit esc than use the default name.
name = await tp.system.prompt("Write display name for link. Esc for default")
if(name == 'null'){
name = alias}
var link = await "[[" + file + "#" + file + " B " + alias + "|" + name + "]]"}
else{ name = await tp.system.prompt("Write display name for link. Esc for default") var link = await "[[" + file + "]]"}
console.log(link) var formatedlink = '!'+link + '\n\n' ListLinks.push(formatedlink) } } } console.log('Loop finished')
%><% ListLinks.join('') %>
<%*
// Ask the user to pick a file from the vault. const file = (await tp.system.suggester((item) => item.basename, app.vault.getMarkdownFiles())).basename
//Get the frontmatter data for this file. And save the index which points to the Type: parameter. var file_frontmatter = this.app.metadataCache.getFileCache( tp.file.find_tfile(file) )?.frontmatter || {}; var Ary = Object.entries(file_frontmatter) Ary = Ary[1].toString()
// If the file is a Cpage than Link to its Summary Header. Otherwise Just link the entire file. if(Ary == ['Type', 'Cpage'].toString()) { var file_frontmatter = this.app.metadataCache.getFileCache( tp.file.find_tfile(file) )?.frontmatter || {}; var Ary1 = Object.entries(file_frontmatter) Ary1 = Ary1[0].toString() alias = Ary1.split(',')[1]
// Prompt the user to write the display title of the link. If they hit esc than use the name as an alias
name = await tp.system.prompt("Write display name for link")
if(name == 'null'){
name = alias}
var link = await "[[" + file + "#" + file + " B " + alias + "|" + name + "]]"}
else{ // This file is not a Cpage so just link the entire file. name = await tp.system.prompt("Write display name for link") var link = await "[[" + file + "]]"}
%><% link %>