簡體   English   中英

Jekyll:從 markdown 文件調用的變量中的鏈接

[英]Jekyll: link inside a variable called from a markdown file

我在 Jekyll 中有一系列帖子,我希望在底部有一個帶有指向內部頁面鏈接的橫幅。

這是我的 MD 帖子的開頭:

---
layout: post
title:  "Title"
date:   2022-12-06 18:26:05 +0100
categories: category
author: Author name
author-pic: author.jpg
banner-bottom: Some test some test some test [Link to a page]({{ site.baseurl | }}{% link page.html %}).
---

在帖子布局中,我在<div>中調用變量{{page.banner-bottom}}

{% include header.html %}

        <article>
            <h1>{{ page.title }}</h1>
            <div class="author-name">
                <img src="{{ site.baseurl }}/{{ page.author-pic }}" alt=""><span>{{ page.author }}</span>
            </div>

            {{content}}

            <div class="banner-bottom">{{page.banner-bottom}}</div>

    </article>

{% include footer.html %}

不幸的是[Link to a page]({{ site.baseurl }}{% link alumni.html %})呈現為文本,未轉換為鏈接。 知道為什么嗎? 我也試過 escaping site.baseurl like {{ site.baseurl | escape }} {{ site.baseurl | escape }}沒有運氣。

這篇SO 帖子:Custom Variables in Jekyll Front Matter在某種程度上是相關的,但已經很老了。

這是一個想法,實際上不是我自己的。 我使用了這篇博文中的解決方案:How to nest template variables inside yaml front matter once。

這個想法很簡單:

1.創建插件:

# file: _plugins/expand_nested_variable_filter.rb

module Jekyll
  module ExpandNestedVariableFilter
    def flatify(input)
      Liquid::Template.parse(input).render(@context)
    end
  end
end

Liquid::Template.register_filter(Jekyll::ExpandNestedVariableFilter)

2. 應用過濾器:

---
layout: post
title:  "Title"
date:   2022-12-06 18:26:05 +0100
categories: category
author: Author name
author-pic: author.jpg
banner-bottom: Some test and [link to a page]({{ site.baseurl }}{% link page.html %}).
---

{{ site.baseurl | flatify }}

Jekyll 從根文件夾搜索鏈接,因此您可能需要調整路徑。 當你的鏈接無效時,Jekyll 會告訴你:) 我最終用我的 404.html 頁面進行了測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM