Skip to content

Commit 6e27f9b

Browse files
Update links.md
1 parent a2eeb59 commit 6e27f9b

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

cheatsheets/jekyll/templating/links.md

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,68 @@
22

33
## Local paths
44

5-
Use the `link` tag. The smart way to do links for local pages.
5+
There are multiple ways to do this, depending on you situation.
6+
7+
### Relative URL
8+
9+
Use the `relative_url` filter to ensure you get a Github Pages project prefix added.
10+
11+
The downside to this approach as uis that it used a literal string - is does not validate if the page actually exists and also does not obey and permalink settings set on the metadata or config.
12+
13+
```markdown
14+
- [Link text]({{ '/' | relative_url }})
15+
16+
- [Link text]({{ 'foo/' | relative_url }})
17+
```
18+
19+
If you reference a page object, then it is safer to expect the URL to be valid - use of config settings might affect this though such as permalink or collections.
20+
21+
Use `site.page` object for the current page.
22+
23+
```markdown
24+
- [Link text]({{ site.page.url | relative_url }})
25+
```
26+
27+
Use a `for` loop on pages or a collection.
28+
29+
```markdown
30+
{% for item in site.pages %}
31+
- [Link text]({{ item.url | relative_url }})
32+
{% endfor %}
33+
```
34+
35+
### Link tag
36+
37+
Use the `link` tag. The smart way to do links for local pages.
638

739
This will figure out the appropriate URL. And it will give build error if the page path is not valid. Note - do NOT use quotes around the URL otherwise they will be rendered as escaped quote tags and possibly break the HTML.
840

941
e.g.
1042

11-
```
12-
- [Link description]({% link about.md %})
43+
```markdown
44+
- [Link text]({% link about.md %})
1345
```
1446

1547
Result:
1648

1749
```html
18-
<a href="/my-base-url/about.html">Link description</a>
50+
<a href="/my-base-url/about.html">Link text</a>
51+
```
52+
53+
You can pass a variable too:
54+
55+
```markdown
56+
- [Link text]({% link {{ item }} %})
57+
```
58+
59+
60+
This works well for Jekyll 4 - which adds base URL for you. Otherwise you must do:
61+
62+
```markdown
63+
{{ site.baseurl }}{% link about.md %}
1964
```
2065

66+
2167
## Footer links
2268

2369
More Markdown then Jekyll, but still useful to keep text readable. Also this link be just below the paragraph rather than the end of the page.
@@ -67,4 +113,4 @@ Markdown:
67113
```
68114
<!--stackedit_data:
69115
eyJoaXN0b3J5IjpbLTEzMzA0NzcwNTFdfQ==
70-
-->
116+
-->

0 commit comments

Comments
 (0)