Skip to content

Instantly share code, notes, and snippets.

@samsoeder
Last active October 19, 2024 18:21
Show Gist options
  • Save samsoeder/6e63b9a7990966b48ecf8040d00d97ea to your computer and use it in GitHub Desktop.
Save samsoeder/6e63b9a7990966b48ecf8040d00d97ea to your computer and use it in GitHub Desktop.
Obsidian.md Cpage scripts

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:

  • <%alias%> Type: Cpage Discipline: Subdiscipline: Tags:

cssclasses: Cpage


[[<% newtitle %>#B|<% alias %>]]

This is the embedded display body. Everything here will be shown when you embed using ![[<% newtitle %>#<% newtitle %> B <% alias %>]].

%%Everything above the # is the embedded display. %%

%%B%%

This is a optional heading. It must be heading 2 or smaller

This is the main body of the page. This is not shown in the embedding

%%end of body%%

Internal links

Table without id file.link as "in collections"
FROM [[#]] AND "5_Collections"

External links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment