Skip to content

Commit

Permalink
Consider code spans when escaping Markdown styling
Browse files Browse the repository at this point in the history
Previously, Markdown styling, such as _ characters, in an issue title
would always be escaped. This led to unnecessary escaping when the
styling characters were used within a code span (text wrapped in
backticks).

This commit disables the escaping of Markdown styling for text within
a code span. It does not consider the case where the code span uses
multiple backticks as its opening and closing delimiters. Such usage
in an issue title is considered to be unlikely and the added
complexity of coping with such delimiters was not felt to be
justified.

Fixes gh-87
  • Loading branch information
wilkinsona committed Jun 21, 2024
1 parent d76da27 commit ecb6647
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,17 @@ private static Escape htmlTags() {

private static Escape markdownStyling() {
return (input) -> {
boolean withinBackticks = false;
char previous = ' ';
StringBuilder result = new StringBuilder(input.length());
for (char c : input.toCharArray()) {
if (previous != '\\' && c == '*' || c == '_' || c == '~') {
if (!withinBackticks && previous != '\\' && (c == '*' || c == '_' || c == '~')) {
result.append('\\');
}
result.append(c);
if (c == '`') {
withinBackticks = !withinBackticks;
}
previous = c;
}
return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@ void generateWhenMarkdownStylingIsInIssueTitleItIsEscaped() throws IOException {
assertThat(new String(Files.readAllBytes(file))).contains("Bug 5 for \\~strikethrough\\~");
}

@Test
void generateWhenMarkdownStylingWithinBackTicksIsInIssueTitleItIsNotEscaped() throws IOException {
setupGenerator(MilestoneReference.TITLE);
List<Issue> issues = new ArrayList<>();
issues.add(newIssue("Clarify `FactoryBean.OBJECT_TYPE_ATTRIBUTE` supported types", "1", "bug-1-url", Type.BUG));
given(this.service.getMilestoneNumber("v2.3", REPO)).willReturn(23);
given(this.service.getIssuesForMilestone(23, REPO)).willReturn(issues);
Path file = generateChangelog("v2.3");
assertThat(new String(Files.readAllBytes(file)))
.contains("Clarify `FactoryBean.OBJECT_TYPE_ATTRIBUTE` supported types");
}

@Test
void generateWhenEscapedMarkdownStylingIsInIssueTitleItIsNotEscapedAgain() throws IOException {
setupGenerator(MilestoneReference.TITLE);
Expand Down

0 comments on commit ecb6647

Please sign in to comment.