Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

refactor(help): unify --help-page plain/web paths; link-check .txt (#2412)

Maximilian Rooscadf392b4e1c
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index d020070fe..e00a5b71c 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -33,7 +33,7 @@ repos:
# Check links using .config/lychee.toml exclusions
# Uses lychee-system (requires `cargo install lychee`) to avoid /bin/bash dependency on Windows
- id: lychee-system
- types: [markdown]
+ files: '\.(md|txt)$'
args:
- --config=.config/lychee.toml
- repo: local
diff --git a/src/help.rs b/src/help.rs
index 4545f6ec8..7f44c8175 100644
--- a/src/help.rs
+++ b/src/help.rs
@@ -55,6 +55,90 @@ use worktrunk::styling::{eprintln, println};
use crate::cli;
+/// Empty span; the displayed "EXPERIMENTAL" text comes from the CSS `::after`
+/// rule so the anchor-link slug generated by Zola ignores it.
+const BADGE_EXPERIMENTAL_HTML: &str = "<span class=\"badge-experimental\"></span>";
+
+/// Output format for a `--help-page`. Web pages keep Zola shortcodes, HTML
+/// spans, and demo GIF figures for the docs site; plain pages strip those for
+/// skill reference files that need portable markdown.
+#[derive(Clone, Copy)]
+enum PageMode {
+ Web,
+ Plain,
+}
+
+impl PageMode {
+ /// ANSI color for the embedded `--help` reference block. Web keeps ANSI so
+ /// it can be converted to HTML downstream; plain strips it.
+ fn color(self) -> ColorChoice {
+ match self {
+ Self::Web => ColorChoice::Always,
+ Self::Plain => ColorChoice::Never,
+ }
+ }
+
+ /// Transform freshly-combined raw help into mode-appropriate markdown. Web
+ /// rewrites `$`-prefixed console blocks into `{% terminal() %}`
+ /// shortcodes; both modes relabel plain ``` ```console ``` fences as
+ /// ``` ```bash ``` for syntax highlighting.
+ fn transform_raw(self, text: String) -> String {
+ let text = match self {
+ Self::Web => convert_dollar_console_to_terminal(&text),
+ Self::Plain => text,
+ };
+ text.replace("```console\n", "```bash\n")
+ }
+
+ /// Transform the body (everything before subdoc placeholders). Web expands
+ /// demo GIFs into `<picture>` figures and converts CLI markers into HTML
+ /// spans; plain strips demo placeholders so they don't leak as comments.
+ fn process_body(self, text: &str) -> String {
+ match self {
+ Self::Web => post_process_for_html(&expand_demo_placeholders(text)),
+ Self::Plain => strip_demo_placeholders(text),
+ }
+ }
+
+ /// Transform subdoc trailing content (e.g. an `Aliases` section after the
+ /// last subdoc marker). Web post-processes CLI markers; plain passes
+ /// through unchanged — each subdoc's body is processed during expansion.
+ fn process_subdoc_trailing(self, text: &str) -> String {
+ match self {
+ Self::Web => post_process_for_html(text),
+ Self::Plain => text.to_string(),
+ }
+ }
+
+ /// Rendering for the `[experimental]` badge.
+ fn experimental_marker(self) -> &'static str {
+ match self {
+ Self::Web => BADGE_EXPERIMENTAL_HTML,
+ Self::Plain => "[experimental]",
+ }
+ }
+
+ /// Emit the page header — an auto-generated comment for web (sync test
+ /// uses it as a region marker), or an H1 title for plain skill pages.
+ fn emit_header(self, subcommand: &str) {
+ match self {
+ Self::Web => std::println!(
+ "<!-- ⚠️ AUTO-GENERATED from `wt {subcommand} --help-page` — edit cli.rs to update -->"
+ ),
+ Self::Plain => std::println!("# wt {subcommand}"),
+ }
+ std::println!();
+ }
+
+ /// Emit the closing END marker (web only).
+ fn emit_footer(self, subcommand: &str) {
+ if matches!(self, Self::Web) {
+ std::println!();
+ std::println!("<!-- END AUTO-GENERATED from `wt {subcommand} --help-page` -->");
+ }
+ }
+}
+
/// Custom help handling for pager support and markdown rendering.
///
/// We intercept help requests to provide:
@@ -83,8 +167,12 @@ pub fn maybe_handle_help_with_pager(alias_help_context: Option<crate::commands::
// Check for --help-page flag (output full doc page with frontmatter)
if args.iter().any(|a| a == "--help-page") {
- let plain = args.iter().any(|a| a == "--plain");
- handle_help_page(&args, plain);
+ let mode = if args.iter().any(|a| a == "--plain") {
+ PageMode::Plain
+ } else {
+ PageMode::Web
+ };
+ handle_help_page(&args, mode);
process::exit(0);
}
@@ -364,7 +452,7 @@ fn handle_help_description(args: &[String]) {
/// ```
///
/// This is used to generate docs/content/merge.md etc from the source.
-fn handle_help_page(args: &[String], plain: bool) {
+fn handle_help_page(args: &[String], mode: PageMode) {
let mut cmd = cli::build_command();
cmd = crate::completion::inject_hook_subcommands(cmd);
cmd = cmd.color(ColorChoice::Never);
@@ -393,61 +481,20 @@ Commands with pages: merge, switch, remove, list"
return;
};
- // Get combined docs: about + subtitle + after_long_help
- // Transform for web docs: $→terminal shortcode, console→bash, status colors, demo images
- // Subdocs are expanded separately so main Command reference comes first
let parent_name = format!("wt {}", subcommand);
- let raw_help = combine_command_docs(sub);
- // Web mode: convert $ code blocks to Zola terminal shortcodes
- // Plain mode: skip shortcode conversion (skills consume plain markdown)
- let raw_help = if plain {
- raw_help
- } else {
- convert_dollar_console_to_terminal(&raw_help)
- };
- let raw_help = raw_help.replace("```console\n", "```bash\n");
+ let raw_help = mode.transform_raw(combine_command_docs(sub));
- // Split content at first subdoc placeholder
- let subdoc_marker = "<!-- subdoc:";
- let (main_content, subdoc_content) = if let Some(pos) = raw_help.find(subdoc_marker) {
- (&raw_help[..pos], Some(&raw_help[pos..]))
- } else {
- (raw_help.as_str(), None)
+ // Subdocs are expanded separately so the main command reference comes first.
+ let (main_content, subdoc_content) = match raw_help.find("<!-- subdoc:") {
+ Some(pos) => (&raw_help[..pos], Some(&raw_help[pos..])),
+ None => (raw_help.as_str(), None),
};
- // Process main content (before subdocs)
- // Web mode: expand demo GIFs and convert CLI markers to HTML
- // Plain mode: skip HTML transforms (skills consume plain markdown)
- let main_help = if plain {
- strip_demo_placeholders(main_content)
- } else {
- let text = expand_demo_placeholders(main_content);
- post_process_for_html(&text)
- };
-
- // Get the help reference block (wrap at 100 chars, with colors for HTML in web mode)
- let reference_block = help_reference_with_color(
- &[subcommand],
- Some(100),
- if plain {
- ColorChoice::Never
- } else {
- ColorChoice::Always
- },
- );
+ let main_help = mode.process_body(main_content);
+ let reference_block = help_reference_with_color(&[subcommand], Some(100), mode.color());
// Use std::println! to preserve ANSI codes in output (the styling::println strips them)
- if plain {
- // Plain mode: H1 title, no AUTO-GENERATED markers
- std::println!("# wt {subcommand}");
- std::println!();
- } else {
- // Web mode: region markers for sync replacement (frontmatter is in skeleton files)
- std::println!(
- "<!-- ⚠️ AUTO-GENERATED from `wt {subcommand} --help-page` — edit cli.rs to update -->"
- );
- std::println!();
- }
+ mode.emit_header(subcommand);
std::println!("{}", main_help.trim());
std::println!();
@@ -461,26 +508,18 @@ Commands with pages: merge, switch, remove, list"
// Subdocs follow, each with their own command reference at the end.
if let Some(subdocs) = subdoc_content {
- let subdocs = if plain {
- subdocs.to_string()
- } else {
- // Apply post-processing to non-marker text (e.g., the Aliases section after
- // the last subdoc marker). Must happen before expansion — after expansion,
- // post_process_for_html has already run on each subcommand section internally
- // (in format_subcommand_section), so re-running it would double-convert.
- post_process_for_html(subdocs)
- };
- let subdocs_expanded = expand_subdoc_placeholders(&subdocs, sub, &parent_name, plain);
+ // Process trailing text (e.g., an Aliases section after the last subdoc
+ // marker) before expansion — each subdoc's own body is post-processed
+ // inside format_subcommand_section, so re-running would double-convert.
+ let subdocs = mode.process_subdoc_trailing(subdocs);
+ let subdocs_expanded = expand_subdoc_placeholders(&subdocs, sub, &parent_name, mode);
std::println!();
std::println!("# Subcommands");
std::println!();
std::println!("{}", subdocs_expanded.trim());
}
- if !plain {
- std::println!();
- std::println!("<!-- END AUTO-GENERATED from `wt {subcommand} --help-page` -->");
- }
+ mode.emit_footer(subcommand);
}
/// Post-process CLI help content for web docs rendering.
@@ -513,12 +552,7 @@ fn post_process_for_html(text: &str) -> String {
.replace("`●` yellow", "<span style='color:#a60'>●</span> yellow")
.replace("`⚠` yellow", "<span style='color:#a60'>⚠</span> yellow")
.replace("`●` gray", "<span style='color:#888'>●</span> gray")
- // Experimental badges — empty span, text added via CSS ::after.
- // Empty so the span doesn't affect Zola's heading slug generation.
- .replace(
- "[experimental]",
- "<span class=\"badge-experimental\"></span>",
- )
+ .replace("[experimental]", BADGE_EXPERIMENTAL_HTML)
// Convert plain URL references to markdown links for web docs
// CLI shows: "Open an issue at https://github.com/max-sixty/worktrunk."
// Web shows: "[Open an issue](https://github.com/max-sixty/worktrunk/issues)."
@@ -648,7 +682,7 @@ fn expand_subdoc_placeholders(
text: &str,
parent_cmd: &clap::Command,
parent_name: &str,
- plain: bool,
+ mode: PageMode,
) -> String {
const PREFIX: &str = "<!-- subdoc: ";
const SUFFIX: &str = " -->";
@@ -665,7 +699,7 @@ fn expand_subdoc_placeholders(
.get_subcommands()
.find(|s| s.get_name() == subcommand_name)
{
- format_subcommand_section(sub, parent_name, subcommand_name, plain)
+ format_subcommand_section(sub, parent_name, subcommand_name, mode)
} else {
format!(
"<!-- subdoc error: subcommand '{}' not found -->",
@@ -713,47 +747,29 @@ fn format_subcommand_section(
sub: &clap::Command,
parent_name: &str,
subcommand_name: &str,
- plain: bool,
+ mode: PageMode,
) -> String {
// parent_name is "wt config", subcommand_name is "create"
// full_command is "wt config create"
let full_command = format!("{} {}", parent_name, subcommand_name);
- // Get combined docs: about + subtitle + after_long_help
- let raw_help = combine_command_docs(sub);
- let raw_help = if plain {
- raw_help
- } else {
- convert_dollar_console_to_terminal(&raw_help)
- };
- let raw_help = raw_help.replace("```console\n", "```bash\n");
+ let raw_help = mode.transform_raw(combine_command_docs(sub));
// Extract [experimental] marker from content start → badge after heading.
// Web mode: placed after heading as HTML badge so Zola's anchor link doesn't wrap it.
// Plain mode: kept as [experimental] text.
- let (has_experimental, raw_help) = if let Some(rest) = raw_help.strip_prefix("[experimental] ")
- {
- (true, rest.to_string())
- } else {
- (false, raw_help)
+ let (has_experimental, raw_help) = match raw_help.strip_prefix("[experimental] ") {
+ Some(rest) => (true, rest.to_string()),
+ None => (false, raw_help),
};
// Split content at first subdoc placeholder so command reference comes before nested subdocs
- let subdoc_marker = "<!-- subdoc:";
- let (main_content, subdoc_content) = if let Some(pos) = raw_help.find(subdoc_marker) {
- (&raw_help[..pos], Some(&raw_help[pos..]))
- } else {
- (raw_help.as_str(), None)
+ let (main_content, subdoc_content) = match raw_help.find("<!-- subdoc:") {
+ Some(pos) => (&raw_help[..pos], Some(&raw_help[pos..])),
+ None => (raw_help.as_str(), None),
};
- // Process main content (before any nested subdocs)
- let main_help = if plain {
- let text = increase_heading_levels(main_content);
- strip_demo_placeholders(&text)
- } else {
- let text = increase_heading_levels(main_content);
- post_process_for_html(&text)
- };
+ let main_help = mode.process_body(&increase_heading_levels(main_content));
// Build command path from parent_name: "wt config" -> ["config", "create"]
let command_path: Vec<&str> = parent_name
@@ -763,25 +779,13 @@ fn format_subcommand_section(
.chain(std::iter::once(subcommand_name))
.collect();
- // Get help reference with colors for web HTML conversion, plain text for skills
- let reference_block = help_reference_with_color(
- &command_path,
- Some(100),
- if plain {
- ColorChoice::Never
- } else {
- ColorChoice::Always
- },
- );
+ let reference_block = help_reference_with_color(&command_path, Some(100), mode.color());
// Format the section: heading, badge (outside heading), main content, command reference
let mut section = format!("## {full_command}\n\n");
if has_experimental {
- if plain {
- section.push_str("[experimental]\n\n");
- } else {
- section.push_str("<span class=\"badge-experimental\"></span>\n\n");
- }
+ section.push_str(mode.experimental_marker());
+ section.push_str("\n\n");
}
if !main_help.is_empty() {
@@ -796,12 +800,8 @@ fn format_subcommand_section(
// Expand nested subdocs after the command reference.
if let Some(subdocs) = subdoc_content {
- let subdocs = if plain {
- subdocs.to_string()
- } else {
- post_process_for_html(subdocs)
- };
- let subdocs_expanded = expand_subdoc_placeholders(&subdocs, sub, &full_command, plain);
+ let subdocs = mode.process_subdoc_trailing(subdocs);
+ let subdocs_expanded = expand_subdoc_placeholders(&subdocs, sub, &full_command, mode);
section.push('\n');
section.push_str(subdocs_expanded.trim());
section.push('\n');