Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

avoid duplicate toc numberings (#8891)

ulfertsf453772ed877
diff --git a/lib/open_project/text_formatting/filters/table_of_contents_filter.rb b/lib/open_project/text_formatting/filters/table_of_contents_filter.rb
index 63c20667d79..8cf56e94b7f 100644
--- a/lib/open_project/text_formatting/filters/table_of_contents_filter.rb
+++ b/lib/open_project/text_formatting/filters/table_of_contents_filter.rb
@@ -70,22 +70,22 @@ module OpenProject::TextFormatting
##
# Appends the header link and returns
# a toc item.
+ # The item is prefixed by a number. If there is already a number prefix provided in the text,
+ # that prefix is used if it matches the calculated number.
def process_item(node, number)
text = node.text
return ''.html_safe unless text.present?
- id = get_unique_id(node.text)
+ id = get_unique_id(text)
add_header_link(node, id)
content_tag(:li, class: 'op-uc-toc--list-item') do
- number = content_tag(:span, number, class: 'op-uc-toc--list-item-number');
- content = content_tag(:span, node.text, class: 'op-uc-toc--list-item-title');
- content_tag(:a, number + content, href: "##{id}", class: 'op-uc-toc--item-link')
+ anchor_tag(text, number, id)
end
end
def get_heading_number(parent_number, num_in_level)
- parent_number == "" ? num_in_level.to_s : parent_number + "." + num_in_level.to_s
+ parent_number == "" ? num_in_level.to_s : "#{parent_number}.#{num_in_level}"
end
def render_nested(level = 0, parent_number = "")
@@ -99,7 +99,7 @@ module OpenProject::TextFormatting
if level == node_level
# We will render this node
node = headings.shift
- num_in_level = num_in_level + 1
+ num_in_level = num_in_level + 1
current_number = get_heading_number(parent_number, num_in_level)
result << process_item(node, current_number)
elsif level < node_level
@@ -126,13 +126,23 @@ module OpenProject::TextFormatting
if headings.empty?
I18n.t(:label_wiki_toc_empty)
else
- render_nested()
+ render_nested
end
end
rescue StandardError => e
Rails.logger.error { "Failed to render table of contents: #{e} #{e.message}" }
end
+
+ private
+
+ def anchor_tag(text, number, id)
+ parsed_text = text.match(Regexp.new("^(#{number}[.)]*)?(.+)$"))
+ number = parsed_text[1] || number
+ number_span = content_tag(:span, number, class: 'op-uc-toc--list-item-number')
+ content_span = content_tag(:span, parsed_text[2].strip, class: 'op-uc-toc--list-item-title')
+ content_tag(:a, number_span + content_span, href: "##{id}", class: 'op-uc-toc--item-link')
+ end
end
end
end
diff --git a/spec/lib/open_project/text_formatting/markdown/toc_macro_spec.rb b/spec/lib/open_project/text_formatting/markdown/toc_macro_spec.rb
index c576cae936a..9e30a83e924 100644
--- a/spec/lib/open_project/text_formatting/markdown/toc_macro_spec.rb
+++ b/spec/lib/open_project/text_formatting/markdown/toc_macro_spec.rb
@@ -149,4 +149,123 @@ describe OpenProject::TextFormatting,
EXPECTED
end
end
+
+ context 'headings with numbers' do
+ it_behaves_like 'format_text produces' do
+ let(:raw) do
+ <<~RAW
+ <macro class="toc op-uc-placeholder"></macro>
+
+ # 1 The first h1 heading
+
+ Some text after the first h1 heading
+
+ ## 1.1 The first h2 heading
+
+ Some text after the first h2 heading
+
+ ### 1.1.1. The first h3 heading
+
+ Some text after the first h3 heading
+
+ # 2) The second h1 heading
+
+ Some text after the second h1 heading
+
+ ## 2.1) The second h2 heading
+
+ Some text after the second h2 heading
+
+ ### 2.1.1 - The second h3 heading
+
+ Some text after the second h3 heading
+ RAW
+ end
+
+ let(:expected) do
+ <<~EXPECTED
+ <p class="op-uc-p">
+ <nav class="op-uc-toc">
+ <ul class="op-uc-toc--list">
+ <li class="op-uc-toc--list-item">
+ <a class="op-uc-toc--item-link op-uc-link" href="#1-the-first-h1-heading">
+ <span class="op-uc-toc--list-item-number">1</span>
+ <span class="op-uc-toc--list-item-title">The first h1 heading</span>
+ </a>
+ </li>
+ <ul class="op-uc-toc--list">
+ <li class="op-uc-toc--list-item">
+ <a class="op-uc-toc--item-link op-uc-link" href="#11-the-first-h2-heading">
+ <span class="op-uc-toc--list-item-number">1.1</span>
+ <span class="op-uc-toc--list-item-title">The first h2 heading</span>
+ </a>
+ </li>
+ <ul class="op-uc-toc--list">
+ <li class="op-uc-toc--list-item">
+ <a class="op-uc-toc--item-link op-uc-link" href="#111-the-first-h3-heading">
+ <span class="op-uc-toc--list-item-number">1.1.1.</span>
+ <span class="op-uc-toc--list-item-title">The first h3 heading</span>
+ </a>
+ </li>
+ </ul>
+ </ul>
+ <li class="op-uc-toc--list-item">
+ <a class="op-uc-toc--item-link op-uc-link" href="#2-the-second-h1-heading">
+ <span class="op-uc-toc--list-item-number">2)</span>
+ <span class="op-uc-toc--list-item-title">The second h1 heading</span>
+ </a>
+ </li>
+ <ul class="op-uc-toc--list">
+ <li class="op-uc-toc--list-item">
+ <a class="op-uc-toc--item-link op-uc-link" href="#21-the-second-h2-heading">
+ <span class="op-uc-toc--list-item-number">2.1)</span>
+ <span class="op-uc-toc--list-item-title">The second h2 heading</span>
+ </a>
+ </li>
+ <ul class="op-uc-toc--list">
+ <li class="op-uc-toc--list-item">
+ <a class="op-uc-toc--item-link op-uc-link" href="#211---the-second-h3-heading">
+ <span class="op-uc-toc--list-item-number">2.1.1</span>
+ <span class="op-uc-toc--list-item-title">- The second h3 heading</span>
+ </a>
+ </li>
+ </ul>
+ </ul>
+ </ul>
+ </nav>
+ </p>
+ <h1 class="op-uc-h1" id="1-the-first-h1-heading">
+ 1 The first h1 heading
+ <a class="op-uc-link_permalink icon-link op-uc-link" href="#1-the-first-h1-heading" aria-hidden="true"></a>
+ </h1>
+ <p class="op-uc-p">Some text after the first h1 heading</p>
+ <h2 class="op-uc-h2" id="11-the-first-h2-heading">
+ 1.1 The first h2 heading
+ <a class="op-uc-link_permalink icon-link op-uc-link" href="#11-the-first-h2-heading" aria-hidden="true"></a>
+ </h2>
+ <p class="op-uc-p">Some text after the first h2 heading</p>
+ <h3 class="op-uc-h3" id="111-the-first-h3-heading">
+ 1.1.1. The first h3 heading
+ <a class="op-uc-link_permalink icon-link op-uc-link" href="#111-the-first-h3-heading" aria-hidden="true"></a>
+ </h3>
+ <p class="op-uc-p">Some text after the first h3 heading</p>
+ <h1 class="op-uc-h1" id="2-the-second-h1-heading">
+ 2) The second h1 heading
+ <a class="op-uc-link_permalink icon-link op-uc-link" href="#2-the-second-h1-heading" aria-hidden="true"></a>
+ </h1>
+ <p class="op-uc-p">Some text after the second h1 heading</p>
+ <h2 class="op-uc-h2" id="21-the-second-h2-heading">
+ 2.1) The second h2 heading
+ <a class="op-uc-link_permalink icon-link op-uc-link" href="#21-the-second-h2-heading" aria-hidden="true"></a>
+ </h2>
+ <p class="op-uc-p">Some text after the second h2 heading</p>
+ <h3 class="op-uc-h3" id="211---the-second-h3-heading">
+ 2.1.1 - The second h3 heading
+ <a class="op-uc-link_permalink icon-link op-uc-link" href="#211---the-second-h3-heading" aria-hidden="true"></a>
+ </h3>
+ <p class="op-uc-p">Some text after the second h3 heading</p>
+ EXPECTED
+ end
+ end
+ end
end