Action for URL shortening?

I’m looking for an action that will generate markdown from a URL by using the domain as the text. The result would be this:

[domain.com](http://www.domain.com/some-long-url)

Which would look like these links here. Alternately, the way links are displayed on the reddit home page is fine.

Does that sort of action exist?

Thanks!

How’s your regular-expression-fu? I haven’t seen an example, but it’s the kind of thing that could be done pretty easily with a regular expression replace script.

Are you saying you have a text with a number of URLs in it, and want it scanned and all of the URLs reformatted this way?

I don’t think it is that easy. If it was a case of including the www or other sub-domain, then it is very straight forward. But the complexity comes in when you start considering the domain name extension as well as an optional sub-domain being included.

Here’s some simple examples of some URLs and the expected text part of the link results.

  • http://www.sylumer.com/foo -> sylumer.com
  • http://sylumer.com/foo -> sylumer.com
  • http://www.sylumer.co.uk/foo -> sylumer.co.uk
  • http://sylumer.co.uk/foo -> sylumer.co.uk
  • http://www.sylumer.education.tas.edu.au/foo -> sylumer.education.tas.edu.au
  • http://sylumer.education.tas.edu.au/foo -> sylumer.education.tas.edu.au

Those are all real domain name extensions after my forum ID. There are well over 8,000 currently and it continues to grow, and they seem to vary between one and three dotted components in length.

I think you actually need to match the domain name extension, then track back one period separated string before that, and return that combination.

Now you could be thorough and iterate over all 8k+ domain name extensions, or you could perhaps whittle it down to the ones you are most likely to come across/use, which would improve the performance substantially I suspect

In the browser, I think you can query it through the Window object with some JavaScript, but we don’t have that option here.

But, if it were okay to return the sub domain as well, then it’s just splitting the URL by forward slashes an taking the third element.

@sylumer In the examples you cite, I’d be happy with the results you describe. I’m almost entirely sharing news articles and blog posts, so it’s all going to come out



If I can get the domain and TLD, and strip out the common subdomains – most notably, www – I’d be happy.

I’m also fine with something like the way that Reddit displays URLs:

nytimes.com/2020/

Why do you say the domain is the third element? Wouldn’t it be the second element? http:// is the first element; you’ll throw that away.

The domain is the next element, ending in the TLD.

Then throw away the next / and everything to the right of that.

@agiletortoise My regex skills are very, very bad. Hence my query here! Suppose I could learn but I thought there might be something lying around that would work, or that some regex genius could whomp something together in a minute.

This regex101 demo is pretty naive but is a basic pattern that would work for most cases, and you end up with the full URL in the full match and $2 would contain the domain for use in the substitution.

I’m about to go out so don’t have time to stick it in an action, but it’s a start.

I’ll play with it when I get a chance. Thanks!

Got it working!

Next step: I’d like to create an action where the original draft looks like this:

text moretext additionaltext etc http:/domain.tld/URL blah blah blah.

And the output looks like this:

text moretext additionaltext etc [domain.tld](http://domain.tld/URL) blah blah blah.

If you are splitting by forward slashes, you encounter two together.

E.g.

Start with https://docs.getdrafts.com/gettingstarted/

Split with every forward slash would produce the following;

  1. https:
  2. docs.getdrafts.com
  3. gettingstarted

Two is the empty string between the first two slashes and 5 is the empty string after the final slash. The start and end of the string act as termination separators.

1 Like