3636import java .util .regex .Pattern ;
3737import org .commonmark .ext .gfm .tables .TableBlock ;
3838import org .commonmark .ext .gfm .tables .TablesExtension ;
39+ import org .commonmark .node .BlockQuote ;
3940import org .commonmark .node .BulletList ;
4041import org .commonmark .node .Code ;
4142import org .commonmark .node .FencedCodeBlock ;
4243import org .commonmark .node .Heading ;
44+ import org .commonmark .node .IndentedCodeBlock ;
45+ import org .commonmark .node .LinkReferenceDefinition ;
4346import org .commonmark .node .ListItem ;
4447import org .commonmark .node .Node ;
4548import org .commonmark .node .OrderedList ;
4649import org .commonmark .node .Paragraph ;
50+ import org .commonmark .node .ThematicBreak ;
4751import org .commonmark .parser .IncludeSourceSpans ;
4852import org .commonmark .parser .Parser ;
4953
@@ -90,7 +94,7 @@ private static class TokenVisitor {
9094 void visit (Node node ) {
9195 boolean alreadyVisitedChildren = false ;
9296 switch (node ) {
93- case Heading heading -> addSpan (heading , HEADER_OPEN_TOKEN , HEADER_CLOSE_TOKEN );
97+ case Heading heading -> visitHeading (heading );
9498 case Paragraph paragraph -> addSpan (paragraph , PARAGRAPH_OPEN_TOKEN , PARAGRAPH_CLOSE_TOKEN );
9599 case BulletList bulletList -> addSpan (bulletList , LIST_OPEN_TOKEN , LIST_CLOSE_TOKEN );
96100 case OrderedList orderedList -> addSpan (orderedList , LIST_OPEN_TOKEN , LIST_CLOSE_TOKEN );
@@ -101,6 +105,14 @@ void visit(Node node) {
101105 alreadyVisitedChildren = true ;
102106 }
103107 case Code code -> visitCodeSpan (code );
108+ case BlockQuote blockQuote ->
109+ throw new UnsupportedOperationException ("Block quotes not supported" );
110+ case IndentedCodeBlock indentedCodeBlock ->
111+ throw new UnsupportedOperationException ("Indented code blocks not supported" );
112+ case ThematicBreak thematicBreak ->
113+ throw new UnsupportedOperationException ("Thematic breaks not supported" );
114+ case LinkReferenceDefinition linkReferenceDefinition ->
115+ throw new UnsupportedOperationException ("Link reference definitions not supported" );
104116 // TODO: others
105117 default -> {}
106118 }
@@ -130,6 +142,19 @@ private boolean visitListItem(ListItem listItem) {
130142 };
131143 }
132144
145+ private void visitHeading (Heading heading ) {
146+ // There are two ways to spell a heading in Markdown:
147+ // 1. With hashes: `# Heading` `## Subheading`
148+ // 2. With dashes or underlines: `Heading\n=======` `Subheading\n-------`
149+ // We currently only support the first kind. We can distinguish the two cases by checking if
150+ // the heading text contains a newline.
151+ String s = nodeString (heading );
152+ if (s .contains ("\n " )) {
153+ throw new UnsupportedOperationException ("Unsupported heading: " + s );
154+ }
155+ addSpan (heading , HEADER_OPEN_TOKEN , HEADER_CLOSE_TOKEN );
156+ }
157+
133158 private void visitFencedCodeBlock (FencedCodeBlock fencedCodeBlock ) {
134159 // Any indentation before the code block is part of FencedCodeBlock. This makes sense
135160 // because the lines inside the code block must also be indented by that amount. That
@@ -205,6 +230,10 @@ private int endPosition(Node node) {
205230 var last = node .getSourceSpans ().getLast ();
206231 return last .getInputIndex () + last .getLength ();
207232 }
233+
234+ private String nodeString (Node node ) {
235+ return input .substring (startPosition (node ), endPosition (node ));
236+ }
208237 }
209238
210239 @ Override
0 commit comments