darlal / obsidian-switcher-plus

Enhanced Quick Switcher plugin for Obsidian.md
GNU General Public License v3.0
442 stars 11 forks source link

[Feature Request] Search all blocks with `^block-ids` by their content #168

Open tyler-dot-earth opened 4 months ago

tyler-dot-earth commented 4 months ago

I'd like the ability to search all blocks with ^block-ids by their content.

I'm imagining this working the same way that the this plugin's symbols work (like # to search headings), but for blocks which already have ^block-ids.

I was actually surprised to see this wasn't already in this plugin, so I'm hoping it's a feature the community would be interested in.

If maintainers are open to it and can maybe share some relevant parts of the codebase to enable this, I might be able to contribute the feature myself. I am already a contributor to the Readwise plugin and familiar with how Obsidian plugins generally work.

tyler-dot-earth commented 4 months ago

While not comprehensive logic for identifying blocks with IDs, this is a simplistic/naiive approach that gets all content on the same line as a block-id:

async getBlocksWithIds(app: App): Promise<string[]> {
    const blocksWithIds: string[] = [];

    // Get all markdown files in the vault
    const files = app.vault.getMarkdownFiles();

    for (const file of files) {
        const content = await app.vault.read(file);
        const lines = content.split('\n');

        for (let i = 0; i < lines.length; i++) {
            const line = lines[i];
            if (line.match(/\^[\w-]+/)) {
                blocksWithIds.push(line);
            }
        }
    }

    return blocksWithIds;
}

Emphasis on this being simplistic/naiive, as it will over-match a lot of lines (really any that contain the ^word) and not match many cases where it should (eg when the ^block-id is on the next line)

tyler-dot-earth commented 4 months ago

I have discovered that Obsidian can kind of search all these references natively.

Just type [[^^ -- the double caret notation is important. I'd only tried single caret. It'll show more than just the blocks with ^block-ids, but it WILL show blocks with block-ids from all files, which is still useful.

I'll leave the request open anyway as this idea may still be a compelling feature for this plugin.

tyler-dot-earth commented 4 months ago

I've made a plugin which does this: https://github.com/tyler-dot-earth/obsidian-blockreffer

Though I still think it would be awesome in Quick Switcher!

darlal commented 4 months ago

I have discovered that Obsidian can kind of search all these references natively.

I believe core Obsidian also supports a search operator for blocks already. So searching block content is already supported out-of-the-box as part of the core search functionality.

[[^^ -- the double caret notation is important. I'd only tried single caret. It'll show more than just the blocks with ^block-ids, but it WILL show blocks with block-ids from all files, which is still useful.

I imagine this capability is to enable users to define new block-ids without interrupting their workflow i.e. you can define a new block-id and link directly to it in one step.

I've made a plugin which does this .. Though I still think it would be awesome in Quick Switcher!

Cool! 👍 it's also perfectly reasonable to let this new plugin evolve independently 😄

Have you seen the Omnisearch plugin? It provides full-text search in a quick-switcher type modal. Your plugin is similar but scope to Blocks, I think the capability you've build could fit in better with Omisearch as well.