Skip to content

Commit 71bf71e

Browse files
committed
refactor: remove yaml_escape, use metadata_to_yaml everywhere
1 parent 38305da commit 71bf71e

3 files changed

Lines changed: 10 additions & 37 deletions

File tree

src/builder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,11 @@ impl RecipeImporterBuilder {
364364
if has_name || has_metadata {
365365
output.push_str("---\n");
366366
if has_name {
367-
output.push_str(&format!(
368-
"title: {}\n",
369-
crate::pipelines::yaml_escape(&components.name)
370-
));
367+
let title_yaml = crate::pipelines::metadata_to_yaml(&[(
368+
"title".to_string(),
369+
components.name.clone(),
370+
)]);
371+
output.push_str(&title_yaml);
371372
}
372373
if has_metadata {
373374
output.push_str(&components.metadata);

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ pub async fn text_to_cooklang(components: &RecipeComponents) -> Result<String, I
161161
if has_name || has_metadata {
162162
let mut frontmatter = String::from("---\n");
163163
if has_name {
164-
frontmatter.push_str(&format!(
165-
"title: {}\n",
166-
crate::pipelines::yaml_escape(&components.name)
167-
));
164+
let title_yaml = crate::pipelines::metadata_to_yaml(&[(
165+
"title".to_string(),
166+
components.name.clone(),
167+
)]);
168+
frontmatter.push_str(&title_yaml);
168169
}
169170
if has_metadata {
170171
frontmatter.push_str(&components.metadata);

src/pipelines/mod.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ pub fn sanitize_name(name: &str) -> String {
1919
name.split_whitespace().collect::<Vec<_>>().join(" ")
2020
}
2121

22-
/// Serialize a YAML scalar value using serde_yaml.
23-
pub fn yaml_escape(value: &str) -> String {
24-
let yaml = serde_yaml::to_string(&value).unwrap_or_else(|_| value.to_string());
25-
yaml.trim_end().to_string()
26-
}
27-
2822
/// Build a YAML metadata string from a Recipe's fields.
2923
/// Handles nested values (e.g. nutrition) by parsing pre-formatted YAML blocks.
3024
pub fn metadata_to_yaml(entries: &[(String, String)]) -> String {
@@ -57,29 +51,6 @@ pub fn metadata_to_yaml(entries: &[(String, String)]) -> String {
5751
mod tests {
5852
use super::*;
5953

60-
#[test]
61-
fn test_yaml_escape_plain_value() {
62-
assert_eq!(yaml_escape("hello world"), "hello world");
63-
}
64-
65-
#[test]
66-
fn test_yaml_escape_colon() {
67-
assert_eq!(yaml_escape("test : sub"), "'test : sub'");
68-
}
69-
70-
#[test]
71-
fn test_yaml_escape_url() {
72-
assert_eq!(
73-
yaml_escape("http://example.com/recipe"),
74-
"http://example.com/recipe"
75-
);
76-
}
77-
78-
#[test]
79-
fn test_yaml_escape_hash() {
80-
assert_eq!(yaml_escape("value # comment"), "'value # comment'");
81-
}
82-
8354
#[test]
8455
fn test_metadata_to_yaml_simple() {
8556
let entries = vec![

0 commit comments

Comments
 (0)