APIdia Launch Partner
Top Description Inners Fields Constructors Methods
jdk.internal.org.commonmark.parser

public Class Parser

extends Object
Class Inheritance
Imports
jdk.internal.org.commonmark.Extension, jdk.internal.org.commonmark.internal.DocumentParser, .InlineParserContextImpl, .InlineParserImpl, .LinkReferenceDefinitions, jdk.internal.org.commonmark.node.*, jdk.internal.org.commonmark.parser.block.BlockParserFactory, jdk.internal.org.commonmark.parser.delimiter.DelimiterProcessor, java.io.IOException, .Reader, java.util.ArrayList, .List, .Set

Parses input text to a tree of nodes.

Start with the builder method, configure the parser and build it. Example:


Parser parser = Parser.builder().build();
Node document = parser.parse("input text");

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
Parser.Builder

Builder for configuring a Parser.

public static interface

Field Summary

Modifier and TypeField and Description
private final List<BlockParserFactory>
private final List<DelimiterProcessor>
private final IncludeSourceSpans
private final InlineParserFactory
private final List<PostProcessor>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Parser.Builder

Returns:

a builder
builder
()

Create a new builder for configuring a Parser.

private DocumentParser
public Node

Returns:

the root node
parse
(String
the text to parse - must not be null
input
)

Parse the specified input text into a tree of nodes.

public Node

Returns:

the root node
parseReader
(Reader
the reader to parse - must not be null
input
)

Parse the specified reader into a tree of nodes.

private Node
postProcess(Node document)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blockParserFactoriesback to summary
private final List<BlockParserFactory> blockParserFactories
delimiterProcessorsback to summary
private final List<DelimiterProcessor> delimiterProcessors
includeSourceSpansback to summary
private final IncludeSourceSpans includeSourceSpans
inlineParserFactoryback to summary
private final InlineParserFactory inlineParserFactory
postProcessorsback to summary
private final List<PostProcessor> postProcessors

Constructor Detail

Parserback to summary
private Parser(Parser.Builder builder)

Method Detail

builderback to summary
public static Parser.Builder builder()

Create a new builder for configuring a Parser.

Returns:Parser.Builder

a builder

createDocumentParserback to summary
private DocumentParser createDocumentParser()
parseback to summary
public Node parse(String input)

Parse the specified input text into a tree of nodes.

This method is thread-safe (a new parser state is used for each invocation).

Parameters
input:String

the text to parse - must not be null

Returns:Node

the root node

parseReaderback to summary
public Node parseReader(Reader input) throws IOException

Parse the specified reader into a tree of nodes. The caller is responsible for closing the reader.


Parser parser = Parser.builder().build();
try (InputStreamReader reader = new InputStreamReader(new FileInputStream("file.md"), StandardCharsets.UTF_8)) {
    Node document = parser.parseReader(reader);
    // ...
}
Note that if you have a file with a byte order mark (BOM), you need to skip it before handing the reader to this library. There's existing classes that do that, e.g. see BOMInputStream in Commons IO.

This method is thread-safe (a new parser state is used for each invocation).

Parameters
input:Reader

the reader to parse - must not be null

Returns:Node

the root node

Exceptions
IOException:
when reading throws an exception
postProcessback to summary
private Node postProcess(Node document)
jdk.internal.org.commonmark.parser back to summary

public Class Parser.Builder

extends Object
Class Inheritance

Builder for configuring a Parser.

Field Summary

Modifier and TypeField and Description
private final List<BlockParserFactory>
private final List<DelimiterProcessor>
private Set<Class<? extends Block>>
private IncludeSourceSpans
private InlineParserFactory
private final List<PostProcessor>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Parser
build()

Returns: the configured Parser

public Parser.Builder

Returns:

this
customBlockParserFactory
(BlockParserFactory
a block parser factory implementation
blockParserFactory
)

Adds a custom block parser factory.

public Parser.Builder

Returns:

this
customDelimiterProcessor
(DelimiterProcessor
a delimiter processor implementation
delimiterProcessor
)

Adds a custom delimiter processor.

public Parser.Builder

Returns:

this
enabledBlockTypes
(Set<Class<? extends Block>>
A list of block nodes the parser will parse. If this list is empty, the parser will not recognize any CommonMark core features.
enabledBlockTypes
)

Describe the list of markdown features the parser will recognize and parse.

public Parser.Builder
extensions(Iterable<? extends Extension>
extensions to use on this parser
extensions
)

Returns: this

private InlineParserFactory
public Parser.Builder

Returns:

this
includeSourceSpans
(IncludeSourceSpans
which kind of source spans should be included
includeSourceSpans
)

Whether to calculate org.commonmark.node.SourceSpan for Node.

public Parser.Builder

Returns:

this
inlineParserFactory
(InlineParserFactory
an inline parser factory implementation
inlineParserFactory
)

Overrides the parser used for inline markdown processing.

public Parser.Builder
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blockParserFactoriesback to summary
private final List<BlockParserFactory> blockParserFactories
delimiterProcessorsback to summary
private final List<DelimiterProcessor> delimiterProcessors
enabledBlockTypesback to summary
private Set<Class<? extends Block>> enabledBlockTypes
includeSourceSpansback to summary
private IncludeSourceSpans includeSourceSpans
inlineParserFactoryback to summary
private InlineParserFactory inlineParserFactory
postProcessorsback to summary
private final List<PostProcessor> postProcessors

Constructor Detail

Builderback to summary
public Builder()

Method Detail

buildback to summary
public Parser build()
Returns:Parser

the configured Parser

customBlockParserFactoryback to summary
public Parser.Builder customBlockParserFactory(BlockParserFactory blockParserFactory)

Adds 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:BlockParserFactory

a block parser factory implementation

Returns:Parser.Builder

this

customDelimiterProcessorback to summary
public Parser.Builder customDelimiterProcessor(DelimiterProcessor delimiterProcessor)

Adds a custom delimiter processor.

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.

Parameters
delimiterProcessor:DelimiterProcessor

a delimiter processor implementation

Returns:Parser.Builder

this

enabledBlockTypesback to summary
public Parser.Builder enabledBlockTypes(Set<Class<? extends Block>> 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:

To parse only a subset of the features listed above, pass a list of each feature's associated Block class.

E.g., to only parse headings and lists:

    Parser.builder().enabledBlockTypes(new HashSet<>(Arrays.asList(Heading.class, ListBlock.class)));
    
Parameters
enabledBlockTypes:Set<Class<? extends Block>>

A list of block nodes the parser will parse. If this list is empty, the parser will not recognize any CommonMark core features.

Returns:Parser.Builder

this

extensionsback to summary
public Parser.Builder extensions(Iterable<? extends Extension> extensions)
Parameters
extensions:Iterable<? extends Extension>

extensions to use on this parser

Returns:Parser.Builder

this

getInlineParserFactoryback to summary
private InlineParserFactory getInlineParserFactory()
includeSourceSpansback to summary
public Parser.Builder includeSourceSpans(IncludeSourceSpans includeSourceSpans)

Whether to calculate org.commonmark.node.SourceSpan for Node.

By default, source spans are disabled.

Parameters
includeSourceSpans:IncludeSourceSpans

which kind of source spans should be included

Returns:Parser.Builder

this

Since
0.16.0
inlineParserFactoryback to summary
public Parser.Builder inlineParserFactory(InlineParserFactory 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 (![alt](http://))

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:InlineParserFactory

an inline parser factory implementation

Returns:Parser.Builder

this

postProcessorback to summary
public Parser.Builder postProcessor(PostProcessor postProcessor)
jdk.internal.org.commonmark.parser back to summary

public Interface Parser.ParserExtension

extends Extension
Known Direct Implementers
jdk.internal.org.commonmark.ext.gfm.tables.TablesExtension

Extension for Parser.

Method Summary

Modifier and TypeMethod and Description
public void
extend(Parser.Builder parserBuilder)

Method Detail

extendback to summary
public void extend(Parser.Builder parserBuilder)