Skip to content

Commit

Permalink
Save original literal in ThematicBreak on parsing
Browse files Browse the repository at this point in the history
Fixes #295.
  • Loading branch information
robinst committed Mar 12, 2024
1 parent 6385869 commit bd40c08
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
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());
}
}

0 comments on commit bd40c08

Please sign in to comment.