Support for shortlinks?

At work we use a number of internal link systems; go/foobar for a link shorterner, bug/12345 for the bug tracker, and more. Many of our internal systems support this natively; that is, typing bug/12345 into documents will automatically link it to http://bug/12345 (which then redirects to the full URLhttps://bugtracker.company-internal.com/show_bug?id=12345). Now, typing http://bug/12345 works, but it would be really nice to type something shorter, especially if it can be just bug/12345. Bonus points if I can actually make the underlying link the full URL.

Any idea on how to do this?

Doing this at the rendering system is likely impossible. Instead, I’ve added a couple of links to toggle the prefix (this is slightly hacky, but, hey, tinkerware, right?)

silverbullet.registerCommand({name: "Linkify: Add http://"}, async() => {
  let text = await editor.getText();
  matches = text.matchAll(/([\s^])((bug|change)\/[A-Za-z0-9_\-]+)/g);
  let changes = [];
  for (match of matches) {
    let from = match.index + match[1].length;
    let to = from + match[2].length;
    changes.push({from:from, to:to, insert: "http://" + match[2]});
  }
  console.log(changes);
  editor.dispatch({changes});
})

silverbullet.registerCommand({name: "Linkify: Drop http://"}, async() => {
  let text = await editor.getText();
  matches = text.matchAll(/\bhttp:\/\/((bug|change)\/[A-Za-z0-9_\-]+)/g);
  let changes = [];
  for (match of matches) {
    let from = match.index;
    let to = match.index + match[0].length;
    changes.push({from:from, to:to, insert: match[1]});
  }
  console.log(changes);
  editor.dispatch({changes});
})
2 Likes

There is a very simply sulution - at least kind of :slight_smile:

if you create a snippet like that:

---
tags: template
description: insert Shortlink bugtracker
hooks.snippet:
  slashCommand: bug
---
https://bugtracker.company-internal.com/show_bug?id=|^|

You are able to type somewhere in your text:
/bug{enter}12345
and it expands to your full link:
https://bugtracker.company-internal.com/show_bug?id=12345

Not exactly what you are looking for, but basically it does what you are looking for: insert the link with a few keystrokes… :slight_smile:

BTW: Even less keystrokes you get, if you add a keybinding to the snippet :slight_smile:

1 Like

A bit of a teaser, but as part of the stuff I’m working on as part of Space Lua I’ll likely be adding a ${lua expression} syntax, which will Live Preview to whatever markdown that expression returns.

This would mean you can easily define a function named bug that could return markdown linking to a bug.

Concept code (but not really, since this already works on Edge, just don’t use it yet as some key things may still change):

Anywhere in your space:

```space-lua
function bug(id)
  return "[Bug #" .. id .. "](https://bugtracker.company-internal.com/show_bug?id=" .. id .. ")"
end
```

And you can then use it anywhere else like this:

See this bug: ${bug "10"}, or if you prefer ${bug(10)}

Which would render to Bug #10

4 Likes