Skip to content

Commit

Permalink
Fix LinkReferenceDefinition having null SourceSpan when title is present
Browse files Browse the repository at this point in the history
Fixes #292.
  • Loading branch information
robinst committed Mar 12, 2024
1 parent 4e65b06 commit 21fc1df
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ State getState() {
}

private boolean startDefinition(Scanner scanner) {
// Finish any outstanding references now. We don't do this earlier because we need addSourceSpan to have been
// called before we do it.
finishReference();

scanner.whitespace();
if (!scanner.next('[')) {
return false;
Expand Down Expand Up @@ -205,7 +209,6 @@ private boolean startTitle(Scanner scanner) {
title.append('\n');
}
} else {
finishReference();
// There might be another reference instead, try that for the same character.
state = State.START_DEFINITION;
}
Expand Down Expand Up @@ -235,7 +238,6 @@ private boolean title(Scanner scanner) {
return false;
}
referenceValid = true;
finishReference();
paragraphLines.clear();

// See if there's another definition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public interface BlockParser {

BlockContinue tryContinue(ParserState parserState);

/**
* Add the part of a line that belongs to this block parser to parse (i.e. without any container block markers).
* Note that the line will only include a {@link SourceLine#getSourceSpan()} if source spans are enabled for inlines.
*/
void addLine(SourceLine line);

/**
Expand Down
19 changes: 19 additions & 0 deletions commonmark/src/test/java/org/commonmark/test/SourceSpansTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -164,6 +165,24 @@ public void linkReferenceDefinition() {
assertEquals(Arrays.asList(SourceSpan.of(1, 0, 4)), paragraph.getSourceSpans());
}

@Test
public void linkReferenceDefinitionMultiple() {
var doc = PARSER.parse("[foo]: /foo\n[bar]: /bar\n");
var def1 = (LinkReferenceDefinition) doc.getFirstChild();
var def2 = (LinkReferenceDefinition) doc.getLastChild();
assertEquals(List.of(SourceSpan.of(0, 0, 11)), def1.getSourceSpans());
assertEquals(List.of(SourceSpan.of(1, 0, 11)), def2.getSourceSpans());
}

@Test
public void linkReferenceDefinitionWithTitle() {
var doc = PARSER.parse("[1]: #not-code \"Text\"\n[foo]: /foo\n");
var def1 = (LinkReferenceDefinition) doc.getFirstChild();
var def2 = (LinkReferenceDefinition) doc.getLastChild();
assertEquals(List.of(SourceSpan.of(0, 0, 21)), def1.getSourceSpans());
assertEquals(List.of(SourceSpan.of(1, 0, 11)), def2.getSourceSpans());
}

@Test
public void linkReferenceDefinitionHeading() {
// This is probably the trickiest because we have a link reference definition at the start of a paragraph
Expand Down

0 comments on commit 21fc1df

Please sign in to comment.