Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Fix escaped pipe characters in markdown table help output (#368)
Maximilian Roos00f0f4bfe338
diff --git a/docs/content/hook.md b/docs/content/hook.mdindex 25b460be7..ee69b4c83 100644--- a/docs/content/hook.md+++ b/docs/content/hook.md@@ -159,8 +159,8 @@ Templates support Jinja2 filters for transforming values:| Filter | Example | Description ||--------|---------|-------------|-| `sanitize` | `{{ branch \| sanitize }}` → feature-foo | Replace `/` and `\` with `-` |-| `hash_port` | `{{ branch \| hash_port }}` → 12472 | Hash string to port (10000-19999) |+| `sanitize` | `{{ branch \| sanitize }}` | Replace `/` and `\` with `-` |+| `hash_port` | `{{ branch \| hash_port }}` | Hash to port 10000-19999 |The `sanitize` filter makes branch names safe for filesystem paths. The `hash_port` filter is useful for running dev servers on unique ports per worktree:diff --git a/src/cli.rs b/src/cli.rsindex 55a240a20..ae37eb3a0 100644--- a/src/cli.rs+++ b/src/cli.rs@@ -1629,8 +1629,8 @@ Templates support Jinja2 filters for transforming values:| Filter | Example | Description ||--------|---------|-------------|-| `sanitize` | `{{ branch \| sanitize }}` → feature-foo | Replace `/` and `\` with `-` |-| `hash_port` | `{{ branch \| hash_port }}` → 12472 | Hash string to port (10000-19999) |+| `sanitize` | `{{ branch \| sanitize }}` | Replace `/` and `\` with `-` |+| `hash_port` | `{{ branch \| hash_port }}` | Hash to port 10000-19999 |The `sanitize` filter makes branch names safe for filesystem paths. The `hash_port` filter is useful for running dev servers on unique ports per worktree:diff --git a/src/md_help.rs b/src/md_help.rsindex d0165a47c..b6e0626f8 100644--- a/src/md_help.rs+++ b/src/md_help.rs@@ -143,10 +143,11 @@ fn render_table_with_termimad(lines: &[&str], indent: &str, max_width: Option<usreturn String::new();}- // Preprocess lines to strip markdown links (termimad doesn't handle them)+ // Preprocess lines to strip markdown links and unescape pipes+ // (termimad doesn't handle either)let processed: Vec<String> = lines.iter()- .map(|line| strip_markdown_links(line))+ .map(|line| unescape_table_pipes(&strip_markdown_links(line))).collect();let markdown = processed.join("\n");@@ -171,6 +172,15 @@ fn render_table_with_termimad(lines: &[&str], indent: &str, max_width: Option<us}}+/// Unescape pipe characters in markdown table cells: `\|` -> `|`+///+/// In markdown tables, `|` is the column delimiter. To include a literal pipe+/// character inside a cell, you escape it as `\|`. Termimad doesn't handle this+/// escape sequence, so we preprocess it.+fn unescape_table_pipes(line: &str) -> String {+ line.replace(r"\|", "|")+}+/// Strip markdown links, keeping only the link text: `[text](url)` -> `text`////// Limitation: Links in clap help text may be broken across lines by clap's wrapping@@ -412,14 +422,29 @@ mod tests {);}+ #[test]+ fn test_unescape_table_pipes() {+ // Basic conversion+ assert_eq!(unescape_table_pipes(r"a \| b"), "a | b");+ // Multiple escapes+ assert_eq!(+ unescape_table_pipes(r"\| start \| end \|"),+ "| start | end |"+ );+ // No escapes - unchanged+ assert_eq!(unescape_table_pipes("no pipes here"), "no pipes here");+ // Regular pipe - unchanged+ assert_eq!(unescape_table_pipes("a | b"), "a | b");+ }+#[test]fn test_render_table_escaped_pipe() {// In markdown tables, \| represents a literal pipe character- // Note: termimad keeps \| as-is rather than converting to |+ // We preprocess to convert \| to | before sending to termimadlet lines = vec!["| Category | Symbol | Meaning |","| --- | --- | --- |",- "| Remote | `\\|` | In sync |",+ r"| Remote | `\|` | In sync |",];let result = render_table(&lines, None);// Table should render with the content@@ -431,6 +456,9 @@ mod tests {result.contains("In sync"),"Table should contain cell content");+ // The escaped pipe should be converted to a literal pipe+ assert!(result.contains('|'));+ assert!(!result.contains(r"\|"));}// ============================================================================diff --git a/tests/snapshots/integration__integration_tests__help__help_list_long.snap b/tests/snapshots/integration__integration_tests__help__help_list_long.snapindex 55c1ac345..8f4918833 100644--- a/tests/snapshots/integration__integration_tests__help__help_list_long.snap+++ b/tests/snapshots/integration__integration_tests__help__help_list_long.snap@@ -12,6 +12,7 @@ info:RUST_LOG: warnSOURCE_DATE_EPOCH: "1735776000"WORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"---success: trueexit_code: 0@@ -144,7 +145,7 @@ The Status column has multiple subcolumns. Within each, only the first matching↕ Diverged from the default branch↑ Ahead of the default branch↓ Behind the default branch- Remote \| In sync with remote+ Remote | In sync with remote⇅ Diverged from remote⇡ Ahead of remote⇣ Behind remotediff --git a/tests/snapshots/integration__integration_tests__help__help_list_narrow_80.snap b/tests/snapshots/integration__integration_tests__help__help_list_narrow_80.snapindex 98e890a6c..0adb66f7d 100644--- a/tests/snapshots/integration__integration_tests__help__help_list_narrow_80.snap+++ b/tests/snapshots/integration__integration_tests__help__help_list_narrow_80.snap@@ -12,6 +12,7 @@ info:RUST_LOG: warnSOURCE_DATE_EPOCH: "1735776000"WORKTRUNK_CONFIG_PATH: /nonexistent/test/config.toml+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"---success: trueexit_code: 0@@ -153,7 +154,7 @@ branch or target |↕ Diverged from the default branch↑ Ahead of the default branch↓ Behind the default branch- Remote \| In sync with remote+ Remote | In sync with remote⇅ Diverged from remote⇡ Ahead of remote⇣ Behind remote