# Add a maximum nesting depth for collapsible details

**URL:** <https://meta.discourse.org/t/add-a-maximum-nesting-depth-for-collapsible-details/414455>\
**Category:** Feature\
**Tags:** hide-details\
**Created:** [October 11, 2026, 5:20am UTC](https://meta.discourse.org/t/add-a-maximum-nesting-depth-for-collapsible-details/414455 "2026-10-11T05:20:13Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Ethsim2](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ethsim2/32/522255_2.png) [@Ethsim2](https://meta.discourse.org/u/Ethsim2)\
**Post date:** [October 11, 2026, 9:57am UTC](https://meta.discourse.org/t/add-a-maximum-nesting-depth-for-collapsible-details/414455/2 "2026-10-11T09:57:03Z")

</div>

Hi @scavin, thanks for reporting this and for providing the reproduction details.

I’ve been investigating the Markdown endpoint slowdown you described and identified an issue in `MarkdownEndpoint::CookedProcessor#replace_details`: nested `<details>` elements were being converted repeatedly, resulting in exponential processing.

I’ve opened a draft upstream PR with a fix:

> <https://github.com/discourse/discourse/pull/44461>
>
> \## Background
> 
> In \[scavin's Discourse Meta report\](https://meta.discourse.org/…t/add-a-maximum-nesting-depth-for-collapsible-details/414455), a post containing 24 nested \`\[details\]\` sections was associated with severe browser slowdowns and Markdown endpoint timeouts.
> 
> Investigation identified redundant recursive processing in \`MarkdownEndpoint::CookedProcessor#replace\_details\`.
> 
> \## Implemented: Markdown endpoint performance fix
> 
> The current implementation visits nested \`\<details\>\` elements in document order, recursively converting descendants multiple times.
> 
> For a chain of \`n\` nested details elements, recursive conversion calls grow as \`2^n - 1\`.
> 
> Processing the elements innermost-first using \`.to\_a.reverse\_each\` reduces this to \`n\` recursive conversions in the tested case.
> 
> \### Reproduction
> 
> At nesting depth 8:
> 
> | | Original | Patched |
> |---|---:|---:|
> | Recursive conversions | 255 | 8 |
> | Conversion time | 322 ms | 10.5 ms |
> 
> The measured speedup was approximately 31x.
> 
> \### Verification
> 
> \- Confirmed that the original implementation makes 255 recursive conversions at depth 8, versus 8 after the fix.
> \- Confirmed identical Markdown output in the synthetic nesting benchmarks.
> \- Added regression coverage for nested and sibling details.
> 
> \## Implemented: Configurable nesting limit
> 
> Added \`details\_max\_nesting\_depth\` to the built-in \`discourse-details\` plugin.
> 
> \- \`0\` (default): Unlimited nesting, preserving existing behaviour.
> \- Positive values from \`1\` to \`100\`: Maximum permitted nesting depth.
> \- Server-side validation applies when creating or editing posts.
> \- Over-limit submissions receive a translated validation error.
> \- Existing posts with unchanged raw content are not rejected merely because the setting changes.
> \- Sibling details do not count as additional nesting levels.
> \- Nested raw HTML \`\<details\>\` elements are also checked.
> \- Details syntax within fenced code blocks is ignored.
> 
> Validation occurs when a post is submitted or edited, not continuously during composition.
> 
> \### Feature verification
> 
> Added 12 feature-specific regression tests:
> 
> \- 10 model-validation tests covering limits, siblings, raw HTML, code fences and post edits.
> \- 2 HTTP request tests confirming that rejected creations and edits return HTTP 422 with the validation message.
> \- The rejected edit test also confirms that the original post remains unchanged.
> 
> The combined Markdown endpoint and Details plugin backend suite passed locally:
> 
> \*\*34 examples, 0 failures\*\*
> 
> Syntax Tree, RuboCop and \`git diff --check\` also passed.
> 
> The performance fix and configurable nesting limit are separate commits so either change can be reviewed independently.
> 
> The nesting limit is an additional safeguard; it does not itself guarantee that every browser-side performance issue is prevented.
> 
> \## References
> 
> \- \[Original Discourse Meta report\](https://meta.discourse.org/t/add-a-maximum-nesting-depth-for-collapsible-details/414455)
> \- \[Temporary depth-limit plugin\](https://github.com/scavin/discourse-details-depth-limit)

At eight nesting levels, the fix reduced recursive conversions from 255 to 8, with approximately a 31× speedup in my local benchmark. The PR has passed its initial GitHub CI checks.

I’m also working on your proposed `details_max_nesting_depth` site setting within the built-in Details plugin, using `0` for unlimited nesting to preserve existing behaviour. The initial implementation is written, but its integration tests are still in progress.

The performance fix addresses the server-side Markdown conversion problem; the configurable limit would provide an additional safeguard, particularly for browser-side performance.

I’d welcome feedback on the proposed setting and whether it would be preferable to include it in the same PR or submit it separately.

---

_[View the full topic](https://meta.discourse.org/t/add-a-maximum-nesting-depth-for-collapsible-details/414455)._
