Custom Command for inserting attachment embeds

Hello Silverbullet community!!
I’ve tinkering with this project for a couple of days and I find it very interesting and useful. The only thing I was missing was a way to manage attachments (files), but reading on the documentation for Space Scripts and syscalls found a couple syscalls that could let me do just exactly that, a simple way to insert local files and I implemented it in a space script and now I offer it to you. It is just a barebones version because embed height is hardcoded and stuff, but I was really amazed about how simple it was to make this

silverbullet.registerCommand({name: "Attachment: Embed"}, async () => {
  const byteFormatter = Intl.NumberFormat("en", {
    notation: "compact",
    style: "unit",
    unit: "byte",
    unitDisplay: "narrow",
  });
  const files = await space.listAttachments()
  const selected = await editor.filterBox(
    'Select',
    files.map((f) => {
      const size = byteFormatter.format(f.size)
      const date = new Date(f.lastModified).toLocaleString()
      return {
        name: f.name,
        description: `${date} - ${size} - ${f.contentType}`
      }
    }),
    'select a file from the list to insert a link embed in the cursor position',
    'File'
  )
  if (selected) {
    await editor.insertAtCursor(`![[${selected.name}|x400]]`)  
  }
});

Then, in my settings I have mapped this command to the /embed slash and that is it. Now you can write /embed, show a file picker, pick a file and insert an embed link.

5 Likes

Very nice!