jekyll markdown 内部链接
Jekyll 使用 Markdown 格式的链接,但如何链接到内部内容?
[[link]]
Jekyll uses Markdown-formatted links, but how can I link to internal content?
[[link]]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您现在可以使用以下内容发布内部链接:
Jekyll 中也引用了该链接文档。
https://github.com/mojombo/jekyll/pull/369
You can now post internal links by using the following:
This is also referenced in the Jekyll Documentation.
https://github.com/mojombo/jekyll/pull/369
现在可以使用
link
标签链接到帖子以外的页面。link
适用于帖子、页面、集合中的文档和文件。使用
link
标记时请记住包含文件扩展名。要使用它创建链接:
请参阅 Jekyll 文档。
It is now possible to link to pages other than posts using the
link
tag.link
works for posts, pages, documents in a collection, and files.Remember to include the file extension when using the
link
tag.To use it to create a link:
See Jekyll Documentation.
如果内部内容位于同一页面上,则可以使用
auto_ids
功能链接到它。您可以在_config.yml
中启用此功能:启用此功能后,每个标题都会根据标题文本获得一个
id
引用。例如将变成
您可以通过执行以下操作从同一文档中链接到此内容:
如果您愿意,您可以分配一个显式 id:
并链接到它
If the internal content is on the same page then it is possible to link to it using the
auto_ids
feature. You enable this in_config.yml
:With this enabled each heading gets an
id
ref based on the heading text. For examplewill become
You can link to this from within the same document by doing something like this:
You can assign an explicit id if you prefer:
and link to it
对于页面,他们决定不添加
page_url
标记,因为您无论如何,必须知道页面的路径。所以你只需要手动链接到它:或者如果你想以编程方式获取页面的标题,你可以做一些大而难看的事情:
For pages, they decided not to add a
page_url
tag because you'd have to know the path of the page anyway. So you just have to link to it manually:Or you can do something big and ugly like this if you want to programatically get the title of the page:
Jekyll 中有多种链接方式,其中一些现在已经过时了。
使用链接标签
链接到内部文件的推荐方法是
请注意,如果文件移动或被删除,这将导致错误。
使用永久链接
要链接到页面而不导致错误(而是损坏的链接):
请注意,这里您需要知道页面的永久链接并将其通过
relative_url
过滤器传递,以确保其前缀为网站的基本网址。页面的永久链接取决于配置文件中的
permalink
设置以及文件前面的permalink
键。使用 jekyll-relative-links
如果你想使用相对路径(并且希望链接在 GitHub 的 markdown 视图中工作),你应该使用
jekyll-relative-links
。这使您可以编写如下链接:There are multiple ways of linking in Jekyll, some of which are now outdated.
With link tags
The recommended way to link to internal files is
Note that this will cause an error if the file moves or gets deleted.
With permalinks
To link to a page without causing errors (broken links instead):
Note that here you need to know the permalink of the page and pass it through the
relative_url
filter to ensure that it is prefixed with the base url of the site.The permalink of a page depends on the
permalink
setting in your config file and thepermalink
key in the front matter of the file.With jekyll-relative-links
If you want to use relative paths (and want the links to work in GitHub's markdown view), you should use
jekyll-relative-links
. This lets you write links like:想象一下这是您的项目目录:
要将“index.md”链接到文件夹“blog”内名为“20190920-post1.md”的文件,请执行以下操作:
添加以下内容:
[任何文本](./相对路径)
例如:
输出:
Imagine this is your project directory:
To link "index.md" to a file inside folder "blog" called "20190920-post1.md", do the following:
Add the following:
[any text](./relative path)
For example:
Output:
在 Jekyll 3.9.x 中工作:
在 HTML 中:
博客链接
Working in Jekyll 3.9.x:
In HTML:
<a href="{{ '/blog/' | relative_url }}">Link to blog</a>
2023 年 9 月
由于 Jekyll 不知道您要包含哪个帖子,因此您需要包含完整的文件名。
您不需要包含扩展名。
日期提醒
请注意,
标记内输出的文件名和日期类别是不同的。这是因为该帖子在 Front Matter 中的日期不同。当页面呈现为 HTML 时,它会查看 Front Matter 日期。
要显示正确的 URL,
您需要用方括号
(
和)
括起 Liquid 并使用[
]
包含锚文本。链接后重命名文件
如果您在链接后重命名帖子文件,Jekyll 会抛出错误。
包含属性
您可以使用大括号来包含属性。注意开头的冒号。
包含多个属性
要包含更多属性,您可以像添加常规 HTML 属性一样添加它们,后跟一个空格。
Sep 2023
Since Jekyll doesn't know which post you want to include, you need to include the full file name.
You do not need to include the extension.
Date alert
Notice that the filename and the date category that output inside the
<p>
tag are different. That is because the post has a different date in the Front Matter. When the page renders to HTML, it looks at the Front Matter date.To display a proper URL
You need to wrap the Liquid with brackets
(
and)
and use[
]
to include your anchor text.Rename a file after linking
If you rename your post file after linking, Jekyll will throw an error.
Include an attribute
You can use curly brackets to include your attributes. Notice the colon at the beginning.
Include multiple attributes
To include more attributes, you can add them like regular HTML attributes followed by a space.