Is there a fluid filter to restrict the URL to just a domain name?

I am wondering if there is a liquid filter that restricts the URL to only the domain name.

For example, let's say I wanted to have a link to an article called "The Greatest Article Ever", and the URL was something uncomfortable, like http://example.com/long/ugly/directory/123948/name . Let them also say that I had both of these values ​​in my YAML metadata in the array as "title:" and "url:" respectively, and my desired result 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 can I use liquid to restrict an item.url array to only a domain name?

I was looking for a good filter in liquid documents to do this, but I only found a deletion (which can use "http: //"), but there was nothing to just delete everything except the domain name.

Anyone have any thoughts?

Thanks!

+4
source share
1 answer

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

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

I have not tested it, but it should work quite well for the most part. With this pipeline, you just need to build the output, as you said:

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

Hope this helps. :)

+7
source

Source: https://habr.com/ru/post/1492012/


All Articles