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

jekyll, liquid

Solution

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. :)

Problem

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!

Original source