Skip to content

Commit 321a788

Browse files
committed
Allow multi-line content in setext headings (api break, spec 0.24, #28)
This changes the MatchedBlockParser API to return the full paragraph content instead of just the first line. Extensions might be affected.
1 parent e2ceaa6 commit 321a788

5 files changed

Lines changed: 19 additions & 14 deletions

File tree

commonmark-ext-gfm-tables/src/main/java/org/commonmark/ext/gfm/tables/internal/TableBlockParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ public static class Factory extends AbstractBlockParserFactory {
157157
@Override
158158
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
159159
CharSequence line = state.getLine();
160-
CharSequence paragraphStartLine = matchedBlockParser.getParagraphStartLine();
161-
if (paragraphStartLine != null && paragraphStartLine.toString().contains("|")) {
160+
CharSequence paragraph = matchedBlockParser.getParagraphContent();
161+
if (paragraph != null && paragraph.toString().contains("|") && !paragraph.toString().contains("\n")) {
162162
CharSequence separatorLine = line.subSequence(state.getIndex(), line.length());
163163
if (TABLE_HEADER_SEPARATOR.matcher(separatorLine).matches()) {
164-
List<String> headParts = split(paragraphStartLine);
164+
List<String> headParts = split(paragraph);
165165
List<String> separatorParts = split(separatorLine);
166166
if (separatorParts.size() >= headParts.size()) {
167-
return BlockStart.of(new TableBlockParser(paragraphStartLine))
167+
return BlockStart.of(new TableBlockParser(paragraph))
168168
.atIndex(state.getIndex())
169169
.replaceActiveBlockParser();
170170
}

commonmark-ext-gfm-tables/src/test/java/org/commonmark/ext/gfm/tables/TablesTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public void separatorCanNotHaveLeadingSpaceThenPipe() {
3232
assertRendering("Abc|Def\n |---|---", "<p>Abc|Def\n|---|---</p>\n");
3333
}
3434

35+
@Test
36+
public void headerMustBeOneLine() {
37+
assertRendering("No\nAbc|Def\n---|---", "<p>No\nAbc|Def\n---|---</p>\n");
38+
}
39+
3540
@Test
3641
public void oneHeadNoBody() {
3742
assertRendering("Abc|Def\n---|---", "<table>\n" +

commonmark/src/main/java/org/commonmark/internal/DocumentParser.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,10 @@ public BlockParser getMatchedBlockParser() {
511511
}
512512

513513
@Override
514-
public CharSequence getParagraphStartLine() {
514+
public CharSequence getParagraphContent() {
515515
if (matchedBlockParser instanceof ParagraphParser) {
516516
ParagraphParser paragraphParser = (ParagraphParser) matchedBlockParser;
517-
if (paragraphParser.hasSingleLine()) {
518-
return paragraphParser.getContentString();
519-
}
517+
return paragraphParser.getContentString();
520518
}
521519
return null;
522520
}

commonmark/src/main/java/org/commonmark/internal/HeadingParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
4747
}
4848
CharSequence line = state.getLine();
4949
int nextNonSpace = state.getNextNonSpaceIndex();
50-
CharSequence paragraphStartLine = matchedBlockParser.getParagraphStartLine();
50+
CharSequence paragraph = matchedBlockParser.getParagraphContent();
5151
Matcher matcher;
5252
if ((matcher = ATX_HEADING.matcher(line.subSequence(nextNonSpace, line.length()))).find()) {
5353
// ATX heading
@@ -58,12 +58,12 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
5858
return BlockStart.of(new HeadingParser(level, content))
5959
.atIndex(line.length());
6060

61-
} else if (paragraphStartLine != null &&
61+
} else if (paragraph != null &&
6262
((matcher = SETEXT_HEADING.matcher(line.subSequence(nextNonSpace, line.length()))).find())) {
6363
// setext heading line
6464

6565
int level = matcher.group(0).charAt(0) == '=' ? 1 : 2;
66-
String content = paragraphStartLine.toString();
66+
String content = paragraph.toString();
6767
return BlockStart.of(new HeadingParser(level, content))
6868
.atIndex(line.length())
6969
.replaceActiveBlockParser();

commonmark/src/main/java/org/commonmark/parser/block/MatchedBlockParser.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ public interface MatchedBlockParser {
1010
BlockParser getMatchedBlockParser();
1111

1212
/**
13-
* @return the first line of the paragraph if the matched block is a paragraph and we're on the second line,
14-
* null otherwise
13+
* Returns the current content of the paragraph if the matched block is a paragraph. The content can be multiple
14+
* lines separated by {@code '\n'}.
15+
*
16+
* @return paragraph content or {@code null}
1517
*/
16-
CharSequence getParagraphStartLine();
18+
CharSequence getParagraphContent();
1719

1820
}

0 commit comments

Comments
 (0)