Skip to content

Support markdown syntax in table cells #1386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion lib/rdoc/markdown.kpeg
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,46 @@
"<s>#{text}</s>"
end
end

##
# Parses inline markdown in table cells

def parse_table_cells(table)
# Parse header cells
table.header = table.header.map { |cell| parse_cell_inline(cell) }

# Parse body cells
table.body = table.body.map do |row|
row.map { |cell| parse_cell_inline(cell) }
end

table
end

##
# Parses inline markdown in a single table cell

def parse_cell_inline(text)
return text if text.nil? || text.empty?

# Create a new parser instance for the cell
cell_parser = RDoc::Markdown.new(@extensions, @debug)

# Parse the cell content
doc = cell_parser.parse(text)

# Extract the parsed content
if doc && doc.parts && !doc.parts.empty?
para = doc.parts.first
if para.is_a?(RDoc::Markup::Paragraph)
para.parts.join
else
text
end
else
text
end
end
}

root = Doc
Expand Down Expand Up @@ -1201,7 +1241,10 @@ CodeFence = &{ github? }

Table = &{ github? }
TableHead:header TableLine:line TableRow+:body
{ table = RDoc::Markup::Table.new(header, line, body) }
{
table = RDoc::Markup::Table.new(header, line, body)
parse_table_cells(table)
}

TableHead = TableItem2+:items "|"? @Newline
{ items }
Expand Down
24 changes: 24 additions & 0 deletions test/rdoc/rdoc_markdown_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,30 @@ def test_gfm_table_2
assert_equal expected, doc
end

def test_gfm_table_with_links_and_code
doc = parse <<~MD
| Feature | Description | Example |
|---------|-------------|---------|
| Link | [RDoc](https://github.com/ruby/rdoc) | See docs |
| Code | `puts "hello"` | Inline code |
| Both | Link to [`method`](https://example.com) | Combined |
MD

head = %w[Feature Description Example]
align = [nil, nil, nil]

# Expected behavior: table cells should contain parsed markup
body = [
['Link', '{RDoc}[https://github.com/ruby/rdoc]', 'See docs'],
['Code', '<code>puts "hello"</code>', 'Inline code'],
['Both', 'Link to {<code>method</code>}[https://example.com]', 'Combined'],
]

expected = doc(@RM::Table.new(head, align, body))

assert_equal expected, doc
end

def parse(text)
@parser.parse text
end
Expand Down