-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass original label to InlineParserContext, let it normalize it for l…
…ookup This is useful for implementing a custom context that wants access to the original label instead of lowercased and whitespace-collapsed. Fixes #204.
- Loading branch information
Showing
8 changed files
with
105 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
commonmark/src/main/java/org/commonmark/internal/LinkReferenceDefinitions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.commonmark.internal; | ||
|
||
import org.commonmark.internal.util.Escaping; | ||
import org.commonmark.node.LinkReferenceDefinition; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public class LinkReferenceDefinitions { | ||
|
||
// LinkedHashMap for determinism and to preserve document order | ||
private final Map<String, LinkReferenceDefinition> definitions = new LinkedHashMap<>(); | ||
|
||
public void add(LinkReferenceDefinition definition) { | ||
String normalizedLabel = Escaping.normalizeLabelContent(definition.getLabel()); | ||
|
||
// spec: When there are multiple matching link reference definitions, the first is used | ||
if (!definitions.containsKey(normalizedLabel)) { | ||
definitions.put(normalizedLabel, definition); | ||
} | ||
} | ||
|
||
public LinkReferenceDefinition get(String label) { | ||
String normalizedLabel = Escaping.normalizeLabelContent(label); | ||
return definitions.get(normalizedLabel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
commonmark/src/test/java/org/commonmark/test/InlineParserContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.commonmark.test; | ||
|
||
import org.commonmark.internal.InlineParserImpl; | ||
import org.commonmark.node.LinkReferenceDefinition; | ||
import org.commonmark.parser.InlineParser; | ||
import org.commonmark.parser.InlineParserContext; | ||
import org.commonmark.parser.InlineParserFactory; | ||
import org.commonmark.parser.Parser; | ||
import org.commonmark.parser.delimiter.DelimiterProcessor; | ||
import org.commonmark.renderer.html.HtmlRenderer; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class InlineParserContextTest { | ||
|
||
@Test | ||
public void labelShouldBeOriginalNotNormalized() { | ||
CapturingInlineParserFactory inlineParserFactory = new CapturingInlineParserFactory(); | ||
|
||
Parser parser = Parser.builder().inlineParserFactory(inlineParserFactory).build(); | ||
String input = "[link with special label][FooBarBaz]\n\n[foobarbaz]: /url"; | ||
|
||
String rendered = HtmlRenderer.builder().build().render(parser.parse(input)); | ||
|
||
// Lookup should pass original label to context | ||
assertEquals(Collections.singletonList("FooBarBaz"), inlineParserFactory.lookups); | ||
|
||
// Context should normalize label for finding reference | ||
assertEquals("<p><a href=\"/url\">link with special label</a></p>\n", rendered); | ||
} | ||
|
||
static class CapturingInlineParserFactory implements InlineParserFactory { | ||
|
||
private List<String> lookups = new ArrayList<>(); | ||
|
||
@Override | ||
public InlineParser create(final InlineParserContext inlineParserContext) { | ||
InlineParserContext wrappedContext = new InlineParserContext() { | ||
@Override | ||
public List<DelimiterProcessor> getCustomDelimiterProcessors() { | ||
return inlineParserContext.getCustomDelimiterProcessors(); | ||
} | ||
|
||
@Override | ||
public LinkReferenceDefinition getLinkReferenceDefinition(String label) { | ||
lookups.add(label); | ||
return inlineParserContext.getLinkReferenceDefinition(label); | ||
} | ||
}; | ||
|
||
return new InlineParserImpl(wrappedContext); | ||
} | ||
} | ||
} |