diff --git a/lib/rdoc/markdown.kpeg b/lib/rdoc/markdown.kpeg
index 4b251f193d..174206ea74 100644
--- a/lib/rdoc/markdown.kpeg
+++ b/lib/rdoc/markdown.kpeg
@@ -1253,7 +1253,7 @@ TableRow = ( ( TableItem:item1 TableItem2*:items { [item1, *items] } ):row | Tab
{ row }
TableItem2 = "|" TableItem
TableItem = < /(?:\\.|[^|\n])+/ >
- { text.strip.gsub(/\\(.)/, '\1') }
+ { text.strip.gsub(/\\([|])/, '\1') }
TableLine = ( ( TableAlign:align1 TableAlign2*:aligns {[align1, *aligns] } ):line | TableAlign2+:line ) "|"? @Newline
{ line }
diff --git a/test/rdoc/rdoc_markdown_test.rb b/test/rdoc/rdoc_markdown_test.rb
index a825b27579..d33eec47f3 100644
--- a/test/rdoc/rdoc_markdown_test.rb
+++ b/test/rdoc/rdoc_markdown_test.rb
@@ -1157,6 +1157,36 @@ def test_gfm_table_with_links_and_code
assert_equal expected, doc
end
+ def test_gfm_table_with_backslashes_in_code_spans
+ doc = parse <<~MD
+ Plain text: `$\\\\` and `$\\\\ ` should work.
+
+ | Context | Code | Expected |
+ |---------|------|----------|
+ | Plain | `$\\\\` | Should show backslash |
+ | Escaped | `$\\\\ ` | Should show backslash |
+ | Multiple| `\\\\n\\\\t` | Should show backslashes |
+ MD
+
+ head = %w[Context Code Expected]
+ align = [nil, nil, nil]
+
+ body = [
+ ['Plain', '$\\\\
', 'Should show backslash'],
+ ['Escaped', '$\\\\
', 'Should show backslash'],
+ ['Multiple', '\\\\n\\\\t
', 'Should show backslashes'],
+ ]
+
+ expected_table = @RM::Table.new(head, align, body)
+
+ expected = doc(
+ para('Plain text: $\\\\
and $\\\\
should work.'),
+ expected_table
+ )
+
+ assert_equal expected, doc
+ end
+
def parse(text)
@parser.parse text
end