From 4424481487b4ba52cff579ee52d5c463690b8d73 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Mon, 3 Feb 2025 13:32:57 +0100 Subject: [PATCH] Supply `metadata.content_type` for `to html` output (#14990) # Description Adds pipeline metadata to the `to html` command output (hardcoded to `text/html; charset=utf-8`) # User-Facing Changes Pipeline metadata is now included with the `to html` command output. --- Cargo.lock | 1 + crates/nu-cmd-extra/Cargo.toml | 3 ++- crates/nu-cmd-extra/src/extra/formats/to/html.rs | 7 ++++++- crates/nu-command/tests/format_conversions/html.rs | 9 +++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4dcc7ce5fe..a3d91219e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3542,6 +3542,7 @@ dependencies = [ "fancy-regex", "heck", "itertools 0.13.0", + "mime", "nu-ansi-term", "nu-cmd-base", "nu-cmd-lang", diff --git a/crates/nu-cmd-extra/Cargo.toml b/crates/nu-cmd-extra/Cargo.toml index ec6cfae4e3..f6a6c83311 100644 --- a/crates/nu-cmd-extra/Cargo.toml +++ b/crates/nu-cmd-extra/Cargo.toml @@ -34,8 +34,9 @@ serde = { workspace = true } serde_urlencoded = { workspace = true } v_htmlescape = { workspace = true } itertools = { workspace = true } +mime = { workspace = true } [dev-dependencies] nu-cmd-lang = { path = "../nu-cmd-lang", version = "0.101.1" } nu-command = { path = "../nu-command", version = "0.101.1" } -nu-test-support = { path = "../nu-test-support", version = "0.101.1" } \ No newline at end of file +nu-test-support = { path = "../nu-test-support", version = "0.101.1" } diff --git a/crates/nu-cmd-extra/src/extra/formats/to/html.rs b/crates/nu-cmd-extra/src/extra/formats/to/html.rs index b047b386f1..81a5309ae5 100644 --- a/crates/nu-cmd-extra/src/extra/formats/to/html.rs +++ b/crates/nu-cmd-extra/src/extra/formats/to/html.rs @@ -330,7 +330,12 @@ fn to_html( output_string = run_regexes(®ex_hm, &output_string); } - Ok(Value::string(output_string, head).into_pipeline_data()) + let metadata = PipelineMetadata { + data_source: nu_protocol::DataSource::None, + content_type: Some(mime::TEXT_HTML_UTF_8.to_string()), + }; + + Ok(Value::string(output_string, head).into_pipeline_data_with_metadata(metadata)) } fn theme_demo(span: Span) -> PipelineData { diff --git a/crates/nu-command/tests/format_conversions/html.rs b/crates/nu-command/tests/format_conversions/html.rs index 9f6644a812..27b9660fd4 100644 --- a/crates/nu-command/tests/format_conversions/html.rs +++ b/crates/nu-command/tests/format_conversions/html.rs @@ -14,6 +14,15 @@ fn out_html_simple() { ); } +#[test] +fn out_html_metadata() { + let actual = nu!(r#" + echo 3 | to html | metadata | get content_type + "#); + + assert_eq!(actual.out, r#"text/html; charset=utf-8"#); +} + #[test] fn out_html_partial() { let actual = nu!(r#"