Class: Banzai::Filter::TableOfContentsTagFilter

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

Overview

Using [[_TOC_]] or [TOC] (both case insensitive) on it’s own line, inserts a Table of Contents list.

[[_TOC_]] is based on the Gollum syntax. This way we have some consistency between wiki and normal markdown. Parser will have converted it into a wikilink.

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

Based on Banzai::Filter::GollumTagsFilter

Defined Under Namespace

Classes: HeaderNode

Constant Summary collapse

OR_SELF =
'descendant-or-self::text()'
TOC_QUERY =
%(#{OR_SELF}[parent::p and starts-with(translate(., '[TOC]', '[toc]'), '[toc]')]).freeze
GOLLUM_TOC_QUERY =
%(#{OR_SELF}[ancestor::a[@data-wikilink="true"] and starts-with(translate(., '_TOC_', '_toc_'), '_toc_')])
.freeze
HEADER_CSS =
'h1, h2, h3, h4, h5, h6'
HEADER_XPATH =
Gitlab::Utils::Nokogiri.css_to_xpath(HEADER_CSS).freeze

Constants included from Concerns::PipelineTimingCheck

Concerns::PipelineTimingCheck::MAX_PIPELINE_SECONDS

Instance Method Summary collapse

Methods included from Concerns::PipelineTimingCheck

#exceeded_pipeline_max?

Instance Method Details

#callObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/banzai/filter/table_of_contents_tag_filter.rb', line 27

def call
  return doc unless MarkdownFilter.glfm_markdown?(context)
  return doc if context[:no_header_anchors]

  doc.xpath(GOLLUM_TOC_QUERY).each do |node|
    process_toc_tag(node.parent) if toc_tag_gollum?(node)
  end

  doc.xpath(TOC_QUERY).each do |node|
    next unless node.parent.children.size == 1 &&
      node.text? &&
      node.content.strip.casecmp?('[toc]')

    process_toc_tag(node)
  end

  doc
end