Sha256: 95b171d0bc03a5d581b4db19f6741b0abee8a1ac65237364f759c5e7b16adbe2

Contents?: true

Size: 1.12 KB

Versions: 8

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'nokogiri'
require 'table_of_contents/parser'

module Jekyll
  class TocTag < Liquid::Tag
    def render(context)
      return unless context.registers[:page]['toc'] == true
      content_html = context.registers[:page].content
      ::Jekyll::TableOfContents::Parser.new(content_html).build_toc
    end
  end

  module TableOfContentsFilter
    def toc_only(html)
      return html unless toc_enabled?
      ::Jekyll::TableOfContents::Parser.new(html, toc_config).build_toc
    end

    def inject_anchors(html)
      return html unless toc_enabled?
      ::Jekyll::TableOfContents::Parser.new(html, toc_config).inject_anchors_into_html
    end

    def toc(html)
      return html unless toc_enabled?
      ::Jekyll::TableOfContents::Parser.new(html, toc_config).toc
    end

    private

    def toc_enabled?
      @context.registers[:page]['toc'] == true
    end

    def toc_config
      @context.registers[:site].config['toc'] || {}
    end
  end
end

Liquid::Template.register_filter(Jekyll::TableOfContentsFilter)
# Liquid::Template.register_tag('toc', Jekyll::TocTag) # will be enabled at v1.0

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
jekyll-toc-0.9.0.beta1 lib/jekyll-toc.rb
jekyll-toc-0.8.0 lib/jekyll-toc.rb
jekyll-toc-0.8.0.rc1 lib/jekyll-toc.rb
jekyll-toc-0.8.0.beta2 lib/jekyll-toc.rb
jekyll-toc-0.8.0.beta1 lib/jekyll-toc.rb
jekyll-toc-0.7.1 lib/jekyll-toc.rb
jekyll-toc-0.7.0 lib/jekyll-toc.rb
jekyll-toc-0.7.0.beta1 lib/jekyll-toc.rb