Defect #40716
closed"Edit this section" on Wiki pages misinterprets issue links with double hash (##nnn) as ATX headings
0%
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.
@text.split(/(^(?:\S+\r?\n\r?(?:\=+|\-+)|#+.+|(?:~~~|```).*)\s*$)/).each do |part|
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
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!
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
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!
Updated by Go MAEDA 6 months ago
- File test-40716.patch test-40716.patch added
- Target version set to 5.1.4
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.
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.
Updated by daijiro fukuda 5 months ago
Thanks for merging!
About Defect #40918: Wiki (markdown syntax): "Edit this section" does not extract SeText headings correctly (#note-4),
I'll take a look if I have time.