From f33e7bd06fdeeb8939dd49e76f69015db776846c Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Fri, 24 Mar 2023 15:58:43 +0100 Subject: [PATCH] SONAR-18895 fix markdown maximum heading level --- .../org/sonar/markdown/HtmlHeadingChannel.java | 16 +++++++++------- .../java/org/sonar/markdown/MarkdownTest.java | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlHeadingChannel.java b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlHeadingChannel.java index 9e746bf66e6..8e9a5ea6c99 100644 --- a/sonar-markdown/src/main/java/org/sonar/markdown/HtmlHeadingChannel.java +++ b/sonar-markdown/src/main/java/org/sonar/markdown/HtmlHeadingChannel.java @@ -58,15 +58,17 @@ public class HtmlHeadingChannel extends RegexChannel { protected void consume(CharSequence token, MarkdownOutput output) { int index = 0; int headingLevel = 0; - while(index < token.length() && Character.isWhitespace(token.charAt(index))) { - index ++; + while (index < token.length() && Character.isWhitespace(token.charAt(index))) { + index++; } - while(index < token.length() && index <= MAX_HEADING_DEPTH && token.charAt(index) == '=') { - index ++; - headingLevel ++; + while (index < token.length() && token.charAt(index) == '=') { + if (index < MAX_HEADING_DEPTH) { + headingLevel++; + } + index++; } - while(index < token.length() && Character.isWhitespace(token.charAt(index))) { - index ++; + while (index < token.length() && Character.isWhitespace(token.charAt(index))) { + index++; } CharSequence headingText = token.subSequence(index, token.length()); diff --git a/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java b/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java index 705888c838c..1476b589ac4 100644 --- a/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java +++ b/sonar-markdown/src/test/java/org/sonar/markdown/MarkdownTest.java @@ -67,8 +67,8 @@ public class MarkdownTest { @Test public void shouldDecorateHeadings() { - assertThat(Markdown.convertToHtml(" = Top\r== Sub\r\n=== Sub sub\n ==== \n 1.five")) - .isEqualTo("

Top

Sub

Sub sub

1.five"); + assertThat(Markdown.convertToHtml(" = Top\r== Sub\r\n=== Sub sub\n ==== \n ===== five\n============ max")) + .isEqualTo("

Top

Sub

Sub sub

five
max
"); } @Test -- 2.39.5