问题
tl;dr I want Markdown, relative links, Jekyll, and Github pages to play well together.
I would like to be able to view a file that contains working vanilla Markdown style relative links (i.e. [a relative link](other_file.md)
) on Github and on Github pages using Jekyll.
So far this works with viewing said file on Github since Github added support for relative links.
However this doesn't work out of the box in Jekyll because it expects {{ site.baseurl }}{{ post.url }}
.
Is there some preprocessor or other method I could use to get vanilla Markdown style relative links working in Jekyll without using any 'unsafe' plugins?
回答1:
Markdown, relative links, Jekyll, and Github pages are playing well together
Markdown
Default Markdown parser for Jekyll is Kramdown. It allows you to write markdown in file (page.md or page.markdown) and in variables.
When you have some markdown in a variable, you can parse it with {{ variable | markdownify }}
Markdown links
Your example link ([a relative link](other_file.md)
) will render a perfect relative link from you page to the other_file.md sibling page.
You are free to use this syntax.
{{ site.baseurl }}
and {{ post.url }}
variables
Those variables are not mandatory in Jekyll.
{{ post.url }}
is often found in for loops and result in links like [{{ post.title }}]({{ post.url }})
and this is only automation. And you're not obliged to use automation.
{{ site.baseurl }}
(which is different from {{ site.url }}
) is a very useful variable to build links relative to site root.
A link like [a relative link](/other_file.md)
will only work if your site is at the root of a domain, because it converted like /other_file.html
.
If you site is hosted at domain.tld.blog
, this link will miss is target.
If you set baseurl: /blog
in your _config.yml
file.
Adding [a relative link]({{ site.baseurl }}/other_file.md)
will give you the perfect relative link to root : /blog/other_file.html
.
回答2:
I am using Cory Gross docsync method which uses javascript after the page is loaded to manipulate relative links. This allows for vanilla style links.
来源:https://stackoverflow.com/questions/27879849/how-do-i-get-vanilla-markdown-style-relative-links-to-work-with-vanilla-jekyll-a