Skip to content

Commit

Permalink
Escape special chars in markdown
Browse files Browse the repository at this point in the history
if the text happens to contain an @ github tries to map it to a
username, and if it contains a # it tries to link it to an existing
issue/pr.

This now escapes these two special chars to prevent accidental
confusion, especially as there are users on github named @deprecated and
@OverRide
  • Loading branch information
laeubi committed Jan 26, 2025
1 parent cff4428 commit c6d7a98
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ public MarkdownBuilder(Path output) {
}

public MarkdownBuilder add(String string) {
lines.add(string);
lines.add(escape(string));
return this;
}

public MarkdownBuilder addListItem(String item) {
lines.add("- " + item);
lines.add("- " + escape(item));
return this;
}

public static String escape(String item) {
return item.replace("@", "<span>@</span>").replace("#", "<span>#</span>");
}

public void write() throws MojoFailureException {
if (output == null) {
return;
Expand All @@ -62,17 +66,17 @@ public void newLine() {
}

public void h1(String string) {
lines.add("# " + string);
lines.add("# " + escape(string));
lines.add("");
}

public void h2(String string) {
lines.add("## " + string);
lines.add("## " + escape(string));
lines.add("");
}

public void h3(String string) {
lines.add("### " + string);
lines.add("### " + escape(string));
lines.add("");
}

Expand Down

0 comments on commit c6d7a98

Please sign in to comment.