Skip to content

Commit

Permalink
footnotes: Add Markdown rendering for inline footnotes
Browse files Browse the repository at this point in the history
  • Loading branch information
robinst committed Sep 12, 2024
1 parent c910105 commit e3795b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.commonmark.ext.footnotes.FootnoteDefinition;
import org.commonmark.ext.footnotes.FootnoteReference;
import org.commonmark.ext.footnotes.InlineFootnote;
import org.commonmark.node.*;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.html.HtmlNodeRendererContext;
Expand All @@ -26,13 +27,15 @@ public FootnoteMarkdownNodeRenderer(MarkdownNodeRendererContext context) {

@Override
public Set<Class<? extends Node>> getNodeTypes() {
return Set.of(FootnoteReference.class, FootnoteDefinition.class);
return Set.of(FootnoteReference.class, InlineFootnote.class, FootnoteDefinition.class);
}

@Override
public void render(Node node) {
if (node instanceof FootnoteReference) {
renderReference((FootnoteReference) node);
} else if (node instanceof InlineFootnote) {
renderInline((InlineFootnote) node);
} else if (node instanceof FootnoteDefinition) {
renderDefinition((FootnoteDefinition) node);
}
Expand All @@ -45,6 +48,12 @@ private void renderReference(FootnoteReference ref) {
writer.raw("]");
}

private void renderInline(InlineFootnote inlineFootnote) {
writer.raw("^[");
renderChildren(inlineFootnote);
writer.raw("]");
}

private void renderDefinition(FootnoteDefinition def) {
writer.raw("[^");
writer.raw(def.getLabel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import static org.junit.Assert.assertEquals;

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

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

@Test
public void testInline() {
assertRoundTrip("^[test *foo*]\n");
}

private void assertRoundTrip(String input) {
String rendered = parseAndRender(input);
assertEquals(input, rendered);
Expand Down

0 comments on commit e3795b2

Please sign in to comment.