Can I import dataviewjs from other file?

Things I have tried

  1. Try to count how many search results in a file
  2. Reuse the dataview codes, or import from other files

What I’m trying to do

I have somes tags in ONE file. I tried to count how many times it appears in my file. Every line it only display only one time. I searched posts by no good results found. I’ve found some people have same requests.
Then I modified a dataviewjs code shared from others, formally to display the results and it just works for my purpose:

// Modified from https://zhuanlan.zhihu.com/p/373623264
dv.paragraph(count("ABC") + count("#search"))

function count(term){
const thisPath = dv.current().file.path //search in this file
const files = app.vault.getMarkdownFiles().filter(file => file.path.includes(thisPath)) // filter to restrict in this file

var count = 0
// filter all notes with search term
const arr = files.map(async ( file) => {
	const content = await app.vault.cachedRead(file)
	const lines = content.split("\n").filter(line => line.contains(term))
	count = lines.length -1 // search term exist at this note, must minus 1
	return lines
})
Promise.all(arr).then(values => {
	// dv.list(values.flat()) // display all results 
	dv.paragraph(count) // display counts
})
}

There is another question. I have same request in many files, but the maintainance may be sucks if I copy same codes to many files. I am new to Javascript. Is there any good method to import functions from other files? Please give me some examples. Thank you very much!

1 Like

I think people have done this with the “customJS” community plugin but I have not yet personally tried it. Maybe a “user script” via the “Templater” community plugin could also work? I don’t know how those work with dataviewjs.

Good luck! I am eager to read the solution.

The very helpful @Craig has a solution over in this forum thread that looks more straightforward!

1 Like

Thank you!
I have seen one similar post but no actual content so I don’t know what the file content would be.
However, I tried but it still doesn’t works. There is no content shows at my file.

The dataviewJs.js content is:

function countN(term,thisPath){
const files = app.vault.getMarkdownFiles().filter(file => file.path.includes(thisPath)) // filter to restrict in this file

var count = 0
// filter all notes with search term
const arr = files.map(async ( file) => {
	const content = await app.vault.cachedRead(file)
	const lines = content.split("\n").filter(line => line.contains(term))
	return lines
})
Promise.all(arr).then(values => {
	return (values[0].length)
})
}
exports.countN = countN;

The code I added to file is

var dataviewUtils = require(app.vault.adapter.basePath + "/dataviewUtils.js");
dv.span(dataviewUtils.countN("searchwords",dv.current().file.path))

But it shows nothing!

FYI, Dataview now provides a cleaner way to include custom JS. You can put the js file anywhere in your vault (say, Files/my_view.js) and then invoke it with dv.view("Files/my_view.js")

More info here: Codeblock Reference - Dataview

2 Likes