Obsidian+PR Cookbook - 𝕏 - YouTube - Official Obsidian Docs
Using Plugin REPL involves writing some code in a language called JavaScript. Other commonly used Obsidian plugins like Dataview and Templater make use of JavasScript (Dataview also defines it’s own programming language). Programming is a skilled and often complex activity that some people spend their entire lives working on. But I am going to argue here that a little JavaScript can go a long way and you don’t need to learn all of it.
So firstly, you probably don’t need to understand that much to do the things that you want to do. I will give a summary of some of the things that you need to know here. JavaScript can be used to buid complex things, so have a bunch of complex features to handle the complexity but you don’t need this, but to get started with Plugin REPL you need a lot less
Why program anyway
Why program at all? You can do lots of things in Obsidian without programming. And there are many plugins that let you do things.
The value that programming provides is the ability to do similar things slightly differently by clicking together parts. Often plugins will only let you do things in one way.
Programs also allow you to express what you want to do in text, which can at times be a lot simpler than updating all the settings for Plugin.
The basics of JavaScript
Firstly, you may well need to understand no JavaScript to use JavaScript - you can find the pieces of code that you need to do what you want to do and copy and paste them into Obsidian. Alternatively, you might be able to find example code and adapt it slightly to do what you want.
If you need to actually create something new, the main thing that you need to be able for Plugiun REPL is call functions (Plugin REPL provides you with a lot of functions to do things), collect informtion from functions, and use variables.
Functions
To use/run/call a function called myFunction you add a pair of brackets like so:
myFunction()
Sometimes the function needs some information. You can provide this information inside the brackets with commas separating each value:
myFunction(1, 2)
Sometimes you will want to provide this function with a piece of text, this text has to go in quotation marks.
myFunction("The quick brown fox jumps over the lazy dog")
Getting information from a function
Sometimes a function can give you information about what it has done. This is called the result of the function. You can save the result of the function in a variable. To do this you use the equals symbol like so
result = myFunction()
If you you are creating more complex programs there are more “correct” ways of doing this - but you can worry about them later.
Using the information from a function
Once you have collected information from a function you can use it in a function like so:
otherFunction(result)
Plugin REPL also provides some “default” pieces of information such as path.
Other things
That’s basically it for the javascript you need. Everything is calling functions and combining the results. But there are a couple of other things you need. If you want to create a command you need to use the following code
newCommand(async function() {
... functions go here
})
The other complexity is that some functions start and “run in the background” if you want to collect information from them you have to “await” the reply like so.
a = await promptString()
This is a little confusing, but it is done so that in complicated programs you can do things at the same time.
Even more
So if this is all you need why does JavaScript have more features. The way I think about it is that everything else is about “dealing with the complexity” of writing larger programs. If you want to do more complicated things you might need to learn these.
The first thing it would use useful to learn about are methods. These are functions that are “attached” to a piece information. You use them like
result = a.f()
This is like saying result = f(a) but f is attached to a so you can use it on a. This makes things a bit “neater”. An example of when you might want to use this is on the dv object:
dv = getDv()
dv.pages()
To learn this you might like to get a book on JavaScript or look at examples and try to understand what the code is doing.
I would suggest you learn things in this order.
Everytthing else.
Google what you are trying to do.
What next
I hope this is enough of an introduction on JavaScript that you could use it for some features of Plugin REPL. You might like to check out some examples of cool things you can do in the cookbook for inspiration.
I am not affiliated with Obsidian. I am the author of plugin repl