1

In GitHub pages, absolute links are not working as expected. My goal is that all links in the document are absolute in regards to the root directory, and when rendered in GitHub pages, to have it prefix links with the project name so all absolute URLs in links work correctly (e.g. /test/page.md to /<PROJECT_NAME>/test/page.html. However, I'm running into issues where links from pages in subfolders are being transformed incorrectly. For example, here is a sample file structure:

  • index.md
  • doc.md
  • folder
    • index.md
    • doc2.md

My _config.yml contains the following:

baseurl: "/<PROJECT_NAME>"

Inside index.md, I have the following link: [Link Text](/folder/index.md). This correctly get converted to an HTML link pointing to /<PROJECT_NAME>/folder/index.html. A link in index.md to [Link Text](/doc.md) also works as expected.

Inside /folder/index.md I have [Link Text](/folder/doc2.md). This is incorrectly converted to /folder/doc2.md, while it should be <PROJECT_NAME>/folder/doc2.html. Similarly, a [Link Text](/doc.md) is incorrectly converted to /doc.md, while it should be /<PROJECT_NAME>/doc.html.

In summary: absolute links are correctly translated to include the base URL in the root directly, but not in any subfolders.

Any ideas?

K. Barresi
  • 1,205
  • 1
  • 20
  • 45

2 Answers2

0

What you're trying to use are relatives urls, relatives to root.

For them to work, you have to reference your site.baseurl.

[Link Text]({{ site.baseurl }}/folder/index.md) or [Link Text]({{ site.baseurl }}/index.md) will work.

David Jacquel
  • 46,880
  • 4
  • 106
  • 132
  • Is there any way to accomplish this without using `{{ site.baseurl }}`, i.e. without breaking links in vanilla Markdown? – K. Barresi Apr 20 '18 at 15:01
  • @joosts answer can be a solution, but base href can be tricky. See https://stackoverflow.com/questions/1889076 – David Jacquel Apr 21 '18 at 06:31
0

You might want to try this (in the head):

<head>
  <base href="{{ site.baseurl }}">
</head>

Source

JoostS
  • 9,344
  • 3
  • 27
  • 50