blake.app

Discourse Short Urls

I often in Discourse will link to another topic, but by default share urls are quite long and will look something like this:

https://blog.blake.app/t/ember-inflector/20?u=blake

which can make your markdown look pretty ugly and hard to read/edit if you have a lot of urls in a post.

However if you are linking to an internal post you can use a short url like:

/t/-/20

This way when you write it in markdown it looks so much cleaner:

[Link to a post](/t/-/20)

Well, I got sick of editing the long url by hand so I wrote a small bash/ruby app to do it for me.

Ruby Code

require 'uri'

url = ARGV[0]

if url == nil or url == ''
  puts 'Please specify a url'
  exit 0
end

uri = URI(url)

# /t/uploading-a-local-image-using-the-api/127071/39
path = uri.path

p = path.split('/')
p.slice!(0)
p[1] = '-'

short_url = ''
p.each do |i|
  short_url << "/#{i}"
end
print short_url

and my Bash script in my ~/bin folder to make it executable:

#!/bin/bash
source ~/.bash_profile
source ~/.bashrc

ruby ~/code/discourse_short_url/app.rb $1 | xclip -sel c

Then I can easily run it with:

durl https://blog.blake.app/t/ember-inflector/20?u=blake

and it will automatically add /t/-/20 to my clipboard.

Example link to my previous topic using a short url. See it works just fine :slight_smile:

1 Like