2

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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
gnujoow
  • 374
  • 1
  • 3
  • 16
  • Did you know that kramdown has a TOC function that might suit you needs ? See http://kramdown.gettalong.org/converter/html.html#toc – David Jacquel Nov 22 '16 at 13:58
  • @DavidJacquel I already know `kramdown` has toc feature. as I know kramdown's toc feature only can be used in the same document. In my purpose of generating toc is render toc in another page and that's why I built custom toc generator with ruby. – gnujoow Nov 23 '16 at 06:21

1 Answers1

6

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.

Tom Slominski
  • 140
  • 1
  • 2
  • 8
  • If you came from google you are most likely in the place to write a better question for your answer. Please do so!! Self-Answering questions is something that can be quite usefull for the community as a whole. – Luuklag Apr 03 '18 at 12:13