From a760e75f2f1a73cde4f39bae6930c7595e1c6df1 Mon Sep 17 00:00:00 2001 From: Eric Soler Date: Sat, 3 Oct 2026 05:28:06 +0000 Subject: [PATCH] Preserve backticks in Markdown command names --- src/export/markdown.rs | 30 +++++++++++++++++++++++++++++- tests/integration_tests.rs | 12 ++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/export/markdown.rs b/src/export/markdown.rs index 6333c51..e9e4875 100644 --- a/src/export/markdown.rs +++ b/src/export/markdown.rs @@ -24,7 +24,19 @@ impl MarkupExporter for MarkdownExporter { } fn command(&self, cmd: &str) -> String { - format!("`{cmd}`") + let longest_backtick_run = cmd + .split(|character| character != '`') + .map(str::len) + .max() + .unwrap_or(0); + + if longest_backtick_run == 0 { + format!("`{cmd}`") + } else { + // Use a longer delimiter and keep command backticks separate from it. + let delimiter = "`".repeat(longest_backtick_run + 1); + format!("{delimiter} {cmd} {delimiter}") + } } } @@ -44,3 +56,19 @@ fn test_markdown_formatter_table_divider() { let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]); assert_eq!(divider, "|:---|---:|:---|\n"); } + +#[test] +fn test_markdown_formatter_command_with_backticks() { + let formatter = MarkdownExporter::default(); + + for (command, expected) in [ + ("echo `uname`", "`` echo `uname` ``"), + ("echo ``quoted``", "``` echo ``quoted`` ```"), + ("`` `quoted` ``", "``` `` `quoted` `` ```"), + ("`", "`` ` ``"), + ("echo ```", "```` echo ``` ````"), + (" echo `uname` ", "`` echo `uname` ``"), + ] { + assert_eq!(formatter.command(command), expected, "{command:?}"); + } +} diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 7d1df69..40f3766 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -929,6 +929,18 @@ fn invalid_command_options_preserve_export_files() { } } +#[test] +fn markdown_export_preserves_backticks_and_pipes_in_command_names() { + hyperfine_debug() + .arg("--style=none") + .arg("--export-markdown=-") + .arg("--command-name=echo `uname` | cat") + .arg("sleep 1") + .assert() + .success() + .stdout(predicate::str::contains("| `` echo `uname` \\| cat `` |")); +} + #[test] fn unused_parameters_are_shown_in_benchmark_name() { hyperfine()