Class: Banzai::Filter::TableOfContentsTagFilter

Inherits:
HTML::Pipeline::Filter
  • Object
show all
Defined in:
lib/banzai/filter/table_of_contents_tag_filter.rb

Overview

Using `[[TOC]]` or `[TOC]` (both case insensitive), inserts a Table of Contents list.

`[[TOC]]` is based on the Gollum syntax. This way we have some consistency between with wiki and normal markdown. The support for this has been removed from GollumTagsFilter

`[toc]` is a generally accepted form, used by Typora for example.

Based on Banzai::Filter::GollumTagsFilter

Constant Summary collapse

TEXT_QUERY =
%q(descendant-or-self::text()[ancestor::p and contains(translate(., 'TOC', 'toc'), 'toc')])

Instance Method Summary collapse

Instance Method Details

#callObject


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/banzai/filter/table_of_contents_tag_filter.rb', line 17

def call
  return doc if context[:no_header_anchors]

  doc.xpath(TEXT_QUERY).each do |node|
    if toc_tag?(node)
      # Support [TOC] / [toc] tags, which don't have a wrapping <em>-tag
      process_toc_tag(node)
    elsif toc_tag_em?(node)
      # Support Gollum like ToC tag (`[[_TOC_]]` / `[[_toc_]]`), which will be converted
      # into `[[<em>TOC</em>]]` by the markdown filter, so it
      # needs special-case handling
      process_toc_tag_em(node)
    end
  end

  doc
end