Quickly search/open tag "virtual" page

After some discussion in the chat, I wrote a space script to quickly search/open the virtual tag page. It’s linked to Ctrl-Shift-t (Cmd-Shift-t on mac) but is easy to customise.

Maybe it can be useful to some of you.

```space-script
silverbullet.registerCommand({name: "Search Tag", 
                              hide: true, 
                              key: "Ctrl-Shift-t", 
                              mac: "Cmd-Shift-t"}, async () => {
    let tagName = await editor.prompt(
          "tag name",
          "",
      )
  editor.navigate("📌 " + tagName)
});
```
3 Likes

I modified the script to be able to search with a tag picker. @raphwriter I think now you have what you wanted. Thanks for the idea :slight_smile:

```space-script
silverbullet.registerCommand({name: "Search Tag", 
                              hide: false, 
                              key: "Ctrl-Shift-t", 
                              mac: "Cmd-Shift-t"}, async () => {
  // Get all tags
  const allTags = await system.invokeFunction(
    "index.queryObjects",
    "tag")
  // Only keep one occurrence per tag
  const uniqueNames = new Set();
  const uniqueTags = allTags.filter(tag => {
      if (!uniqueNames.has(tag.name)) {
          uniqueNames.add(tag.name);
          return true; 
      }
      return false;
  });
  // Ask for tag selection
  const tag = await editor.filterBox(
    "Tags",
    uniqueTags.map((f) => {
      return {
        name: f.name,
        description: ""
      }
    }),
    "Select the tags to open it's virtual page",
  )  
  // Show tag page
  editor.navigate("📌 " + tag.name)  
});
```
4 Likes

Really nice – thank you!

@bent0n On SB 0.9.0 I’m getting error notice: Error running command: system is not defined, so I guess this works on the edge version?

I’m running current version (0.9.4) not edge.

I believe syntax is different in earlier versions

I think it’s syscal(“system.invokeFunction”, instead of system.invokeFunction(

Edit: Syntax was changed in 0.9.1: The one with local time, nicer space script and schema

Well, I have got this now:

silverbullet.registerCommand(
	{
		name: 'Search Tag',
		hide: false,
		key: 'Ctrl-Shift-t',
		mac: 'Cmd-Shift-t',
	},
	async () => {
		const allTags = await syscall(
			'system.invokeFunction',
			'index.queryObjects',
			'tag',
		);
		const uniqueNames = new Set();
		const uniqueTags = allTags.filter((tag) => {
			if (!uniqueNames.has(tag.name)) {
				uniqueNames.add(tag.name);
				return true;
			}
			return false;
		});

		const tag = await editor.filterBox(
			'Tags',
			uniqueTags.map((f) => {
				return {
					name: f.name,
					description: '',
				};
			}),
			"Select the tags to open it's virtual page",
		);

		editor.navigate('📌 ' + tag.name);
	},
);

But it argues about: Error running command: editor is not defined (I have no idea how to get the right name for the editor in JS). Also, it’s weird the error popup doesn’t appear if called from the command menu. If called by hitting the keyboard shortcut, the error popup does appear. Any ideas how to fix it, please?

You will need to adapt the syntax of the syscalls (editor.filterBox, editor.navigate) to the old syntax. However, instead of changing the script, I would recommend to update your instance to the current stable version.

OK, I will upgrade then. I upgraded and the original version works. :slight_smile:

The script itself has no error handling and will fail silently. However, it seems SilverBullet’s error handling using shortcut and the command picker is different. Shortcut will show the error in a notification and the command picker shows the error in the browser console.

Shortcut will show the error in a notification and the command picker shows the error in the browser console.

I think both should behave the same from the user’s perspective. Can the error handlers of both be unified?

One thing to keep in mind which I just encountered:

After picking a tag from the tag picker and then altering the result pages’ state (e.g. switching a task to complete/checking its box), the “virtual” results page will be saved as a real page. (Or rather never was a virtual page)

Don’t know if something can be done about that. My guess is that this line

editor.navigate("📌 " + tagName)

isn’t the same thing as when clicking on a hashtag?