Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a liquid filter to limit a url to the domain name only?

Tags:

jekyll

liquid

I'm wondering if there is a liquid filter that would limit a url to the domain name only.

For example, let's say I wanted to have a link to an article called "The Greatest Article Ever" and the url was something unwieldily like http://example.com/long/ugly/directory/123948/name. Let's also say I had both of these values in my YAML metadata in an array as "title:" and "url:" respectively, and my desired output was something like this:

<div class="cool-articles">
  <a href="http://example.com/long/ugly/directory/123948/name">
    The Greatest Article Ever
  </a>
  <span>example.com</span>
</div>

How might I use liquid to limit the array-item.url to the domain name only?

I've been searching through the liquid docs for a nice filter to do it, but I've only found remove (which can nix the "http://") but nothing to just strip out everything but the domain name.

Anyone have any thoughts?

Thanks!

like image 472
adr Avatar asked Mar 24 '23 03:03

adr


1 Answers

Well, you can try to split the URL on slashes (after removing "http://") and get the first element of the resulting array. For example:

{{ url | remove:'http://' | split:'/' | first }}

I didn't test it, but it should work reasonably well for the most part. With this pipeline in place, you just need to construct the output as you said:

<div class="cool-articles">
  <a href="{{ url }}">
    {{ title }}
  </a>
  <span>{{ url | remove:'http://' | split:'/' | first }}</span>
</div>

I hope that helps. :)

like image 107
agarie Avatar answered Apr 08 '23 05:04

agarie