File: jekyll-toc.rb

package info (click to toggle)
ruby-jekyll-toc 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 244 kB
  • sloc: ruby: 912; makefile: 9
file content (53 lines) | stat: -rw-r--r-- 1,326 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

require 'nokogiri'
require 'table_of_contents/configuration'
require 'table_of_contents/parser'

module Jekyll
  # toc tag for Jekyll
  class TocTag < Liquid::Tag
    def render(context)
      return '' unless context.registers[:page]['toc']

      content_html = context.registers[:page]['content']
      toc_config = context.registers[:site].config['toc'] || {}
      TableOfContents::Parser.new(content_html, toc_config).build_toc
    end
  end

  # Jekyll Table of Contents filter plugin
  module TableOfContentsFilter
    # Deprecated method. Removed in v1.0.
    def toc_only(html)
      return '' unless toc_enabled?

      TableOfContents::Parser.new(html, toc_config).build_toc
    end

    def inject_anchors(html)
      return html unless toc_enabled?

      TableOfContents::Parser.new(html, toc_config).inject_anchors_into_html
    end

    def toc(html)
      return html unless toc_enabled?

      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