jekyll markdown 内部链接

发布于 2024-10-10 22:05:24 字数 79 浏览 6 评论 0原文

Jekyll 使用 Markdown 格式的链接,但如何链接到内部内容?

[[link]] 

Jekyll uses Markdown-formatted links, but how can I link to internal content?

[[link]] 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

傲世九天 2024-10-17 22:05:24

您现在可以使用以下内容发布内部链接:

[Some Link]({% post_url 2010-07-21-name-of-post %})

Jekyll 中也引用了该链接文档

https://github.com/mojombo/jekyll/pull/369

You can now post internal links by using the following:

[Some Link]({% post_url 2010-07-21-name-of-post %})

This is also referenced in the Jekyll Documentation.

https://github.com/mojombo/jekyll/pull/369

少钕鈤記 2024-10-17 22:05:24

现在可以使用 link 标签链接到帖子以外的页面。 link 适用于帖子、页面、集合中的文档和文件。

{{ site.baseurl }}{% link _collection/name-of-document.md %}
{{ site.baseurl }}{% link _posts/2016-07-26-name-of-post.md %}
{{ site.baseurl }}{% link news/index.html %}
{{ site.baseurl }}{% link /assets/files/doc.pdf %}

使用 link 标记时请记住包含文件扩展名。
要使用它创建链接:

[Link to a document]({{ site.baseurl }}{% link _collection/name-of-document.md %})
[Link to a post]({{ site.baseurl }}{% link _posts/2016-07-26-name-of-post.md %})
[Link to a page]({{ site.baseurl }}{% link news/index.html %})
[Link to a file]({{ site.baseurl }}{% link /assets/files/doc.pdf %})

请参阅 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.

{{ site.baseurl }}{% link _collection/name-of-document.md %}
{{ site.baseurl }}{% link _posts/2016-07-26-name-of-post.md %}
{{ site.baseurl }}{% link news/index.html %}
{{ site.baseurl }}{% link /assets/files/doc.pdf %}

Remember to include the file extension when using the link tag.
To use it to create a link:

[Link to a document]({{ site.baseurl }}{% link _collection/name-of-document.md %})
[Link to a post]({{ site.baseurl }}{% link _posts/2016-07-26-name-of-post.md %})
[Link to a page]({{ site.baseurl }}{% link news/index.html %})
[Link to a file]({{ site.baseurl }}{% link /assets/files/doc.pdf %})

See Jekyll Documentation.

想念有你 2024-10-17 22:05:24

如果内部内容位于同一页面上,则可以使用auto_ids功能链接到它。您可以在 _config.yml 中启用此功能:

kramdown:
    auto_ids: true

启用此功能后,每个标题都会根据标题文本获得一个 id 引用。例如

### My Funky Heading

将变成

<h3 id="my-funky-heading">My Funky Heading</h3>

您可以通过执行以下操作从同一文档中链接到此内容:

The funky text is [described below](#my-funky-heading)

如果您愿意,您可以分配一个显式 id:

### My Funky Heading
{: #funky }

并链接到它

The funky text is [described below](#funky)

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:

kramdown:
    auto_ids: true

With this enabled each heading gets an id ref based on the heading text. For example

### My Funky Heading

will become

<h3 id="my-funky-heading">My Funky Heading</h3>

You can link to this from within the same document by doing something like this:

The funky text is [described below](#my-funky-heading)

You can assign an explicit id if you prefer:

### My Funky Heading
{: #funky }

and link to it

The funky text is [described below](#funky)
情仇皆在手 2024-10-17 22:05:24

对于页面,他们决定不添加 page_url 标记,因为您无论如何,必须知道页面的路径。所以你只需要手动链接到它:

[My page](/path/to/page.html)

或者如果你想以编程方式获取页面的标题,你可以做一些大而难看的事情:

{% for page in site.pages %}
  {% if page.url == '/path/to/page.html' %}
[{{ page.title }}]({{ page.url }})
  {% endif %}
{% endfor %}

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:

[My page](/path/to/page.html)

Or you can do something big and ugly like this if you want to programatically get the title of the page:

{% for page in site.pages %}
  {% if page.url == '/path/to/page.html' %}
[{{ page.title }}]({{ page.url }})
  {% endif %}
{% endfor %}
感性不性感 2024-10-17 22:05:24

Jekyll 中有多种链接方式,其中一些现在已经过时了。

使用链接标签

链接到内部文件的推荐方法是

[Link]({{ site.baseurl }}{% link path/to/file.md %})

请注意,如果文件移动或被删除,这将导致错误。

使用永久链接

要链接到页面而不导致错误(而是损坏的链接):

[Link]({{ '/path/to/page/' | relative_url }})

请注意,这里您需要知道页面的永久链接并将其通过 relative_url 过滤器传递,以确保其前缀为网站的基本网址。

页面的永久链接取决于配置文件中的permalink 设置以及文件前面的permalink 键。

使用 jekyll-relative-links

如果你想使用相对路径(并且希望链接在 GitHub 的 markdown 视图中工作),你应该使用 jekyll-relative-links。这使您可以编写如下链接:

[Link](./path/to/file.md)

[Link to file in parent folder](../file.md)

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

[Link]({{ site.baseurl }}{% link path/to/file.md %})

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):

[Link]({{ '/path/to/page/' | relative_url }})

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 the permalink 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:

[Link](./path/to/file.md)

[Link to file in parent folder](../file.md)
忆悲凉 2024-10-17 22:05:24

想象一下这是您的项目目录:

项目目录

要将“index.md”链接到文件夹“blog”内名为“20190920-post1.md”的文件,请执行以下操作:

  1. 打开文件“index.md”。
  2. 添加以下内容:

    [任何文本](./相对路径)

例如:

- [Sept 20th 2019 - Kikucare's Journey](./blog/20190920-post1.md)

输出:

在此处输入图像描述

Imagine this is your project directory:

project directory

To link "index.md" to a file inside folder "blog" called "20190920-post1.md", do the following:

  1. Open the file "index.md".
  2. Add the following:

    [any text](./relative path)

For example:

- [Sept 20th 2019 - Kikucare's Journey](./blog/20190920-post1.md)

Output:

enter image description here

橘和柠 2024-10-17 22:05:24

在 Jekyll 3.9.x 中工作:

在 HTML 中:
博客链接

Working in Jekyll 3.9.x:

In HTML:
<a href="{{ '/blog/' | relative_url }}">Link to blog</a>

好听的两个字的网名 2024-10-17 22:05:24

2023 年 9 月

由于 Jekyll 不知道您要包含哪个帖子,因此您需要包含完整的文件名。
您不需要包含扩展名。

日期提醒
请注意,

标记内输出的文件名和日期类别是不同的。这是因为该帖子在 Front Matter 中的日期不同。当页面呈现为 HTML 时,它会查看 Front Matter 日期。

{% post_url 2023-09-28-test-page %}

<!-- outputs this -->
<p>/2023/01/02/test-page.html</p>

要显示正确的 URL,

您需要用方括号 () 括起 Liquid 并使用 [ ] 包含锚文本。

[A Test Page]({% post_url 2023-09-28-test-page %})

<!-- outputs this -->
<p><a href="/2023/01/02/test-page.html">A Test Page</a></p>

链接后重命名文件

如果您在链接后重命名帖子文件,Jekyll 会抛出错误。

Error: Could not find post "2023-09-28-test-page" in tag 'post_url'. 
Make sure the post exists and the name is correct.
Error: Run jekyll build --trace for more information.

包含属性

您可以使用大括号来包含属性。注意开头的冒号。

[A Test Page]({% post_url 2023-09-28-test-page %}){:target="_blank"}

<!-- outputs this -->
<p><a href="/2023/01/02/test-23-page.html" target="_blank">A Test Page</a></p>

包含多个属性

要包含更多属性,您可以像添加常规 HTML 属性一样添加它们,后跟一个空格。

[A Test Page]({% post_url 2023-09-28-test-page %}){:target="_blank" class="active"}

<!-- outputs this -->
<p><a href="/2023/01/02/test-23-page.html" target="_blank" class="active">A Test Page</a></p>

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.

{% post_url 2023-09-28-test-page %}

<!-- outputs this -->
<p>/2023/01/02/test-page.html</p>

To display a proper URL

You need to wrap the Liquid with brackets ( and ) and use [ ] to include your anchor text.

[A Test Page]({% post_url 2023-09-28-test-page %})

<!-- outputs this -->
<p><a href="/2023/01/02/test-page.html">A Test Page</a></p>

Rename a file after linking

If you rename your post file after linking, Jekyll will throw an error.

Error: Could not find post "2023-09-28-test-page" in tag 'post_url'. 
Make sure the post exists and the name is correct.
Error: Run jekyll build --trace for more information.

Include an attribute

You can use curly brackets to include your attributes. Notice the colon at the beginning.

[A Test Page]({% post_url 2023-09-28-test-page %}){:target="_blank"}

<!-- outputs this -->
<p><a href="/2023/01/02/test-23-page.html" target="_blank">A Test Page</a></p>

Include multiple attributes

To include more attributes, you can add them like regular HTML attributes followed by a space.

[A Test Page]({% post_url 2023-09-28-test-page %}){:target="_blank" class="active"}

<!-- outputs this -->
<p><a href="/2023/01/02/test-23-page.html" target="_blank" class="active">A Test Page</a></p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文