rubyjekyllliquidjekyll-extensions

Pass multiple arguments to custom plugin in jekyll


I'm working with jekyll to make a website.
And I built a custom table of content plugin with Ruby.

Here is the code:

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)

And in some document:

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

It works well.

To enhance its feature, I would like to add some argument to render toc. So I added argument head of the function just like this.

def toc(html,option)

But when I call the function in jekyll template, an error occurs like this:

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

I have tried {{ (three,1) | toc }}, {{ three, 1 | toc }}, {{ three | 1 | toc }} to call the function with 2 arguments but the results turned out the same way.

How do I call a function in jekyll with multiple arguments?


Solution

  • This answer is unlikely to be relevant to the original poster, but if anyone has come here from Google, like I did, here's how I solved it.

    Plugin code:

    module Jekyll
      module YourFilter
        def yourFilter( input, arg1, arg2 )
          # your code
        end
      end
    end
    
    Liquid::Template.register_filter(Jekyll::YourFilter)
    

    Tag code in your content:

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

    The key thing is that there's a semicolon after the filter name in the tag code. This seems to allow the plugin to parse multiple arguments, and not just the last one.