Skip to content

Commit e3795b2

Browse files
committed
footnotes: Add Markdown rendering for inline footnotes
1 parent c910105 commit e3795b2

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

commonmark-ext-footnotes/src/main/java/org/commonmark/ext/footnotes/internal/FootnoteMarkdownNodeRenderer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.commonmark.ext.footnotes.FootnoteDefinition;
44
import org.commonmark.ext.footnotes.FootnoteReference;
5+
import org.commonmark.ext.footnotes.InlineFootnote;
56
import org.commonmark.node.*;
67
import org.commonmark.renderer.NodeRenderer;
78
import org.commonmark.renderer.html.HtmlNodeRendererContext;
@@ -26,13 +27,15 @@ public FootnoteMarkdownNodeRenderer(MarkdownNodeRendererContext context) {
2627

2728
@Override
2829
public Set<Class<? extends Node>> getNodeTypes() {
29-
return Set.of(FootnoteReference.class, FootnoteDefinition.class);
30+
return Set.of(FootnoteReference.class, InlineFootnote.class, FootnoteDefinition.class);
3031
}
3132

3233
@Override
3334
public void render(Node node) {
3435
if (node instanceof FootnoteReference) {
3536
renderReference((FootnoteReference) node);
37+
} else if (node instanceof InlineFootnote) {
38+
renderInline((InlineFootnote) node);
3639
} else if (node instanceof FootnoteDefinition) {
3740
renderDefinition((FootnoteDefinition) node);
3841
}
@@ -45,6 +48,12 @@ private void renderReference(FootnoteReference ref) {
4548
writer.raw("]");
4649
}
4750

51+
private void renderInline(InlineFootnote inlineFootnote) {
52+
writer.raw("^[");
53+
renderChildren(inlineFootnote);
54+
writer.raw("]");
55+
}
56+
4857
private void renderDefinition(FootnoteDefinition def) {
4958
writer.raw("[^");
5059
writer.raw(def.getLabel());

commonmark-ext-footnotes/src/test/java/org/commonmark/ext/footnotes/FootnoteMarkdownRendererTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import static org.junit.Assert.assertEquals;
1212

1313
public class FootnoteMarkdownRendererTest {
14-
private static final Set<Extension> EXTENSIONS = Set.of(FootnotesExtension.create());
14+
private static final Set<Extension> EXTENSIONS = Set.of(FootnotesExtension.builder().inlineFootnotes(true).build());
1515
private static final Parser PARSER = Parser.builder().extensions(EXTENSIONS).build();
1616
private static final MarkdownRenderer RENDERER = MarkdownRenderer.builder().extensions(EXTENSIONS).build();
1717

@@ -36,6 +36,11 @@ public void testBackslashInLabel() {
3636
assertRoundTrip("[^\\foo]\n\n[^\\foo]: note\n");
3737
}
3838

39+
@Test
40+
public void testInline() {
41+
assertRoundTrip("^[test *foo*]\n");
42+
}
43+
3944
private void assertRoundTrip(String input) {
4045
String rendered = parseAndRender(input);
4146
assertEquals(input, rendered);

0 commit comments

Comments
 (0)