- Enclosing class:
- Parser
Parser.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<BlockParserFactory>private final List<DelimiterProcessor>private IncludeSourceSpansprivate final List<InlineContentParserFactory>private InlineParserFactoryprivate final List<LinkProcessor>private intprivate intprivate final List<PostProcessor> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()customBlockParserFactory(BlockParserFactory blockParserFactory) Add a custom block parser factory.customDelimiterProcessor(DelimiterProcessor delimiterProcessor) Add a custom delimiter processor for inline parsing.customInlineContentParserFactory(InlineContentParserFactory inlineContentParserFactory) Add a factory for a custom inline content parser, for extending inline parsing or overriding built-in parsing.enabledBlockTypes(Set<Class<? extends Block>> enabledBlockTypes) Describe the list of markdown features the parser will recognize and parse.extensions(Iterable<? extends Extension> extensions) private InlineParserFactoryincludeSourceSpans(IncludeSourceSpans includeSourceSpans) Whether to calculate source positions for parsedNodes, seeNode.getSourceSpans().inlineParserFactory(InlineParserFactory inlineParserFactory) Overrides the parser used for inline markdown processing.linkMarker(Character linkMarker) Add a custom link marker for link processing.linkProcessor(LinkProcessor linkProcessor) Add a custom link/image processor for inline parsing.maxInlineNesting(int maxInlineNesting) Limit how deeply inline nodes may nest inside each other while parsing, to protect against malicious input.maxOpenBlockParsers(int maxOpenBlockParsers) Limit how many block parsers may be open at once while parsing.postProcessor(PostProcessor postProcessor)
-
Field Details
-
blockParserFactories
-
inlineContentParserFactories
-
delimiterProcessors
-
linkProcessors
-
postProcessors
-
linkMarkers
-
enabledBlockTypes
-
inlineParserFactory
-
includeSourceSpans
-
maxOpenBlockParsers
private int maxOpenBlockParsers -
maxInlineNesting
private int maxInlineNesting
-
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
build
- Returns:
- the configured
Parser
-
extensions
- Parameters:
extensions- extensions to use on this parser- Returns:
this
-
enabledBlockTypes
Describe the list of markdown features the parser will recognize and parse.By default, CommonMark will recognize and parse the following set of "block" elements:
Heading(#)HtmlBlock(<html></html>)ThematicBreak(Horizontal Rule) (---)FencedCodeBlock(```)IndentedCodeBlockBlockQuote(>)ListBlock(Ordered / Unordered List) (1. / *)
To parse only a subset of the features listed above, pass a list of each feature's associated
Blockclass.E.g., to only parse headings and lists:
Parser.builder().enabledBlockTypes(Set.of(Heading.class, ListBlock.class));- Parameters:
enabledBlockTypes- A list of block nodes the parser will parse. If this list is empty, the parser will not recognize any CommonMark core features.- Returns:
this
-
includeSourceSpans
Whether to calculate source positions for parsedNodes, seeNode.getSourceSpans().By default, source spans are disabled.
- Parameters:
includeSourceSpans- which kind of source spans should be included- Returns:
this- Since:
- 0.16.0
-
maxOpenBlockParsers
Limit how many block parsers may be open at once while parsing.Once the limit is reached, additional block starts are treated as plain text instead of creating deeper nested block structure. The document root parser is not counted.
The default is 100; use
Integer.MAX_VALUEfor no limit.- Parameters:
maxOpenBlockParsers- maximum number of open non-document block parsers, must be zero or greater- Returns:
this
-
maxInlineNesting
Limit how deeply inline nodes may nest inside each other while parsing, to protect against malicious input. This covers emphasis-like delimiters (e.g.*or the ins extension's+) as well as images, both of which can nest arbitrarily deep for a small amount of input:- Some delimiter processors only consume part of a run of delimiters at a time (e.g.
two
*characters forStrongEmphasis), so a run of delimiters around a single character can nest one level deeper for each pair that's consumed, as in"*".repeat(n) + "x" + "*".repeat(n). - Images can contain images, as in
"".repeat(n).
Once the limit is reached, the delimiters or brackets that would nest another level are treated as plain text instead. Note that the limit applies to the nesting of the resulting nodes, no matter which of the above (or which combination of them) produced it.
Deeply nested inline nodes are a problem for any code that walks the resulting tree recursively, such as the renderers in this library, which would otherwise fail with a
StackOverflowError.The default is 100; use
Integer.MAX_VALUEfor no limit.- Parameters:
maxInlineNesting- maximum nesting depth of inline nodes, must be zero or greater- Returns:
this
- Some delimiter processors only consume part of a run of delimiters at a time (e.g.
two
-
customBlockParserFactory
Add a custom block parser factory.Note that custom factories are applied before the built-in factories. This is so that extensions can change how some syntax is parsed that would otherwise be handled by built-in factories. "With great power comes great responsibility."
- Parameters:
blockParserFactory- a block parser factory implementation- Returns:
this
-
customInlineContentParserFactory
public Parser.Builder customInlineContentParserFactory(InlineContentParserFactory inlineContentParserFactory) Add a factory for a custom inline content parser, for extending inline parsing or overriding built-in parsing.Note that parsers are triggered based on a special character as specified by
InlineContentParserFactory.getTriggerCharacters(). It is possible to register multiple parsers for the same character, or even for some built-in special character such as`. The custom parsers are tried first in order in which they are registered, and then the built-in ones. -
customDelimiterProcessor
Add a custom delimiter processor for inline parsing.Note that multiple delimiter processors with the same characters can be added, as long as they have a different minimum length. In that case, the processor with the shortest matching length is used. Adding more than one delimiter processor with the same character and minimum length is invalid.
If you want more control over how parsing is done, you might want to use
customInlineContentParserFactory(org.commonmark.parser.beta.InlineContentParserFactory)instead.- Parameters:
delimiterProcessor- a delimiter processor implementation- Returns:
this
-
linkProcessor
Add a custom link/image processor for inline parsing.Multiple link processors can be added, and will be tried in order in which they were added. If no link processor applies, the normal behavior applies. That means these can override built-in link parsing.
- Parameters:
linkProcessor- a link processor implementation- Returns:
this
-
linkMarker
Add a custom link marker for link processing. A link marker is a character like!which, if it appears before the[of a link, changes the meaning of the link.If a link marker followed by a valid link is parsed, the
LinkInfothat is passed toLinkProcessorwill have itsLinkInfo.marker()set. A link processor should check theText.getLiteral()and then do any processing, and will probably want to useLinkResult.includeMarker().- Parameters:
linkMarker- a link marker character- Returns:
this
-
postProcessor
-
inlineParserFactory
Overrides the parser used for inline markdown processing.Provide an implementation of InlineParserFactory which provides a custom inline parser to modify how the following are parsed: bold (**) italic (*) strikethrough (~~) backtick quote (`) link ([title](http://)) image ()
Note that if this method is not called or the inline parser factory is set to null, then the default implementation will be used.
- Parameters:
inlineParserFactory- an inline parser factory implementation- Returns:
this
-
getInlineParserFactory
-