Skip to content

Commit a406b01

Browse files
committed
Support markdown syntax in table cells
This will allow things like backticks and links being rendered in table cells.
1 parent f3204b7 commit a406b01

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

lib/rdoc/markdown.kpeg

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,46 @@
496496
"<s>#{text}</s>"
497497
end
498498
end
499+
500+
##
501+
# Parses inline markdown in table cells
502+
503+
def parse_table_cells(table)
504+
# Parse header cells
505+
table.header = table.header.map { |cell| parse_cell_inline(cell) }
506+
507+
# Parse body cells
508+
table.body = table.body.map do |row|
509+
row.map { |cell| parse_cell_inline(cell) }
510+
end
511+
512+
table
513+
end
514+
515+
##
516+
# Parses inline markdown in a single table cell
517+
518+
def parse_cell_inline(text)
519+
return text if text.nil? || text.empty?
520+
521+
# Create a new parser instance for the cell
522+
cell_parser = RDoc::Markdown.new(@extensions, @debug)
523+
524+
# Parse the cell content
525+
doc = cell_parser.parse(text)
526+
527+
# Extract the parsed content
528+
if doc && doc.parts && !doc.parts.empty?
529+
para = doc.parts.first
530+
if para.respond_to?(:parts)
531+
para.parts.join
532+
else
533+
text
534+
end
535+
else
536+
text
537+
end
538+
end
499539
}
500540

501541
root = Doc
@@ -1201,7 +1241,10 @@ CodeFence = &{ github? }
12011241

12021242
Table = &{ github? }
12031243
TableHead:header TableLine:line TableRow+:body
1204-
{ table = RDoc::Markup::Table.new(header, line, body) }
1244+
{
1245+
table = RDoc::Markup::Table.new(header, line, body)
1246+
parse_table_cells(table)
1247+
}
12051248

12061249
TableHead = TableItem2+:items "|"? @Newline
12071250
{ items }

test/rdoc/rdoc_markdown_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,30 @@ def test_gfm_table_2
11331133
assert_equal expected, doc
11341134
end
11351135

1136+
def test_gfm_table_with_links_and_code
1137+
doc = parse <<~MD
1138+
| Feature | Description | Example |
1139+
|---------|-------------|---------|
1140+
| Link | [RDoc](https://github.com/ruby/rdoc) | See docs |
1141+
| Code | `puts "hello"` | Inline code |
1142+
| Both | Link to [`method`](https://example.com) | Combined |
1143+
MD
1144+
1145+
head = %w[Feature Description Example]
1146+
align = [nil, nil, nil]
1147+
1148+
# Expected behavior: table cells should contain parsed markup
1149+
body = [
1150+
['Link', '{RDoc}[https://github.com/ruby/rdoc]', 'See docs'],
1151+
['Code', '<code>puts "hello"</code>', 'Inline code'],
1152+
['Both', 'Link to {<code>method</code>}[https://example.com]', 'Combined'],
1153+
]
1154+
1155+
expected = doc(@RM::Table.new(head, align, body))
1156+
1157+
assert_equal expected, doc
1158+
end
1159+
11361160
def parse(text)
11371161
@parser.parse text
11381162
end

0 commit comments

Comments
 (0)