Project

General

Profile

Actions

Defect #40716

closed

"Edit this section" on Wiki pages misinterprets issue links with double hash (##nnn) as ATX headings

Added by daijiro fukuda 7 months ago. Updated 5 months ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Wiki
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:
Resolution:
Fixed
Affected version:

Description

Abstraction

"Edit this section" in Wiki with the Markdown syntax does not extract ATX headings correctly.

ATX headings:

# Headline

## Headline

The current trunk(rev.22841) and v5.0.2 extracts it by the regexp '#+.+', but this would not be sufficient.

Especially, since it does not consider the following condition, it mishandles ticket references such as "#11111" or "##11111".

The opening sequence of # characters must be followed by a space or by the end of line.

I explain the details in "How to reproduce".

How to reproduce

Prepare Wiki with the Markdown syntax as follows.

# Wiki

## Section A

A

##1 : this is a ticket reference.

AA

## Section B

B

We can click each section's "Edit this section" button (surrounded by red rectangles in the image above).
However, we can't edit each section correctly since the ticket reference "##1" is wrongly detected as a level 2 section.

When clicking the button of "Section A":
(URI: "Wiki/edit?section=2")

When clicking the button of "Section B":
(URI: "Wiki/edit?section=3")

The expected result when clicking the button of "Section A":

## Section A

A

##1 : this is a ticket reference.

AA

The expected result when clicking the button of "Section B":

## Section B

B

Environment

$ RAILS_ENV=development bin/about

Environment:
  Redmine version                5.1.2.devel
  Ruby version                   3.2.2-p53 (2023-03-30) [x86_64-linux]
  Rails version                  7.1.2
  Environment                    development
  Database adapter               SQLite
  Mailer queue                   ActiveJob::QueueAdapters::AsyncAdapter
  Mailer delivery                smtp
Redmine settings:
  Redmine theme                  Default
SCM:
  Subversion                     1.13.0
  Git                            2.25.1
  Filesystem                     
Redmine plugins:
  no plugin installed

How should we fix this?

It would be necessary to improve the regular expression based on GitHub Flavored Markdown
(because the commonmarker is wrapping comrak, and comrak is based on GitHub Flavored Markdown.)

The current (rev.22841) regexp for ATX headings is '#+.+' as follows.

https://github.com/redmine/redmine/blob/c0af36b12da1d053d87735c174e7c79e97254e75/lib/redmine/wiki_formatting/section_helper.rb#L45

@text.split(/(^(?:\S+\r?\n\r?(?:\=+|\-+)|#+.+|(?:~~~|```).*)\s*$)/).each do |part|

https://github.com/redmine/redmine/blob/c0af36b12da1d053d87735c174e7c79e97254e75/lib/redmine/wiki_formatting/section_helper.rb#L55-L56

          elsif part =~ /\A(#+).+/
            level = $1.size

For example, we can make these conditions consider spaces following to the opening '#'.
(I have uploaded this patch file.)

Index: lib/redmine/wiki_formatting/section_helper.rb
===================================================================
--- lib/redmine/wiki_formatting/section_helper.rb (revision 22841)
+++ lib/redmine/wiki_formatting/section_helper.rb (working copy)
@@ -42,7 +42,7 @@
         i = 0
         l = 1
         inside_pre = false
-        @text.split(/(^(?:\S+\r?\n\r?(?:\=+|\-+)|#+.+|(?:~~~|```).*)\s*$)/).each do |part|
+        @text.split(/(^(?:\S+\r?\n\r?(?:\=+|\-+)|#+ +.+|(?:~~~|```).*)\s*$)/).each do |part|
           level = nil
           if part =~ /\A(~{3,}|`{3,})(\s*\S+)?\s*$/
             if !inside_pre
@@ -52,7 +52,7 @@
             end
           elsif inside_pre
             # nop
-          elsif part =~ /\A(#+).+/
+          elsif part =~ /\A(#+) +.+/
             level = $1.size
           elsif part =~ /\A.+\r?\n\r?(\=+|\-+)\s*$/
             level = $1.include?('=') ? 1 : 2

This solves the case I explained in "How to reproduce", but this is not enough to handle the following headlines.

  # Headline may have some spaces in the line head.

#(\t) Tab can be replaced to spaces

I often use ticket references, so I'm happy to solve just the case I explained in "How to reproduce" first.


Files


Related issues

Has duplicate Redmine - Defect #40715: Wiki (markdown syntax): "Edit this section" does not extract ATX headings correctlyClosed

Actions
Actions

Also available in: Atom PDF