Skip to content

Commit 75f9a32

Browse files
fix(tostring): recurse into nested inline elements inside bold/italic
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).
1 parent 38a14a4 commit 75f9a32

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

lua/markview/renderers/markdown/tostring.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ md_str.bold = function (match)
168168
removed = string.gsub(match, "^%_%_", ""):gsub("%_%_$", "");
169169
end
170170

171-
return removed;
171+
return md_str.tostring(md_str.buffer, removed, false);
172172

173173
---|fE
174174
end
@@ -192,7 +192,8 @@ md_str.bold_italic = function (match)
192192
r = math.min(be and #be or 0, af and #af or 0);
193193
end
194194

195-
return vim.fn.strpart(match, r, vim.fn.strchars(match) - (r + r));
195+
local removed = vim.fn.strpart(match, r, vim.fn.strchars(match) - (r + r));
196+
return md_str.tostring(md_str.buffer, removed, false);
196197

197198
---|fE
198199
end
@@ -339,7 +340,7 @@ md_str.italic = function (match)
339340
removed = string.gsub(match, "^%_", ""):gsub("%_$", "");
340341
end
341342

342-
return removed;
343+
return md_str.tostring(md_str.buffer, removed, false);
343344

344345
---|fE
345346
end

test/tostring_recursion.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; Bold/italic wrapping inline elements — tostring recursion
2+
3+
### Bold + link
4+
5+
| Kind | Short | Long |
6+
|------|-------|------|
7+
| Bold + link | **[bold link](https://example.com)** | **[bold link with long URL](https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis-combined-with-links-and-images)** |
8+
| Italic + link | *[italic link](https://example.com)* | *[italic link with long URL](https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis-combined-with-links-and-images)* |
9+
| Bold-italic + link | ***[both](https://example.com)*** | ***[both with long URL](https://spec.commonmark.org/0.31.2/#emphasis-and-strong-emphasis-combined-with-links-and-images)*** |
10+
11+
### Bold + image
12+
13+
| Kind | Short | Long |
14+
|------|-------|------|
15+
| Bold + image | **![icon](https://example.com/i.svg)** | **![a]( https://raw.githubusercontent.com/nvim-treesitter/playground/master/assets/screenshot.png)** |
16+
| Italic + image | *![icon](https://example.com/i.svg)* | *![a](https://raw.githubusercontent.com/nvim-treesitter/playground/master/assets/screenshot.png)* |
17+
18+
### Bold + code
19+
20+
| Kind | Short | Long |
21+
|------|-------|------|
22+
| Bold + code | **`short`** | **`vim.api.nvim_buf_set_extmark(buffer, ns, row, col, opts)`** |
23+
| Italic + code | *`short`* | *`vim.api.nvim_buf_set_extmark(buffer, ns, row, col, opts)`* |
24+
25+
### Mixed nesting
26+
27+
| Description | Content |
28+
|-------------|---------|
29+
| Bold wrapping link + code | **[link](https://example.com) and `code`** |
30+
| Plain bold (no nesting) | **just bold text** |
31+
| Plain italic | *just italic text* |
32+
| No emphasis | [link](https://example.com) then `code` |

0 commit comments

Comments
 (0)