obsidian-dataview icon indicating copy to clipboard operation
obsidian-dataview copied to clipboard

dv.view() - no information about the javascript files

Open OnkelTem opened this issue 3 years ago • 8 comments

https://blacksmithgu.github.io/obsidian-dataview/api/code-reference/#dvviewpath-input

Describe the problem Missed information about the javascript files: what format they should use, how to export things?

I tried a few things but none of them worked.

E.g. scripts/view.1js:

console.log(`Loading view1 by ${__filename}`);

function foo(...args) {
  console.log('foo is called with args', ...args)

}
module.exports = foo;

And then:

```dataviewjs
await dv.view("scripts/view1", 'asd')
```

makes only the loading line in the console:

Loading view1 by /opt/Obsidian/resources/electron.asar/renderer/init.js

so the function foo was never called. Why?

Describe the solution you'd like I cannot suggest an edit because I didn't find a solution.

OnkelTem avatar Sep 13 '22 20:09 OnkelTem

Can you look at an example snippet in #1337? Getting that added to the docs would be great as well.

AB1908 avatar Sep 13 '22 22:09 AB1908

@AB1908 Hi! Sorry, I didn't get how it is related to my question :) I'm asking about an example of a proper javascript file.

OnkelTem avatar Sep 14 '22 08:09 OnkelTem

The example in #1337 is the content of the .js file! You can put in js file the same thing you can write in dataviewjs code block.

mnvwvnm avatar Sep 14 '22 10:09 mnvwvnm

@mnvwvnm Ah, that's the point!

Well, it doesn't feel right to me, I wouldn't guess. In JS world, one exports things, because files are treated as modules and are subject to further processing and with module.exports you manage scopes. That's how it's done in Templater for example.

It looks like syntactic sugar. As if dataviewjs created a module stub with a single export function with args, and then would put my stuff in the body of that function. In my opinion, this game doesn't worth the candle.

OnkelTem avatar Sep 14 '22 21:09 OnkelTem

What's the use case?

AB1908 avatar Sep 14 '22 22:09 AB1908

Here is another issue I stumbled upon.

I created a file called .views/view1.js, put some code in the body and then called it from Obisdian as:

```dataviewjs
await dv.view(".views/view1", 'asd')
```

However, it didn't work and I saw a message:

Dataview: custom view not found for '.views/view1/view.js' or '.views/view1.js'.

After a little investigation, I found out that Obsidian (or Dataview?) imposes some limitations on file naming. Particularly, after I removed that leading dot it started "seeing" my file. But I would like to use dot, because it hides this techie .views/ dir from the user. Any ideas about what's going on here?

OnkelTem avatar Sep 14 '22 22:09 OnkelTem

@AB1908 Use case for what, sorry? If you speak about regular exports, then they're more anticipated by fellow javascript programmers :) Natural javascript code organization is also a good thing.

OnkelTem avatar Sep 14 '22 22:09 OnkelTem

Views aren't meant to help with that kinda organization if I'm being honest. I doubt this will properly be supported either as it just isn't as widely used. I do understand where you're coming from though. Perhaps look at CustomJS plugin.

The dot problem is likely an Obsidian quirk. It doesn't properly index any folders with leading dots but I'm not quite certain why plugin code is having trouble with it. Perhaps it's related.

AB1908 avatar Sep 14 '22 22:09 AB1908

The reason foo didn't run is because you never called it, this isn't a module it is just a script. Dataview is simply running the .js file as a script so it starts at the top where you call console.log() and then the script ends. You declared foo but never called it. Try this:

console.log(`Loading view1 by ${__filename}`);
foo()

function foo(...args) {
   console.log('foo is called with args', ...args)
 }

ceciltech avatar Feb 24 '23 16:02 ceciltech