Skip to content

Commit

Permalink
Add LinkReferenceDefinition nodes into document
Browse files Browse the repository at this point in the history
This is part of #98 and was enabled by the refactoring of definition
parsing for spec 0.29.
  • Loading branch information
robinst committed Jul 12, 2019
1 parent 813dae3 commit cb43ae9
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ with the exception that 0.x versions can break between minor versions.

## Unreleased
### Added
- `LinkReferenceDefinition` nodes are now part of the document (not
rendered by default).
- `InlineParserContext.getLinkReferenceDefinition` was added to allow
custom inline parsers to look up definitions for reference links.
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,18 @@ private void finalize(BlockParser blockParser) {
deactivateBlockParser();
}

blockParser.closeBlock();

if (blockParser instanceof ParagraphParser) {
ParagraphParser paragraphParser = (ParagraphParser) blockParser;
// TODO: Insert resulting nodes into AST (before paragraph node)
addDefinitionsFrom(paragraphParser);
addDefinitionsFrom((ParagraphParser) blockParser);
}

blockParser.closeBlock();
}

private void addDefinitionsFrom(ParagraphParser paragraphParser) {
for (LinkReferenceDefinition definition : paragraphParser.getDefinitions()) {
// Add nodes into document before paragraph.
paragraphParser.getBlock().insertBefore(definition);

String label = definition.getLabel();
// spec: When there are multiple matching link reference definitions, the first is used
if (!definitions.containsKey(label)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.commonmark.node;

// TODO: We're currently not adding these to the document.
// But that would be very useful for being able to render Nodes back to Markdown, see #98.

/**
* A link reference definition, e.g.:
* <pre><code>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package org.commonmark.test;

import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class LinkReferenceDefinitionNodeTest {

@Test
public void testDefinitionWithoutParagraph() {
Node document = parse("This is a paragraph with a [foo] link.\n\n[foo]: /url 'title'");
List<Node> nodes = Nodes.getChildren(document);

assertThat(nodes.size(), is(2));
assertThat(nodes.get(0), instanceOf(Paragraph.class));
LinkReferenceDefinition definition = assertDef(nodes.get(1), "foo");

assertThat(definition.getDestination(), is("/url"));
assertThat(definition.getTitle(), is("title"));
}

@Test
public void testDefinitionWithParagraph() {
Node document = parse("[foo]: /url\nThis is a paragraph with a [foo] link.");
List<Node> nodes = Nodes.getChildren(document);

assertThat(nodes.size(), is(2));
// Note that definition is not part of the paragraph, it's a sibling
assertThat(nodes.get(0), instanceOf(LinkReferenceDefinition.class));
assertThat(nodes.get(1), instanceOf(Paragraph.class));
}

@Test
public void testMultipleDefinitions() {
Node document = parse("This is a paragraph with a [foo] link.\n\n[foo]: /url\n[bar]: /url");
List<Node> nodes = Nodes.getChildren(document);

assertThat(nodes.size(), is(3));
assertThat(nodes.get(0), instanceOf(Paragraph.class));
assertDef(nodes.get(1), "foo");
assertDef(nodes.get(2), "bar");
}

@Test
public void testMultipleDefinitionsWithSameLabel() {
Node document = parse("This is a paragraph with a [foo] link.\n\n[foo]: /url1\n[foo]: /url2");
List<Node> nodes = Nodes.getChildren(document);

assertThat(nodes.size(), is(3));
assertThat(nodes.get(0), instanceOf(Paragraph.class));
LinkReferenceDefinition def1 = assertDef(nodes.get(1), "foo");
assertThat(def1.getDestination(), is("/url1"));
// When there's multiple definitions with the same label, the first one "wins", as in reference links will use
// that. But we still want to preserve the original definitions in the document.
LinkReferenceDefinition def2 = assertDef(nodes.get(2), "foo");
assertThat(def2.getDestination(), is("/url2"));
}

@Test
public void testDefinitionOfReplacedBlock() {
Node document = parse("[foo]: /url\nHeading\n=======");
List<Node> nodes = Nodes.getChildren(document);

assertThat(nodes.size(), is(2));
assertDef(nodes.get(0), "foo");
assertThat(nodes.get(1), instanceOf(Heading.class));
}

@Test
public void testDefinitionInListItem() {
Node document = parse("* [foo]: /url\n [foo]\n");
assertThat(document.getFirstChild(), instanceOf(BulletList.class));
Node item = document.getFirstChild().getFirstChild();
assertThat(item, instanceOf(ListItem.class));

List<Node> nodes = Nodes.getChildren(item);
assertThat(nodes.size(), is(2));
assertDef(nodes.get(0), "foo");
assertThat(nodes.get(1), instanceOf(Paragraph.class));
}

@Test
public void testDefinitionInListItem2() {
Node document = parse("* [foo]: /url\n* [foo]\n");
assertThat(document.getFirstChild(), instanceOf(BulletList.class));

List<Node> items = Nodes.getChildren(document.getFirstChild());
assertThat(items.size(), is(2));
Node item1 = items.get(0);
Node item2 = items.get(1);

assertThat(item1, instanceOf(ListItem.class));
assertThat(item2, instanceOf(ListItem.class));

assertThat(Nodes.getChildren(item1).size(), is(1));
assertDef(item1.getFirstChild(), "foo");

assertThat(Nodes.getChildren(item2).size(), is(1));
assertThat(item2.getFirstChild(), instanceOf(Paragraph.class));
}

private static Node parse(String input) {
Parser parser = Parser.builder().build();
return parser.parse(input);
}

private static LinkReferenceDefinition assertDef(Node node, String label) {
assertThat(node, instanceOf(LinkReferenceDefinition.class));
LinkReferenceDefinition def = (LinkReferenceDefinition) node;
assertThat(def.getLabel(), is(label));
return def;
}
}
17 changes: 17 additions & 0 deletions commonmark/src/test/java/org/commonmark/test/Nodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.commonmark.test;

import org.commonmark.node.Node;

import java.util.ArrayList;
import java.util.List;

public class Nodes {

public static List<Node> getChildren(Node parent) {
List<Node> children = new ArrayList<>();
for (Node child = parent.getFirstChild(); child != null; child = child.getNext()) {
children.add(child);
}
return children;
}
}

0 comments on commit cb43ae9

Please sign in to comment.