Skip to content

Commit

Permalink
fix: escape html in md
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Sep 28, 2024
1 parent 3cb7769 commit a691143
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 27 deletions.
3 changes: 3 additions & 0 deletions cli/src/cli/generate/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub struct Markdown {
#[clap(long)]
url_prefix: Option<String>,

// /// 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<PathBuf>,
Expand Down
3 changes: 1 addition & 2 deletions lib/src/docs/markdown/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/src/docs/markdown/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion lib/src/docs/markdown/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mod tests {
## `mycli plugin [subcommand]`
## `mycli install [args] [flags]`
## `mycli plugin install [args] [flags]`
### Arguments
Expand Down
4 changes: 2 additions & 2 deletions lib/src/docs/markdown/templates/arg_template.md.tera
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

{%- if help %}

{{ help }}
{%- endif -%}
{{ help | escape_md }}
{%- endif -%}
6 changes: 3 additions & 3 deletions lib/src/docs/markdown/templates/cmd_template.md.tera
Original file line number Diff line number Diff line change
Expand Up @@ -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) %}
Expand Down Expand Up @@ -66,5 +66,5 @@

{%- if after_help %}

{{ after_help }}
{{ after_help | escape_md }}
{%- endif -%}
4 changes: 2 additions & 2 deletions lib/src/docs/markdown/templates/flag_template.md.tera
Original file line number Diff line number Diff line change
@@ -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 -%}
{{ help | escape_md }}
{%- endif -%}
13 changes: 1 addition & 12 deletions lib/src/docs/markdown/templates/index_template.md.tera
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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 -%}
2 changes: 1 addition & 1 deletion lib/src/docs/markdown/templates/spec_template.md.tera
Original file line number Diff line number Diff line change
@@ -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 %}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/docs/markdown/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ pub(crate) static TERA: Lazy<Tera> = Lazy::new(|| {
},
);

tera.register_filter(
"escape_md",
move |value: &tera::Value, _: &HashMap<String, tera::Value>| {
let value = value.as_str().unwrap();
Ok(value.replace("<", "&lt;").replace(">", "&gt;").into())
},
);

tera
});
2 changes: 1 addition & 1 deletion lib/src/spec/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit a691143

Please sign in to comment.