Skip to content

Commit 3127748

Browse files
committed
fix: keep a backtick in the checked value inside its code span
The span delimiter now grows past the longest backtick run in the value, with padding spaces when the value starts or ends with one.
1 parent ffae6bd commit 3127748

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎main.py‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,22 @@ def _skip_count(results: list[ScopeResult]) -> int:
565565
_STATUS_ICONS = {"pass": "\u2705", "fail": "\u274c", "skip": "\u2298"}
566566

567567

568-
def _table_cell_escape(text: str) -> str:
569-
"""Escape a value for a Markdown table cell.
570-
571-
A ``|`` inside a cell ends the cell; a commit subject is arbitrary text.
568+
def _table_cell_code(text: str) -> str:
569+
"""Render a checked value as a code span safe for a Markdown table cell.
570+
571+
A commit subject is arbitrary text. A ``|`` inside a cell ends the cell,
572+
and a backtick would end a fixed single-backtick span, so the delimiter
573+
grows past the longest backtick run in the value instead. A value that
574+
starts or ends with a backtick additionally needs the padding spaces —
575+
CommonMark strips exactly one of each.
572576
"""
573-
return text.replace("|", "\\|")
577+
escaped = text.replace("|", "\\|")
578+
delimiter = "`"
579+
while delimiter in escaped:
580+
delimiter += "`"
581+
if escaped.startswith("`") or escaped.endswith("`"):
582+
escaped = f" {escaped} "
583+
return f"{delimiter}{escaped}{delimiter}"
574584

575585

576586
def _rules_cell(scope: ScopeResult) -> str:
@@ -633,7 +643,7 @@ def _markdown_table(results: list[ScopeResult]) -> str:
633643
continue
634644
icon = _STATUS_ICONS[scope.status]
635645
value = _scope_value(scope)
636-
value_display = f"`{_table_cell_escape(value)}`" if value else "\u2014"
646+
value_display = _table_cell_code(value) if value else "\u2014"
637647
rows.append(
638648
f"| {icon} | {scope.label} | {value_display} | {_rules_cell(scope)} |"
639649
)

‎main_test.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,19 @@ def test_pipe_in_a_value_does_not_break_the_table(self):
833833
table = main._markdown_table([scope])
834834
self.assertIn("`feat: a\\|b`", table)
835835

836+
def test_backtick_in_a_value_does_not_end_the_code_span(self):
837+
"""The span delimiter grows past the longest backtick run inside."""
838+
scope = pass_scope("Commit 1/1", value="fix: escape `quoted` text")
839+
table = main._markdown_table([scope])
840+
self.assertIn("``fix: escape `quoted` text``", table)
841+
842+
def test_value_ending_in_a_backtick_gets_padding_spaces(self):
843+
"""CommonMark reads a backtick against the delimiter as part of it;
844+
the padding spaces keep the content and the fence apart."""
845+
scope = pass_scope("Commit 1/1", value="fix: dangling `")
846+
table = main._markdown_table([scope])
847+
self.assertIn("`` fix: dangling ` ``", table)
848+
836849
def test_body_opens_with_hidden_marker(self):
837850
body = main.render_job_summary([pass_scope("Branch")])
838851
self.assertTrue(body.startswith(main.COMMENT_MARKER))

0 commit comments

Comments
 (0)