foundry icon indicating copy to clipboard operation
foundry copied to clipboard

fix(doc): preserve NatSpec characters in fenced code blocks

Open mablr opened this issue 3 months ago • 1 comments

Forge documentation escapes MDX-sensitive characters such as < and { in NatSpec text. This also affects fenced code examples, where those characters are safe and should remain literal.

Implement a narrowly scoped solution that:

  • preserves < and { inside unambiguous backtick or tilde fenced code blocks;
  • retains existing escaping for prose, inline code, tables, malformed Markdown, and ambiguous GFM constructs;
  • supports standard fence indentation and closing rules;
  • adds focused unit and Forge documentation-generation tests.

The implementation should not attempt to reproduce the complete downstream MDX/GFM parsing pipeline.

mablr avatar Sep 05 '26 13:09 mablr

Opening #16696 for this.

The escaping was running in two places without fence awareness — per /// line in collect_comments (solar emits one item per line, before continuations are stitched together) and on stitched descriptions in replace_inline_links — so fenced examples were being escaped at both layers.

The fix tracks the CommonMark fenced-code-block rules MDX inherits (backtick/tilde markers, the info-string backtick rule, <=3-space indentation, same-marker closing) with a small per-line FenceTracker for the item stream and whole-string fenced_code_regions for the stitched path, and emits fence lines verbatim at both sites. Escaping outside fences (prose, inline code spans, tables) is unchanged byte-for-byte, per the scope note in the issue.

Tests: 8 focused unit tests in hir_ext.rs (fence variants incl. the ambiguous info-string rule, indentation, unclosed fences) plus a Forge documentation-generation test in crates/forge/tests/cli/doc.rs asserting the fenced example reaches the generated page verbatim while prose escaping and {Ident} links are unaffected.

DefiAudit0x avatar Sep 06 '26 18:09 DefiAudit0x