|
| 1 | +package org.commonmark.ext.footnotes; |
| 2 | + |
| 3 | +import org.commonmark.Extension; |
| 4 | +import org.commonmark.ext.footnotes.internal.*; |
| 5 | +import org.commonmark.parser.Parser; |
| 6 | +import org.commonmark.parser.beta.InlineContentParserFactory; |
| 7 | +import org.commonmark.renderer.NodeRenderer; |
| 8 | +import org.commonmark.renderer.html.HtmlRenderer; |
| 9 | +import org.commonmark.renderer.markdown.MarkdownNodeRendererContext; |
| 10 | +import org.commonmark.renderer.markdown.MarkdownNodeRendererFactory; |
| 11 | +import org.commonmark.renderer.markdown.MarkdownRenderer; |
| 12 | + |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +/** |
| 16 | + * Extension for footnotes with syntax like GitHub Flavored Markdown: |
| 17 | + * <pre><code> |
| 18 | + * Some text with a footnote[^1]. |
| 19 | + * |
| 20 | + * [^1]: The text of the footnote. |
| 21 | + * </code></pre> |
| 22 | + * The <code>[^1]</code> is a {@link FootnoteReference}, with "1" being the label. |
| 23 | + * <p> |
| 24 | + * The line with <code>[^1]: ...</code> is a {@link FootnoteDefinition}, with the contents as child nodes (can be a |
| 25 | + * paragraph like in the example, or other blocks like lists). |
| 26 | + * <p> |
| 27 | + * All the footnotes (definitions) will be rendered in a list at the end of a document, no matter where they appear in |
| 28 | + * the source. The footnotes will be numbered starting from 1, then 2, etc, depending on the order in which they appear |
| 29 | + * in the text (and not dependent on the label). The footnote reference is a link to the footnote, and from the footnote |
| 30 | + * there is a link back to the reference (or multiple). |
| 31 | + * <p> |
| 32 | + * There is also optional support for inline footnotes, use {@link #builder()} and then set {@link Builder#inlineFootnotes}. |
| 33 | + * |
| 34 | + * @see <a href="https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#footnotes">GitHub docs for footnotes</a> |
| 35 | + */ |
| 36 | +public class FootnotesExtension implements Parser.ParserExtension, |
| 37 | + HtmlRenderer.HtmlRendererExtension, |
| 38 | + MarkdownRenderer.MarkdownRendererExtension { |
| 39 | + |
| 40 | + private final boolean inlineFootnotes; |
| 41 | + |
| 42 | + private FootnotesExtension(boolean inlineFootnotes) { |
| 43 | + this.inlineFootnotes = inlineFootnotes; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * The extension with the default configuration (no support for inline footnotes). |
| 48 | + */ |
| 49 | + public static Extension create() { |
| 50 | + return builder().build(); |
| 51 | + } |
| 52 | + |
| 53 | + public static Builder builder() { |
| 54 | + return new Builder(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void extend(Parser.Builder parserBuilder) { |
| 59 | + parserBuilder |
| 60 | + .customBlockParserFactory(new FootnoteBlockParser.Factory()) |
| 61 | + .linkProcessor(new FootnoteLinkProcessor()); |
| 62 | + if (inlineFootnotes) { |
| 63 | + parserBuilder.linkMarker('^'); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void extend(HtmlRenderer.Builder rendererBuilder) { |
| 69 | + rendererBuilder.nodeRendererFactory(FootnoteHtmlNodeRenderer::new); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void extend(MarkdownRenderer.Builder rendererBuilder) { |
| 74 | + rendererBuilder.nodeRendererFactory(new MarkdownNodeRendererFactory() { |
| 75 | + @Override |
| 76 | + public NodeRenderer create(MarkdownNodeRendererContext context) { |
| 77 | + return new FootnoteMarkdownNodeRenderer(context); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Set<Character> getSpecialCharacters() { |
| 82 | + return Set.of(); |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + public static class Builder { |
| 88 | + |
| 89 | + private boolean inlineFootnotes = false; |
| 90 | + |
| 91 | + /** |
| 92 | + * Enable support for inline footnotes without definitions, e.g.: |
| 93 | + * <pre> |
| 94 | + * Some text^[this is an inline footnote] |
| 95 | + * </pre> |
| 96 | + */ |
| 97 | + public Builder inlineFootnotes(boolean inlineFootnotes) { |
| 98 | + this.inlineFootnotes = inlineFootnotes; |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + public FootnotesExtension build() { |
| 103 | + return new FootnotesExtension(inlineFootnotes); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments