As title says with examples below how to import function Bar in Foo?
I tried just doing an import but it fails with “Cannot use import statement outside a module”
Then I figured I should probably do something like module.import in some way but dunno how.
Example files:
Foo.js
Do import somehow?
function Foo() {
Bar();
}
module.exports = Foo;
Bar.js
function Bar() {
console.log("bar");
}
module.exports = Bar;
On a sidenote I tried adding topic tags for js, javascript, code, etc but there were no options for them so they could not be added.
Some of the Templater user scripts examples suggest to pass the tp as an argument, and that allows for automatic imports as well, as then the functions are available through the user scripts.
function bar(tp) {
console.log("bar");
return "bar"
}
module.exports = bar
Some random test file
<%+ tp.user.foo(tp) %>
Which indeed returned “foobar” as expected, when I’m previewing it. I’m getting some type error thingy, seen in the console, but it does do the expected stuff.
This is an interesting idea and I certainly would not mind doing it like this.
I’m a little uncertain as to whether I should check it as solution or not since it technically did not solve the question of how to import. I think I will let it be for a few days and see if somebody else has more input otherwise I will just mark this answer as solution.
If I run a function like tp.file.folder(), it is indeed the correct folder for the currently open note, even though I didn’t pass the tp variable from the script that’s actually executing inside that note.
That would eliminate the need for passing tp around although tp is still needed.
I haven’t see this app object before. That is some nice little extra to know about.
Apart from whatever it is actually intended to be used for I could see it perhaps having some interesting use case combined with dataviewjs for showing some information about plugins or other type of data. Could be put on some dashboard or something.