diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-10 12:08:56 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-10 12:08:56 +0300 |
commit | b4ded0ba7b4d2cdbed5b1f331cf2083a25ee4d7c (patch) | |
tree | 6694fa9d8f3e226597cc01dfb8e3e07b50ae85b6 /lib/banzai | |
parent | 2aaef94c80937d9d188f7b9cbbad2dcd1508c3c1 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/banzai')
-rw-r--r-- | lib/banzai/filter/gollum_tags_filter.rb | 36 | ||||
-rw-r--r-- | lib/banzai/filter/table_of_contents_tag_filter.rb | 45 | ||||
-rw-r--r-- | lib/banzai/pipeline/gfm_pipeline.rb | 1 |
3 files changed, 51 insertions, 31 deletions
diff --git a/lib/banzai/filter/gollum_tags_filter.rb b/lib/banzai/filter/gollum_tags_filter.rb index 0c1bbd2d250..dec4ec871f1 100644 --- a/lib/banzai/filter/gollum_tags_filter.rb +++ b/lib/banzai/filter/gollum_tags_filter.rb @@ -25,12 +25,10 @@ module Banzai # * [[http://example.com/images/logo.png]] # * [[http://example.com/images/logo.png|alt=Logo]] # - # - Insert a Table of Contents list: - # - # * [[_TOC_]] - # # Based on Gollum::Filter::Tags # + # Note: the table of contents tag is now handled by TableOfContentsTagFilter + # # Context options: # :project_wiki (required) - Current project wiki. # @@ -64,23 +62,11 @@ module Banzai def call doc.search(".//text()").each do |node| next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS) + next unless node.content =~ TAGS_PATTERN - # A Gollum ToC tag is `[[_TOC_]]`, but due to MarkdownFilter running - # before this one, it will be converted into `[[<em>TOC</em>]]`, so it - # needs special-case handling - if toc_tag?(node) - process_toc_tag(node) - else - content = node.content - - next unless content =~ TAGS_PATTERN - - html = process_tag($1) + html = process_tag($1) - if html && html != node.content - node.replace(html) - end - end + node.replace(html) if html && html != node.content end doc @@ -88,12 +74,6 @@ module Banzai private - # Replace an entire `[[<em>TOC</em>]]` node with the result generated by - # TableOfContentsFilter - def process_toc_tag(node) - node.parent.parent.replace(result[:toc].presence || '') - end - # Process a single tag into its final HTML form. # # tag - The String tag contents (the stuff inside the double brackets). @@ -129,12 +109,6 @@ module Banzai end end - def toc_tag?(node) - node.content == 'TOC' && - node.parent.name == 'em' && - node.parent.parent.text == '[[TOC]]' - end - def image?(path) path =~ ALLOWED_IMAGE_EXTENSIONS end diff --git a/lib/banzai/filter/table_of_contents_tag_filter.rb b/lib/banzai/filter/table_of_contents_tag_filter.rb new file mode 100644 index 00000000000..13d0a6a4cc7 --- /dev/null +++ b/lib/banzai/filter/table_of_contents_tag_filter.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module Banzai + module Filter + # Using `[[_TOC_]]`, inserts a Table of Contents list. + # This syntax is based on the Gollum syntax. This way we have + # some consistency between with wiki and normal markdown. + # If there ever emerges a markdown standard, we can implement + # that here. + # + # The support for this has been removed from GollumTagsFilter + # + # Based on Banzai::Filter::GollumTagsFilter + class TableOfContentsTagFilter < HTML::Pipeline::Filter + TEXT_QUERY = %q(descendant-or-self::text()[ancestor::p and contains(., 'TOC')]) + + def call + return doc if context[:no_header_anchors] + + doc.xpath(TEXT_QUERY).each do |node| + # A Gollum ToC tag is `[[_TOC_]]`, but due to MarkdownFilter running + # before this one, it will be converted into `[[<em>TOC</em>]]`, so it + # needs special-case handling + process_toc_tag(node) if toc_tag?(node) + end + + doc + end + + private + + # Replace an entire `[[<em>TOC</em>]]` node with the result generated by + # TableOfContentsFilter + def process_toc_tag(node) + node.parent.parent.replace(result[:toc].presence || '') + end + + def toc_tag?(node) + node.content == 'TOC' && + node.parent.name == 'em' && + node.parent.parent.text == '[[TOC]]' + end + end + end +end diff --git a/lib/banzai/pipeline/gfm_pipeline.rb b/lib/banzai/pipeline/gfm_pipeline.rb index f6c12cdb53b..dad0d95685e 100644 --- a/lib/banzai/pipeline/gfm_pipeline.rb +++ b/lib/banzai/pipeline/gfm_pipeline.rb @@ -32,6 +32,7 @@ module Banzai Filter::InlineMetricsFilter, Filter::InlineGrafanaMetricsFilter, Filter::TableOfContentsFilter, + Filter::TableOfContentsTagFilter, Filter::AutolinkFilter, Filter::ExternalLinkFilter, Filter::SuggestionFilter, |