File indexing completed on 2025-01-18 10:18:35
0001 {%- comment -%}
0002 When the website is built by GitHub Pages,
0003 'site.url' is set to 'https://username.github.io'
0004 'site.baseurl' is set to '/lesson-name'
0005
0006 When we start a local server using `jekyll serve`,
0007 'site.url' is set to 'http://localhost:4000' and
0008 'site.baseurl' is empty.
0009
0010 In both of the above cases we set 'relative_root_path' to 'site.baseurl'.
0011
0012 When we build a website locally with `jekyll build`,
0013 both 'site.url' and 'site.baseurl' are empty.
0014 This case is handled by the last 'else' in the code below.
0015 The logic there follows the (adapted) instructions found at:
0016 https://ricostacruz.com/til/relative-paths-in-jekyll
0017
0018 `page.url` gives the URL of the current page with a leading /:
0019
0020 - when the URL ends with an extension (e.g., /foo/bar.html),
0021 we can get the 'depth' of the page by counting the number of
0022 forward slashes ('/') and subtracting 1
0023 - when the URL ends with a forward slash (e.g. /foo/bar/),
0024 we can get the depth of the page by counting the number of /
0025 {%- endcomment -%}
0026
0027 {% if site.url %}
0028 {% assign relative_root_path = site.baseurl %}
0029 {% else %}
0030 {% assign last_char = page.url | slice: -1 %}
0031 {% if last_char == "/" %}
0032 {% assign offset = 0 %}
0033 {% else %}
0034 {% assign offset = 1 %}
0035 {% endif %}
0036 {% assign depth = page.url | split: '/' | size | minus: offset %}
0037 {% if depth <= 1 %}{% assign relative_root_path = '.' %}
0038 {% elsif depth == 2 %}{% assign relative_root_path = '..' %}
0039 {% else %}{% capture relative_root_path %}..{% for i in (3..depth) %}/..{% endfor %}{% endcapture %}
0040 {% endif %}
0041 {% endif %}