@Incubating
public class MarkdownSlurper
extends Object
Parses CommonMark Markdown into a MarkdownDocument backed by nested lists and maps.
Usage:
def doc = new groovy.markdown.MarkdownSlurper().parseText('# Hello')
assert doc.headings[0].text == 'Hello'
GFM-style tables are supported via an optional extension. Call
enableTables(boolean) enableTables(true) after adding
org.commonmark:commonmark-ext-gfm-tables to the runtime classpath.
JsonSlurper and
XmlSlurper, this is a convenience parser, not a security boundary, and the safest
posture is not to feed it attacker-controlled input. If you must, bound the input by size
yourself before parsing and treat the parsed result defensively.
As a backstop for that case, a small but deeply nested document — which could otherwise drive a recursive parse into a StackOverflowError — is reported as a MarkdownRuntimeException rather than a raw Error. There are two independent vectors, on opposite sides of the CommonMark boundary, and both are covered:
'>' * 50000). CommonMark parses
blocks iteratively and returns a very deep tree; the overflow would happen in
this slurper's own recursive walk. maxNestingDepth
bounds this at parse time (via CommonMark's maxOpenBlockParsers) and rejects
any document nested deeper than the limit.('*' * 50000) + 'a' + ('*' * 50000)).
This overflows inside CommonMark's own inline processing, before control returns
here; CommonMark 0.29.0 has no inline-nesting cap, so it is caught and reported
as a MarkdownRuntimeException.| Modifiers | Name | Description |
|---|---|---|
static int |
DEFAULT_MAX_NESTING_DEPTH |
Default maximum nesting depth of block/inline elements accepted before a MarkdownRuntimeException is thrown. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public MarkdownSlurper |
enableTables(boolean enable)Enable GFM-style tables. |
|
public int |
getMaxNestingDepth()Returns the maximum block/container nesting depth the parser will accept. |
|
public MarkdownDocument |
parse(Reader reader)Parses Markdown content from a reader. |
|
public MarkdownDocument |
parse(InputStream stream)Parses Markdown content from an input stream. |
|
public MarkdownDocument |
parse(File file)Parses Markdown content from a file. |
|
public MarkdownDocument |
parse(Path path)Parses Markdown content from a path. |
|
public MarkdownDocument |
parseText(String md)Parses Markdown text into a MarkdownDocument. |
|
public void |
setMaxNestingDepth(int maxNestingDepth)Sets the maximum block/container nesting depth. |
Default maximum nesting depth of block/inline elements accepted before a
MarkdownRuntimeException is thrown. Matches the default nesting cap of the
sibling JsonSlurper.
Enable GFM-style tables. Requires commonmark-ext-gfm-tables on the classpath.
enable is true but the extension jar is missingenable - whether to enable table parsingReturns the maximum block/container nesting depth the parser will accept.
<= 0 when the limit is disabledParses Markdown content from a reader.
reader - the reader supplying Markdown contentParses Markdown content from an input stream. The caller remains responsible for closing the stream.
stream - the input stream supplying Markdown contentParses Markdown content from a file.
file - the file to readParses Markdown content from a path.
path - the path to readParses Markdown text into a MarkdownDocument.
md - the Markdown text to parse Sets the maximum block/container nesting depth. Over-limit nesting is bounded at parse time
(via CommonMark's maxOpenBlockParsers) and rejected with a MarkdownRuntimeException.
A value of 0 or less disables the limit; deeply nested inline emphasis is still caught
and reported regardless.
maxNestingDepth - maximum number of nested block elements to allow