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 #1

Updated by daijiro fukuda 7 months ago

I have confirmed the following with the uploaded patch: wiki-fix-section-extracting-mishandles-ticket-refs.diff.

  • the case described in "How to reproduce" is resolved
  • No failure in the tests

My view on the fix is as explained in "How should we fix this?".
If you think a more drastic fix is needed, please point it out.
Thanks!

Actions #2

Updated by Marius BÄ‚LTEANU 7 months ago

  • Has duplicate Defect #40715: Wiki (markdown syntax): "Edit this section" does not extract ATX headings correctly added
Actions #3

Updated by Go MAEDA 7 months ago

  • Status changed from New to Confirmed
Actions #4

Updated by Mark Asbach 6 months ago

daijiro fukuda wrote in #note-1:

If you think a more drastic fix is needed, please point it out.

I've stumbled upon this issue as we experience a (most probably) related issue, but for setext Headings in MarkDown (https://github.github.com/gfm/#setext-heading). Because I'm not 100% sure this is the same root cause, I've created a new ticket (#40918), but if my assumption is right, a more drastic fix would be good to solve both issues.

Unfortunately, I'm no rails developer and so cannot provide a patch or pull request with sufficient quality. In case you find the time to look at the issue reported by me, I'd be very grateful for that.

Thanks!

Actions #5

Updated by Go MAEDA 6 months ago

Thank you for posting the patch to fix the issue. I wrote a test for the patch.

Setting the target version to 5.1.4.

Actions #6

Updated by Go MAEDA 6 months ago

  • Subject changed from Wiki (markdown syntax): "Edit this section" does not extract ATX headings correctly to "Edit this section" on Wiki pages misinterprets issue links with double hash (##nnn) as ATX headings
  • Status changed from Confirmed to Resolved
  • Assignee set to Go MAEDA
  • Resolution set to Fixed

Committed the patch in r22906.
Thank you for your contribution.

Actions #7

Updated by Go MAEDA 5 months ago

  • Status changed from Resolved to Closed

Merged the changes to 5.1-stable in r22918.

Actions

Also available in: Atom PDF