将多个参数传递给 jekyll 中的自定义插件

pass multiple argument to custom plugin in jekyll

我正在使用 jekyll 制作一个网站。
我用 ruby.

构建了内容插件的自定义 table

这是代码

require 'nokogiri'

module Jekyll

  module TOCGenerator
    TOC_CONTAINER_HTML = '<ul>%1</ul>'

    def toc(html,op)
      toc_top_tag = "h1"
      item_number = 0
      toc_html = ''
      sub_url = html['url']

      doc = Nokogiri::HTML(html['content'])
      doc.css(toc_top_tag).each do |tag|
        toc_html += create_level_html(sub_url, tag['id'], tag.text)
        item_number += 1
      end

      return '' unless item_number > 0

      if 0 < item_number
        toc_table = TOC_CONTAINER_HTML
        .gsub('%1', toc_html)
      end
    end

    private
    def create_level_html(url, anchor_id, tocText)
      link = '<a href="%1#%2">%3</a>'
      .gsub('%1', url)
      .gsub('%2', anchor_id.to_s)
      .gsub('%3', tocText)
      '<li>%1</li>'
      .gsub('%1', link)
    end
  end
end

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

在一些文档中

<div>
{{ page | toc }}
</div>

效果很好。

为了增强其功能,我想为渲染添加一些参数 toc。所以我像这样添加了函数的参数头。

def toc(html,option)

但是当我在jekyll模板中调用函数时,出现了这样的错误

  Liquid Exception: Liquid error (line 41): wrong number of arguments (given 1, expected 2) in /_layouts/default.html

我已经尝试 {{ (three,1) | toc }}{{ three, 1 | toc }}{{ three | 1 | toc }} 调用带有 2 个参数的函数,但结果都是一样的。

如何在 jekyll 中使用多个参数调用函数?

在此先致谢。

这个答案不太可能与原始发布者相关,但如果有人像我一样从 Google 来到这里,我就是这样解决的。

插件代码:

module Jekyll
  module YourFilter
    def yourFilter( input, arg1, arg2 )
      # your code
    end
  end
end

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

您的内容中的标签代码:

{{ 'Lorem ipsum' | yourFilter: 'argument 1', 'argument 2' }}

关键是标签代码中过滤器名称后面有一个分号。这似乎允许插件解析多个参数,而不仅仅是最后一个参数。