Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen
private final AsciiMatcher textEscape;
private final CharMatcher textEscapeInHeading;
private final CharMatcher linkDestinationNeedsAngleBrackets =
AsciiMatcher.builder().c(' ').c('(').c(')').c('<').c('>').c('\n').c('\\').build();
AsciiMatcher.builder()
.c(' ')
.c('(')
.c(')')
.c('<')
.c('>')
.c('\n')
.c('\r')
.c('\\')
.build();
private final CharMatcher linkDestinationEscape =
AsciiMatcher.builder().c('<').c('>').c('\n').c('\\').c('&').build();
AsciiMatcher.builder().c('<').c('>').c('\n').c('\r').c('\\').c('&').build();
private final CharMatcher linkTitleEscapeInQuotes =
AsciiMatcher.builder().c('"').c('\n').c('\\').c('&').build();
AsciiMatcher.builder().c('"').c('\n').c('\r').c('\\').c('&').build();

private final Pattern orderedListMarkerPattern = Pattern.compile("^([0-9]{1,9})([.)])");

Expand All @@ -49,7 +58,7 @@ public CoreMarkdownNodeRenderer(MarkdownNodeRendererContext context) {

textEscape =
AsciiMatcher.builder()
.anyOf("[]<>`*_&\n\\")
.anyOf("[]<>`*_&\n\r\\")
.anyOf(context.getSpecialCharacters())
.build();
textEscapeInHeading = AsciiMatcher.builder(textEscape).anyOf("#").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,10 @@ private void append(char c, CharMatcher escape) throws IOException {

private void appendEscaped(char c) throws IOException {
if (c == '\n') {
// Can't escape this with \, use numeric character reference
// Can't escape these with \, use numeric character reference
buffer.append("&#10;");
} else if (c == '\r') {
buffer.append("&#13;");
} else {
buffer.append('\\');
buffer.append(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public void testEscaping() {
assertRoundTrip("&#9;foo\n");
assertRoundTrip("&#32; foo\n");
assertRoundTrip("foo&#10;&#10;bar\n");
assertRoundTrip("foo&#13;&#13;bar\n");
}

@Test
Expand Down Expand Up @@ -285,6 +286,8 @@ public void testLinks() {
assertRoundTrip("[link](/uri \"tes\\\\\")\n");
assertRoundTrip("[link](/url \"test&#10;&#10;\")\n");
assertRoundTrip("[link](</url&#10;&#10;>)\n");
assertRoundTrip("[link](/url \"test&#13;&#13;\")\n");
assertRoundTrip("[link](</url&#13;&#13;>)\n");
}

@Test
Expand All @@ -297,6 +300,8 @@ public void testImages() {
assertRoundTrip("![a](<b\\>c>)\n");
assertRoundTrip("![a](<b\\\\\\>c>)\n");
assertRoundTrip("![a](/uri \"foo \\\" bar\")\n");
assertRoundTrip("![link](/url \"test&#13;&#13;\")\n");
assertRoundTrip("![link](</url&#13;&#13;>)\n");
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class MarkdownWriterTest {

private static final CharMatcher STAR = c -> c == '*';
private static final CharMatcher NEWLINE = c -> c == '\n';
private static final CharMatcher CARRIAGE_RETURN = c -> c == '\r';
private static final CharMatcher PIPE = c -> c == '|';

@Test
Expand All @@ -28,6 +29,12 @@ public void testTextEscapingNewline() {
assertThat(text("\n", NEWLINE)).isEqualTo("&#10;");
}

@Test
public void testTextEscapingCarriageReturn() {
assertThat(text("a\rb\rc", CARRIAGE_RETURN)).isEqualTo("a&#13;b&#13;c");
assertThat(text("\r", CARRIAGE_RETURN)).isEqualTo("&#13;");
}

@Test
public void testRawEscaping() {
StringBuilder sb = new StringBuilder();
Expand Down