Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to reuse js code in dataviewjs? #271

Open
duoani opened this issue Jun 7, 2021 · 9 comments
Open

Is there a way to reuse js code in dataviewjs? #271

duoani opened this issue Jun 7, 2021 · 9 comments

Comments

@duoani
Copy link

@duoani duoani commented Jun 7, 2021

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?

@duoani
Copy link
Author

@duoani duoani commented Jun 7, 2021

I'd find the way. I got the path of the vault root form dv.app.vault.adapter.basePath, and then require() the scripts.

@duoani duoani closed this Jun 7, 2021
@andi0b
Copy link

@andi0b andi0b commented Jul 21, 2021

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)

@SamLewis0602
Copy link

@SamLewis0602 SamLewis0602 commented Aug 11, 2021

@andi0b were you able to find a workaround to get this working on iOS?

@blacksmithgu blacksmithgu reopened this Aug 11, 2021
@andi0b
Copy link

@andi0b andi0b commented Aug 12, 2021

@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.

@blacksmithgu
Copy link
Owner

@blacksmithgu blacksmithgu commented Aug 13, 2021

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");

@SamLewis0602
Copy link

@SamLewis0602 SamLewis0602 commented Sep 3, 2021

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

@Darksider3
Copy link

@Darksider3 Darksider3 commented Nov 17, 2021

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");

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)

@blacksmithgu
Copy link
Owner

@blacksmithgu blacksmithgu commented Nov 18, 2021

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.

@SamLewis0602
Copy link

@SamLewis0602 SamLewis0602 commented Nov 23, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants