//////////////////////////////////////////

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.

//////////////////////////////////////////

= Processing Markdown

Groovy has an optional `groovy-markdown` module which provides support for
parsing https://commonmark.org/[CommonMark] Markdown. The classes are found
in the `groovy.markdown` package.

[[markdown_markdownslurper]]
== MarkdownSlurper

`MarkdownSlurper` parses Markdown text into a `MarkdownDocument` backed by
nested lists and maps. Each node is a `Map` with a `type` key plus
type-specific fields:

[source,groovy]
----
include::../test/groovy/markdown/MarkdownSlurperTest.groovy[tags=parse_heading,indent=0]
----

The raw structure supports all standard Groovy list/map operations. The
document also exposes convenience properties — `headings`, `codeBlocks`,
`links`, `tables` — that recursively walk the tree.

=== Extracting code blocks

A common pattern when consuming LLM output is to pull fenced code blocks
by language:

[source,groovy]
----
include::../test/groovy/markdown/MarkdownSlurperTest.groovy[tags=code_blocks,indent=0]
----

`codeBlocks` walks the entire tree, so blocks nested inside block quotes
or list items are included.

=== Links

[source,groovy]
----
include::../test/groovy/markdown/MarkdownSlurperTest.groovy[tags=links,indent=0]
----

=== Sections

`section(headingText)` returns the nodes between the given heading and the
next heading of equal or higher level — useful for parsing structured
agent replies:

[source,groovy]
----
include::../test/groovy/markdown/MarkdownSlurperTest.groovy[tags=section,indent=0]
----

=== Tables (optional)

GFM-style tables are supported when the
`org.commonmark:commonmark-ext-gfm-tables` jar is on the classpath. Call
`enableTables(true)` on the slurper:

[source,groovy]
----
include::../test/groovy/markdown/MarkdownSlurperTest.groovy[tags=tables,indent=0]
----

Each row is returned as a `Map` keyed by header.

=== Node types

[cols="1,3"]
|===
| type | fields

| `heading` | `level`, `text`, `children`
| `paragraph` | `children`
| `code_block` | `lang`, `text`
| `list` | `ordered`, `items` (`start` for ordered lists)
| `list_item` | `text`, `children`
| `block_quote` | `children`
| `link` | `href`, `title`, `text`, `children`
| `image` | `src`, `title`, `alt`
| `text` | `value`
| `inline_code` | `text`
| `emphasis`, `strong` | `children`
| `html_block`, `html_inline` | `text`
| `thematic_break` | (no fields)
| `hard_line_break`, `soft_line_break` | (no fields)
| `table` | `headers`, `alignments`, `rows`
|===

=== Untrusted input

Prefer parsing Markdown from trusted sources. Like the sibling `JsonSlurper`
and `XmlSlurper`, `MarkdownSlurper` is a convenience parser, not a security
boundary, so the safest choice is not to hand it attacker-controlled input. If
you must, bound the input by size yourself before parsing and treat the parsed
result defensively.

As a backstop, `MarkdownSlurper` caps how deeply a document may nest, so that a
small but pathologically nested input -- thousands of nested block quotes, or a
long run of emphasis markers -- is reported as a `MarkdownRuntimeException`
rather than being allowed to exhaust the stack with a `StackOverflowError`. The
limit defaults to `1000` and can be set per instance or globally via the
`groovy.markdown.maxNestingDepth` system property; a value of `0` or less
disables the check.

[source,groovy]
----
include::../test/groovy/markdown/MarkdownSlurperTest.groovy[tags=nesting_depth,indent=0]
----

This cap is a robustness backstop, not a licence to treat the parser as hardened.
