API endpoint for searching file content?

Hi all, I’m playing around with making an Obsidian plugin, but I’ve run into a gap in my understanding.

Does anyone know of an API endpoint for searching file content programatically? I’ve seen a couple of plugin authors sneak into the ‘global-search’ plugin to initiate a UI search, but I don’t see any way of initiating a query from JavaScript and getting the results as a JavaScript object.

My use case is that I want a way to execute a search and then render the results differently – sorting by matched line rather than grouping by file. This would be useful for e.g. sorting task lines by title or due date. My strategy right now is to use a custom code block (like the recently-added query block).

I don’t mind manually iterating through the notes in the vault, but I’d rather make use of the existing search engine (with its syntax, knowledge about tags and metadata, etc.) if possible.

6 Likes

If someone ever finds the answer for this. Let me know. I to want to query date (in this caste text) from Obsidian to send it to an external app. I could do this using a Cronjob with a grep command but I want to bundle it as a plugin.

2 Likes

This is also something I’m interested in.

@Craig Where did you find examples of plugin’s sneaking around like that? I’ve just starting looking at plugin source this weekend and haven’t found anything like that.

1 Like

I know the Text Expand plugin does this.

Thanks NomarClub! Yes, the text expand plugin was one of them – all the ones I found used a similar strategy.

1 Like

(I’m resurrecting this thread from the dead, but I wanted to add the answer for any other Google searchers like me.)

There’s a great plugin which will let you perform advanced full-text searches via JS/API:

If you don’t want to use the above plugin for whatever reason, here’s sample code which will perform an vanilla Obsidian search and then return a list of files which match the query.

const query = 'some query'

// Perform the search
app.internalPlugins.plugins['global-search'].instance.openGlobalSearch(query)
const searchLeaf = app.workspace.getLeavesOfType('search')[0]
const search = await searchLeaf.open(searchLeaf.view)
const rawSearchResult = await new Promise(resolve => setTimeout(() => {
    resolve(search.dom.resultDomLookup)
}, 300)) // the delay here was specified in 'obsidian-text-expand' plugin; I assume they had a reason

const files = Array.from(rawSearchResult.keys())

console.log(files.map(x => x.path))
1 Like