Skip to content

Commit 01fa7e6

Browse files
committed
Improved relative_root_path
1 parent efe8c04 commit 01fa7e6

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

_includes/base_path.html

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
1-
{% comment %}
2-
This is adapted from: https://ricostacruz.com/til/relative-paths-in-jekyll
1+
{%- comment -%}
2+
When the website is built by GitHub Pages,
3+
'site.url' is set to 'https://username.github.io'
4+
'site.baseurl' is set to '/lesson-name'
35

4-
`page.url` gives the URL of the current page with a leading /:
6+
When we start a local server using `jekyll serve`,
7+
'site.url' is set to 'http://localhost:4000' and
8+
'site.baseurl' is empty.
59

6-
- when the URL ends with the extension (e.g., /foo/bar.html) then we can get
7-
the depth by counting the number of / and remove - 1
8-
- when the URL ends with a / (e.g. /foo/bar/) then the number / gives the depth
9-
directly
10-
{% endcomment %}
10+
In both of the above cases we set 'relative_root_path' to 'site.url + site.baseurl'.
1111

12-
{% assign relative_root_path = '' %}
12+
When we build a website locally with `jekyll build`,
13+
both 'site.url' and 'site.baseurl' are empty.
14+
This case is handled by the last 'else' in the code below.
15+
The logic there follows the (adapted) instructions found at:
16+
https://ricostacruz.com/til/relative-paths-in-jekyll
1317

14-
{% assign last_char = page.url | slice: -1 %}
18+
`page.url` gives the URL of the current page with a leading /:
1519

16-
{% if last_char == "/"}
17-
{% assign offset = 0 %}
18-
{% else %}
19-
{% assign offset = 1 %}
20-
{% endif %}
20+
- when the URL ends with an extension (e.g., /foo/bar.html),
21+
we can get the 'depth' of the page by counting the number of
22+
forward slashes ('/') and subtracting 1
23+
- when the URL ends with a forward slash (e.g. /foo/bar/),
24+
we can get the depth of the page by counting the number of /
25+
{%- endcomment -%}
2126

22-
{% assign depth = page.url | split: '/' | size | minus: offset %}
23-
{% if depth <= 1 %}{% assign relative_root_path = '.' %}
24-
{% elsif depth == 2 %}{% assign relative_root_path = '..' %}
25-
{% elsif depth == 3 %}{% assign relative_root_path = '../..' %}
26-
{% elsif depth == 4 %}{% assign relative_root_path = '../../..' %}
27+
{% if site.url %}
28+
{% assign relative_root_path = site.url | append: site.baseurl %}
29+
{% else %}
30+
{% assign last_char = page.url | slice: -1 %}
31+
{% if last_char == "/" %}
32+
{% assign offset = 0 %}
33+
{% else %}
34+
{% assign offset = 1 %}
35+
{% endif %}
36+
{% assign depth = page.url | split: '/' | size | minus: offset %}
37+
{% if depth <= 1 %}{% assign relative_root_path = '.' %}
38+
{% elsif depth == 2 %}{% assign relative_root_path = '..' %}
39+
{% else %}{% capture relative_root_path %}..{% for i in (3..depth) %}/..{% endfor %}{% endcapture %}
40+
{% endif %}
2741
{% endif %}

0 commit comments

Comments
 (0)