Skip to content

fix(tostring): recurse into nested inline elements inside bold/italic#485

Merged
OXY2DEV merged 1 commit intoOXY2DEV:mainfrom
ImmanuelHaffner:fix/tostring-bold-italic-recursion
Mar 20, 2026
Merged

fix(tostring): recurse into nested inline elements inside bold/italic#485
OXY2DEV merged 1 commit intoOXY2DEV:mainfrom
ImmanuelHaffner:fix/tostring-bold-italic-recursion

Conversation

@ImmanuelHaffner
Copy link
Contributor

Problem

md_str.bold(), md_str.italic(), and md_str.bold_italic() in the tostring module strip their delimiter characters but return the raw inner text without further processing. Nested inline constructs (links, images, code spans, etc.) inside emphasis are never resolved to their visual representation.

Input tostring returned Actual renderer shows Width error
**[bold link](https://example.com)** [bold link](https://example.com) (width 32) 󰌷 bold link (width 11) +21
**[bold link with long URL](https://…long…)** full URL text (width 122) 󰌷 bold link with long URL (width 25) +97
*[italic link](url)* [italic link](url) 󰌷 italic link similar

This causes massive column width miscalculations in rendered tables.

Fix

Pass the stripped inner text through md_str.tostring() so that nested inline elements are properly reduced. Three one-line changes:

-- bold, italic, bold_italic: before
return removed;

-- after
return md_str.tostring(md_str.buffer, removed, false);

This mirrors the established pattern in the sibling module visual_text.lua, where md_delims_star() calls visual.markdown(inner).

Performance

The recursive lpeg.match only fires on cells where bold/italic actually wraps another inline element — a small subset in practice. Benchmarked overhead on representative table cells:

Scenario Overhead % of 16ms frame budget
Realistic (5×8 table, 2–3 nested cells) +0.02 ms negligible
Stress test (~60 cells, ~5 nested) +0.10 ms 0.6%
Worst case (200 cells, 20 nested) +0.41 ms 2.6%

Verification

✅ **[bold link](url)**               → width=11 (was 32)
✅ **[bold link with long URL](…)**   → width=25 (was 122)
✅ *[italic link](url)*               → width=13 (was 31)
✅ ***[bold italic link](url)***      → width=18 (was 40)
✅ **`code`**                         → width=18 (was 18, already correct)
✅ **bold** / *italic* / ***both***   → unchanged
✅ [link](url) / `code` / ![img](url) → unchanged

md_str.bold(), md_str.italic(), and md_str.bold_italic() stripped
their delimiter characters but returned the raw inner text without
further processing.  This meant nested inline constructs like links,
images, or code spans inside emphasis were never resolved.

For example, **[bold link](https://example.com)** produced the visual
string "[bold link](https://example.com)" (width 32) instead of the
correct "󰌷 bold link" (width 11), causing massive column width
miscalculations in rendered tables.

Pass the stripped inner text through md_str.tostring() so that nested
inline elements (hyperlinks, images, code spans, etc.) are properly
reduced to their visual representation.  This mirrors the established
pattern already used in the sibling module visual_text.lua, where
md_delims_star() calls visual.markdown(inner).
@OXY2DEV OXY2DEV merged commit 1861f95 into OXY2DEV:main Mar 20, 2026
1 check passed
@ImmanuelHaffner ImmanuelHaffner deleted the fix/tostring-bold-italic-recursion branch March 20, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants