obsidian-dataview
obsidian-dataview copied to clipboard
Is there a way to reuse js code in dataviewjs?
I have some dataviewjs
code blocks defined in some .md
files, and they are very similar. Such as some utils functions. I wonder is there a way to place this code together, and import it to a dataviewjs
block, just like import
statement in ES6?
I'd find the way. I got the path of the vault root form dv.app.vault.adapter.basePath
, and then require()
the scripts.
This seems to work, but not on iOS. Here the example I used:
I create a js file <vault>/Queries/library.js
with this content:
exports.formatDate = date=> date.toLocaleString({ weekday:'short', day: 'numeric'})
And then you can use it inside a Note
// ```dataviewjs
const lib = require(dv.app.vault.adapter.basePath+'/Queries/library.js')
dv.paragraph(1,lib.formatDate(dv.current().file.ctime))
// ```
If you want to reload the referenced library every time, without restarting obsidian, use this code:
const src = dv.app.vault.adapter.basePath+'/Queries/library.js'
delete global.require.cache[global.require.resolve(src)]
const lib = require(src)
@andi0b were you able to find a workaround to get this working on iOS?
@andi0b were you able to find a workaround to get this working on iOS?
No, I copy&pasted the code now into every file. Seems to be the better compromise.
I can add a dv.require()
which just eval's code for you (the downside to this being it won't recognize JS modules/require statements properly), which should work across platforms. The main problem is that this all uses asynchronous calls, so you would have to do it as
let code = await dv.require("stuff");
Hey, I actually created a new plugin — now in the community store — that specifically addresses code reuse. It can be used in dataviewjs
blocks as well as templater templates. Check it out and let me know if it helps!
https://github.com/samlewis0602/obsidian-custom-js
I can add a
dv.require()
which just eval's code for you (the downside to this being it won't recognize JS modules/require statements properly), which should work across platforms. The main problem is that this all uses asynchronous calls, so you would have to do it aslet code = await dv.require("stuff");
This would be actually perfect. Nothing against @SamLewis0602 solution here which works good for me currently, but i'd love to have this centralized in here(as the project allows for coding anyways it makes at least sense to me to have said feature available)
For now, to get this feature "out", I'll probably implement a janky solution which simply loads + evals files for you (with a cache to support recursive includes); the best long-term approach would be to override require()
to support loading from the vault on top of it's default behavior, which I think I can do with some proxying.
I'd be curious to see your solution if you're able to get require()
working somehow as that would greatly simplify the API for CustomJS compared to strict format rules due to using eval
What is the status of this issue? I'm interested in the .require() solution as well.
I would recommend going with CustomJS for now. I've seen folks build out user script libraries with it.
CustomJS seems to have taken off! We have dv.view()
to reuse view code but best replicate business logic via CustomJS especially if you want to use it in other plugins. Closing as not planned.