如何检测 Jekyll 标签插件中的当前页面?

发布于 2024-12-05 07:43:36 字数 224 浏览 1 评论 0原文

我有一个 Jekyll(液体)块插件,我想检测当前页面是什么。我看到上下文被传递到渲染中,并且我可以将当​​前站点对象检索为 context.registers[:site]。但是,尝试以 context.registers[:page] 形式获取当前页面会失败。

我试图解决的问题是创建一个简单的块插件来检测当前页面是否是标记中提到的页面,并突出显示它。

任何提示将不胜感激。

谢谢!

I have a Jekyll (Liquid) block plugin and I'd like to detect what the current page is. I see that a context is passed into render, and that I can retrieve the current site object as context.registers[:site]. However, attempts to get the current page as context.registers[:page] fail.

The problem I'm trying to solve is to create a simple block plugin to detect if the current page is the page mentioned in the tag, and highlight it.

Any tips would be greatly appreciated.

Thanks!

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

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

发布评论

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

评论(4

装纯掩盖桑 2024-12-12 07:43:36

事实证明,我们也可以这样做:

  def render(context)
    page_url = context.environments.first["page"]["url"]

这并不明显,但不需要修补代码。

Turns out we can also do this like:

  def render(context)
    page_url = context.environments.first["page"]["url"]

Which is not obvious but it doesn't require patching the code.

哭泣的笑容 2024-12-12 07:43:36

context['page'] 似乎返回包含当前页面大部分属性的哈希值,包括 urlpath

因此 实际页面对象可以通过以下方式检索

context.registers[:site].pages.detect { |p| p.path==context['page']['path'] }

context['page'] seems to return a hash with most of the properties of the current page, including url and path

so the actual page object can be retrieved with

context.registers[:site].pages.detect { |p| p.path==context['page']['path'] }
千鲤 2024-12-12 07:43:36

我认为按原样使用 Jekyll 没有什么好方法。 convertible.rb 仅将 site 对象传递给 Liquid,其中不包含任何特定于页面的数据。

我建议只编辑 convertible.rb 来传递您需要的数据,向主项目提交拉取请求以拉取您的更改,并在本地使用您的 fork 来生成您的网站。开源万岁!

以下简单的补丁适用于我在本地针对 Jekyll 0.11.0 的情况,使页面哈希在 Liquid 中可用 context.registers[:page] (注意:此时它是一个预先转换的哈希,因此您将访问 context.registers[:page]['url'],而不是 context.registers[:page].url):

diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb
index d33abc5..a674ef5 100644
--- a/lib/jekyll/convertible.rb
+++ b/lib/jekyll/convertible.rb
@@ -69,7 +69,7 @@ module Jekyll
     #
     # Returns nothing.
     def do_layout(payload, layouts)
-      info = { :filters => [Jekyll::Filters], :registers => { :site => self.site } }
+      info = { :filters => [Jekyll::Filters], :registers => { :site => self.site, :page => payload['page'] } }

       # render and transform content (this becomes the final content of the object)
       payload["pygments_prefix"] = converter.pygments_prefix

希望有帮助!

I don't think there's a good way of doing it with Jekyll as-is. convertible.rb only passes the site object to Liquid, which doesn't contain any page-specific data.

I'd suggest just editing convertible.rb to pass in the data you need, submitting a pull request to the main project to pull in your changes, and using your fork locally to generate your site. Hooray for open source!

The following trivial patch works for me locally against Jekyll 0.11.0, making a page hash available in Liquid as context.registers[:page] (note: it's a pre-converted hash at this point, so you'd access context.registers[:page]['url'], not context.registers[:page].url):

diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb
index d33abc5..a674ef5 100644
--- a/lib/jekyll/convertible.rb
+++ b/lib/jekyll/convertible.rb
@@ -69,7 +69,7 @@ module Jekyll
     #
     # Returns nothing.
     def do_layout(payload, layouts)
-      info = { :filters => [Jekyll::Filters], :registers => { :site => self.site } }
+      info = { :filters => [Jekyll::Filters], :registers => { :site => self.site, :page => payload['page'] } }

       # render and transform content (this becomes the final content of the object)
       payload["pygments_prefix"] = converter.pygments_prefix

Hope that helps!

半步萧音过轻尘 2024-12-12 07:43:36

当制作需要以稍微不同的方式显示当前页面的菜单时,总是会出现此问题。这是我为 Bootstrap 5 编写的 Jekyll 插件:

# Copyright 2020 Michael Slinn
#
# Apache 2 License
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.

module Jekyll
  # Generates a Bootstrap 5 nav item.
  # 
  # Usage: {% nav_item icon link text %}
  #   Quotes are not used
  #   icon is assumed to be within the /assets/icons directory
  #   link must reference a local web page, do not preface with http: or https:
  #   text can be one or more words
  # 
  # Example:           
  # {% nav_item house-door.svg /index.html Welcome! %}

  class Bootstrap5NavItem < Liquid::Tag

    def initialize(href, command_line, tokens)
      super

      @active = '"'

      tokens = command_line.strip.split(" ")

      @icon = tokens.shift
      @link = tokens.shift
      @text = tokens.join(" ").strip
    end

    def render(context)
      relative_link = @link.delete_prefix('/')
      page = context['page']['path']  # relative to site root
      #puts "******* page=#{page}; @link=#{@link}"

      if page == relative_link then
        %Q(<li>
          <a class="nav-link active" aria-current="page" href="#">
            <img class="feather" src="/assets/icons/#{@icon}">
            #{@text}
          </a>
        </li>)
      else
        %Q(<li>
          <a class="nav-link" href="#{@link}">
            <img class="feather" src="/assets/icons/#{@icon}">
            #{@text}
          </a>
        </li>)
      end
    end
  end
end

Liquid::Template.register_tag('nav_item', Jekyll::Bootstrap5NavItem)

This issue always arises when making menu that needs to display the current page a little differently. Here is a Jekyll plugin I wrote that does that for Bootstrap 5:

# Copyright 2020 Michael Slinn
#
# Apache 2 License
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.

module Jekyll
  # Generates a Bootstrap 5 nav item.
  # 
  # Usage: {% nav_item icon link text %}
  #   Quotes are not used
  #   icon is assumed to be within the /assets/icons directory
  #   link must reference a local web page, do not preface with http: or https:
  #   text can be one or more words
  # 
  # Example:           
  # {% nav_item house-door.svg /index.html Welcome! %}

  class Bootstrap5NavItem < Liquid::Tag

    def initialize(href, command_line, tokens)
      super

      @active = '"'

      tokens = command_line.strip.split(" ")

      @icon = tokens.shift
      @link = tokens.shift
      @text = tokens.join(" ").strip
    end

    def render(context)
      relative_link = @link.delete_prefix('/')
      page = context['page']['path']  # relative to site root
      #puts "******* page=#{page}; @link=#{@link}"

      if page == relative_link then
        %Q(<li>
          <a class="nav-link active" aria-current="page" href="#">
            <img class="feather" src="/assets/icons/#{@icon}">
            #{@text}
          </a>
        </li>)
      else
        %Q(<li>
          <a class="nav-link" href="#{@link}">
            <img class="feather" src="/assets/icons/#{@icon}">
            #{@text}
          </a>
        </li>)
      end
    end
  end
end

Liquid::Template.register_tag('nav_item', Jekyll::Bootstrap5NavItem)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文