Skip to content
Merged
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 @@ -8,6 +8,10 @@ public class ThematicBreakParser extends AbstractBlockParser {

private final ThematicBreak block = new ThematicBreak();

public ThematicBreakParser(String literal) {
block.setLiteral(literal);
}

@Override
public Block getBlock() {
return block;
Expand All @@ -29,7 +33,8 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
int nextNonSpace = state.getNextNonSpaceIndex();
CharSequence line = state.getLine().getContent();
if (isThematicBreak(line, nextNonSpace)) {
return BlockStart.of(new ThematicBreakParser()).atIndex(line.length());
var literal = String.valueOf(line.subSequence(state.getIndex(), line.length()));
return BlockStart.of(new ThematicBreakParser(literal)).atIndex(line.length());
} else {
return BlockStart.none();
}
Expand Down
13 changes: 13 additions & 0 deletions commonmark/src/main/java/org/commonmark/node/ThematicBreak.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,21 @@

public class ThematicBreak extends Block {

private String literal;

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}

/**
* @return the source literal that represents this node, if available
*/
public String getLiteral() {
return literal;
}

public void setLiteral(String literal) {
this.literal = literal;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.commonmark.test;

import org.commonmark.node.ThematicBreak;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.testutil.RenderingTestCase;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class ThematicBreakParserTest {

private static final Parser PARSER = Parser.builder().build();

@Test
public void testLiteral() {
assertLiteral("***", "***");
assertLiteral("-- -", "-- -");
assertLiteral(" __ __ __ ", " __ __ __ ");
assertLiteral("***", "> ***");
}

private static void assertLiteral(String expected, String input) {
var tb = Nodes.find(PARSER.parse(input), ThematicBreak.class);
assertNotNull(tb);
assertEquals(expected, tb.getLiteral());
}
}