|
2 | 2 |
|
3 | 3 | ## Local paths |
4 | 4 |
|
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. |
6 | 38 |
|
7 | 39 | 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. |
8 | 40 |
|
9 | 41 | e.g. |
10 | 42 |
|
11 | | -``` |
12 | | -- [Link description]({% link about.md %}) |
| 43 | +```markdown |
| 44 | +- [Link text]({% link about.md %}) |
13 | 45 | ``` |
14 | 46 |
|
15 | 47 | Result: |
16 | 48 |
|
17 | 49 | ```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 %} |
19 | 64 | ``` |
20 | 65 |
|
| 66 | + |
21 | 67 | ## Footer links |
22 | 68 |
|
23 | 69 | 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: |
67 | 113 | ``` |
68 | 114 | <!--stackedit_data: |
69 | 115 | eyJoaXN0b3J5IjpbLTEzMzA0NzcwNTFdfQ== |
70 | | ---> |
| 116 | +--> |
0 commit comments