Skip to content

Commit

Permalink
doc: markdown allow empty lines in list
Browse files Browse the repository at this point in the history
  • Loading branch information
imkiva committed Dec 11, 2022
1 parent a83f0e1 commit e15eeff
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public class DocMdPrinter extends DocHtmlPrinter<DocMdPrinter.Config> {
}

@Override protected void renderHardLineBreak(@NotNull Cursor cursor, EnumSet<Outer> outer) {
if (outer.contains(Outer.List)) cursor.lineBreakWith("<br/>\n");
else cursor.lineBreakWith("\n");
cursor.lineBreakWith("\n");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ private void checkLineStart() {
}
}

/** New line if needed */
public void lineBreakIfNeeded(@NotNull CharSequence lineBreak) {
if (!isAtLineStart())
lineBreakWith(lineBreak);
/** Do something when I am not at line start. */
public void whenLineUsed(@NotNull Runnable runnable) {
if (!isAtLineStart()) runnable.run();
}

public void lineBreakWith(@NotNull CharSequence lineBreak) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,13 @@ protected void renderList(@NotNull Cursor cursor, @NotNull Doc.List list, EnumSe
}

protected static void renderList(@NotNull StringPrinter<?> printer, @NotNull Cursor cursor, @NotNull Doc.List list, EnumSet<Outer> outer) {
// TODO[kiva]: improve this!
var isTopLevel = !outer.contains(Outer.List);
cursor.lineBreakIfNeeded("\n");
cursor.whenLineUsed(() -> printer.renderHardLineBreak(cursor, outer));
if (isTopLevel) printer.renderHardLineBreak(cursor, outer);

list.items().forEachIndexed((idx, item) -> {
cursor.lineBreakIfNeeded("\n");
cursor.whenLineUsed(() -> printer.renderHardLineBreak(cursor, outer));

var pre = list.isOrdered()
? (idx + 1) + "."
Expand All @@ -206,7 +207,7 @@ protected static void renderList(@NotNull StringPrinter<?> printer, @NotNull Cur
});

if (isTopLevel) {
cursor.lineBreakIfNeeded("\n");
cursor.whenLineUsed(() -> printer.renderHardLineBreak(cursor, outer));
printer.renderHardLineBreak(cursor, outer);
}
}
Expand Down
22 changes: 8 additions & 14 deletions pretty/src/test/java/org/aya/pretty/MdStyleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,26 @@ public void testEscapeDocs() {

@Test
public void testList() {
var actual0 = bulletList().renderToMd();
var expected0 = """
assertEquals("""
+ first<br/>
<br/>
+ first
third
+ fourth
+ 4.1
+ 4.2
""";
""".stripIndent(), bulletList().renderToMd().stripIndent());
assertEquals("""
1. first
var actual1 = orderedList().renderToMd();
var expected1 = """
1. first<br/>
<br/>
third
2. fourth
+ 4.1
+ 4.2
""";
assertEquals(expected0, actual0);
assertEquals(expected1, actual1);
""".stripIndent(), orderedList().renderToMd().stripIndent());
}

@NotNull private Doc bulletList() {
Expand Down

0 comments on commit e15eeff

Please sign in to comment.