I'm trying to do the following in Jekyll:
I have an URL;
This URL can be relative ("/media/image.jpg") or absolute "http://example.com/image.jpg";
If the URL is relative I have to append to its beginning my site domain ("http://my-site/media/image.jpg"). If don't, nothing changes.
Sadly I have no clue yet on how to achieve this.
I'd love if somebody could help me.
Thanks in advance.
You can do this using liquid. Here is a basic example of what you could do. You will probably want to adjust it for your own use.
{% assign myurl = "/css/bootstrap.min.css" %}
{% if myurl contains '://' %}
{{myurl}}
{% else %}
{{ myurl | prepend: site.url }}
{% endif %}
Breaking down the code:
This line just creates a variable holding the url:
{% assign myurl = "/css/bootstrap.min.css" %}
Next, we will check if the url is absolute. This simple version does that by checking the string, ://
, is in the url:
{% if myurl contains '://' %}
If the url does, liquid will simply print out the url variable:
{{myurl}}
If not, liquid will prepend the site's url to the beginning of url and print the complete url:
{% else %}
{{ myurl | prepend: site.url }}
{% endif %}