From a6911436156c15246c69ea66e62e2745e419b813 Mon Sep 17 00:00:00 2001 From: Jeff Dickey <216188+jdx@users.noreply.github.com> Date: Sat, 28 Sep 2024 17:22:01 -0500 Subject: [PATCH] fix: escape html in md --- cli/src/cli/generate/markdown.rs | 3 +++ lib/src/docs/markdown/cmd.rs | 3 +-- lib/src/docs/markdown/renderer.rs | 1 + lib/src/docs/markdown/spec.rs | 2 +- .../docs/markdown/templates/arg_template.md.tera | 4 ++-- .../docs/markdown/templates/cmd_template.md.tera | 6 +++--- .../docs/markdown/templates/flag_template.md.tera | 4 ++-- .../docs/markdown/templates/index_template.md.tera | 13 +------------ .../docs/markdown/templates/spec_template.md.tera | 2 +- lib/src/docs/markdown/tera.rs | 8 ++++++++ lib/src/spec/cmd.rs | 2 +- lib/src/spec/mod.rs | 6 +++--- 12 files changed, 27 insertions(+), 27 deletions(-) diff --git a/cli/src/cli/generate/markdown.rs b/cli/src/cli/generate/markdown.rs index cf4e9f4..7da27a0 100644 --- a/cli/src/cli/generate/markdown.rs +++ b/cli/src/cli/generate/markdown.rs @@ -21,6 +21,9 @@ pub struct Markdown { #[clap(long)] url_prefix: Option, + // /// Escape HTML in markdown + // #[clap(long)] + // html_escape: bool, /// Output markdown files to this directory #[clap(long, value_hint = clap::ValueHint::DirPath)] out_dir: Option, diff --git a/lib/src/docs/markdown/cmd.rs b/lib/src/docs/markdown/cmd.rs index 5f03fff..530ca8a 100644 --- a/lib/src/docs/markdown/cmd.rs +++ b/lib/src/docs/markdown/cmd.rs @@ -23,8 +23,7 @@ mod tests { fn test_render_markdown_cmd() { let ctx = MarkdownRenderer::new(&SPEC_KITCHEN_SINK).with_multi(true); assert_snapshot!(ctx.render_cmd(&SPEC_KITCHEN_SINK.cmd).unwrap(), @r####" - - # `mycli mycli [args] [flags] [subcommand]` + # `mycli [args] [flags] [subcommand]` ## Arguments diff --git a/lib/src/docs/markdown/renderer.rs b/lib/src/docs/markdown/renderer.rs index bd41ccd..854886a 100644 --- a/lib/src/docs/markdown/renderer.rs +++ b/lib/src/docs/markdown/renderer.rs @@ -46,6 +46,7 @@ impl MarkdownRenderer { ctx.insert("multi", &self.multi); ctx.insert("spec", &self.spec); ctx.insert("url_prefix", &self.url_prefix); + ctx.insert("html_escape", &true); ctx } } diff --git a/lib/src/docs/markdown/spec.rs b/lib/src/docs/markdown/spec.rs index 1d270d8..2a2b907 100644 --- a/lib/src/docs/markdown/spec.rs +++ b/lib/src/docs/markdown/spec.rs @@ -69,7 +69,7 @@ mod tests { ## `mycli plugin [subcommand]` - ## `mycli install [args] [flags]` + ## `mycli plugin install [args] [flags]` ### Arguments diff --git a/lib/src/docs/markdown/templates/arg_template.md.tera b/lib/src/docs/markdown/templates/arg_template.md.tera index e163f02..b924865 100644 --- a/lib/src/docs/markdown/templates/arg_template.md.tera +++ b/lib/src/docs/markdown/templates/arg_template.md.tera @@ -2,5 +2,5 @@ {%- if help %} -{{ help }} -{%- endif -%} \ No newline at end of file +{{ help | escape_md }} +{%- endif -%} diff --git a/lib/src/docs/markdown/templates/cmd_template.md.tera b/lib/src/docs/markdown/templates/cmd_template.md.tera index b5edb2d..32d9dae 100644 --- a/lib/src/docs/markdown/templates/cmd_template.md.tera +++ b/lib/src/docs/markdown/templates/cmd_template.md.tera @@ -6,12 +6,12 @@ {%- endif %} {%- if before_help %} -{{ before_help }} +{{ before_help | escape_md }} {%- endif %} {%- if help %} -{{ help }} +{{ help | escape_md }} {%- endif %} {%- set args = cmd.args | filter(attribute="hide", value=false) %} @@ -66,5 +66,5 @@ {%- if after_help %} -{{ after_help }} +{{ after_help | escape_md }} {%- endif -%} diff --git a/lib/src/docs/markdown/templates/flag_template.md.tera b/lib/src/docs/markdown/templates/flag_template.md.tera index 3a38229..4c8de0a 100644 --- a/lib/src/docs/markdown/templates/flag_template.md.tera +++ b/lib/src/docs/markdown/templates/flag_template.md.tera @@ -1,5 +1,5 @@ {%- if flag.help_long_md %}{% set help = flag.long_help %}{% elif flag.help_long %}{% set help = flag.help_long %}{% else %}{% set help = flag.help %}{% endif %} {%- if help %} -{{ help }} -{%- endif -%} \ No newline at end of file +{{ help | escape_md }} +{%- endif -%} diff --git a/lib/src/docs/markdown/templates/index_template.md.tera b/lib/src/docs/markdown/templates/index_template.md.tera index 9eac110..d39a1c7 100644 --- a/lib/src/docs/markdown/templates/index_template.md.tera +++ b/lib/src/docs/markdown/templates/index_template.md.tera @@ -5,11 +5,6 @@ {%- if spec.version %} * **version**: {{ spec.version }}{% endif %} -{%- if about %} - -{{ about }} -{% endif %} - {%- include "cmd_template.md.tera" %} {%- set header_level = header_level + 1 %} @@ -19,11 +14,5 @@ {{ "#" | repeat(count=header_level) }} Subcommands {% endif %} -{%- if cmd.full_cmd | length >= 2 %} -{%- set full_cmd = cmd.full_cmd | slice(end=1) | join(sep=" ") %} -{%- set full_cmd = spec.bin ~ " " ~ full_cmd %} -{%- else %} -{%- set full_cmd = spec.bin %} -{%- endif %} -* [`{{ full_cmd }} {{ cmd.usage }}`]({{ url_prefix }}/{{ cmd.full_cmd | join(sep="/") }}.md) +* [`{{ spec.bin }} {{ cmd.usage }}`]({{ url_prefix }}/{{ cmd.full_cmd | join(sep="/") }}.md) {%- endfor -%} diff --git a/lib/src/docs/markdown/templates/spec_template.md.tera b/lib/src/docs/markdown/templates/spec_template.md.tera index 41cd4e9..49be3a5 100644 --- a/lib/src/docs/markdown/templates/spec_template.md.tera +++ b/lib/src/docs/markdown/templates/spec_template.md.tera @@ -1,6 +1,6 @@ {%- set about = spec.about_long | default(value=spec.about) %} {%- set cmd = spec.cmd %} -{{- "#" | repeat(count=header_level) }} `{{ spec.cmd.usage }}` +{{- "#" | repeat(count=header_level) }} `{{ spec.bin }} {{ spec.cmd.usage }}` {%- if spec.version %} * **version**: {{ spec.version }}{% endif %} diff --git a/lib/src/docs/markdown/tera.rs b/lib/src/docs/markdown/tera.rs index 6c56227..5b62cfe 100644 --- a/lib/src/docs/markdown/tera.rs +++ b/lib/src/docs/markdown/tera.rs @@ -23,5 +23,13 @@ pub(crate) static TERA: Lazy = Lazy::new(|| { }, ); + tera.register_filter( + "escape_md", + move |value: &tera::Value, _: &HashMap| { + let value = value.as_str().unwrap(); + Ok(value.replace("<", "<").replace(">", ">").into()) + }, + ); + tera }); diff --git a/lib/src/spec/cmd.rs b/lib/src/spec/cmd.rs index ab68b88..56ac3d0 100644 --- a/lib/src/spec/cmd.rs +++ b/lib/src/spec/cmd.rs @@ -207,7 +207,7 @@ impl SpecCommand { && self.subcommands.is_empty() } pub fn usage(&self) -> String { - let mut usage = self.name.clone(); + let mut usage = self.full_cmd.join(" "); let total_count = self.args.len() + self.flags.len(); if self.subcommands.is_empty() && total_count <= 2 { let inlines = self diff --git a/lib/src/spec/mod.rs b/lib/src/spec/mod.rs index 599243e..5babd66 100644 --- a/lib/src/spec/mod.rs +++ b/lib/src/spec/mod.rs @@ -201,9 +201,6 @@ fn extract_usage_from_comments(full: &str) -> String { } fn set_subcommand_ancestors(cmd: &mut SpecCommand, ancestors: &[String]) { - if cmd.usage.is_empty() { - cmd.usage = cmd.usage(); - } let ancestors = ancestors.to_vec(); for subcmd in cmd.subcommands.values_mut() { subcmd.full_cmd = ancestors @@ -213,6 +210,9 @@ fn set_subcommand_ancestors(cmd: &mut SpecCommand, ancestors: &[String]) { .collect(); set_subcommand_ancestors(subcmd, &subcmd.full_cmd.clone()); } + if cmd.usage.is_empty() { + cmd.usage = cmd.usage(); + } } impl Display for Spec {