Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ pub fn concat_files(files: &[ProcessedFile], config: &YekConfig) -> anyhow::Resu
.then_with(|| a.rel_path.cmp(&b.rel_path))
});

let unescaped_template = config
.output_template
.as_ref()
.map(|t| t.replace("\\\\\n", "\n").replace("\\\\n", "\n"));

let mut files_to_include = Vec::new();
for file in sorted_files {
let content_size = if config.token_mode {
Expand All @@ -198,15 +203,11 @@ pub fn concat_files(files: &[ProcessedFile], config: &YekConfig) -> anyhow::Resu
}))
.map_err(|e| anyhow!("Failed to serialize JSON: {}", e))?
} else {
config
.output_template
unescaped_template
.as_ref()
.expect("output_template should be set")
.replace("FILE_PATH", &file.rel_path)
.replace("FILE_CONTENT", &content)
// Handle both literal "\n" and escaped "\\n"
.replace("\\\\\n", "\n") // First handle escaped newline
.replace("\\\\n", "\n") // Then handle escaped \n sequence
};
count_tokens(&formatted)
} else {
Expand Down Expand Up @@ -238,19 +239,16 @@ pub fn concat_files(files: &[ProcessedFile], config: &YekConfig) -> anyhow::Resu
)?
} else {
// Use the user-defined template
let template = unescaped_template
.as_ref()
.expect("output_template should be set");
files_to_include
.iter()
.map(|f| {
let content = format_content_with_line_numbers(&f.content, config.line_numbers);
config
.output_template
.as_ref()
.expect("output_template should be set")
template
.replace("FILE_PATH", &f.rel_path)
.replace("FILE_CONTENT", &content)
// Handle both literal "\n" and escaped "\\n"
.replace("\\\\\n", "\n") // First handle escaped newline
.replace("\\\\n", "\n") // Then handle escaped \n sequence
})
.collect::<Vec<_>>()
.join("\n")
Expand Down
31 changes: 31 additions & 0 deletions tests/lib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,37 @@ mod lib_tests {
assert!(output_custom.contains("==README.md==\n---\n# Yek\n===="));
}

#[test]
fn test_concat_files_preserves_literal_newlines_in_content() {
init_tracing();
let temp_dir = tempdir().unwrap();
let config = create_test_config(vec![temp_dir.path().to_string_lossy().to_string()]);

// File containing literal "\n", "\\n", "\r\n", and regex patterns in strings
let content = r#"def escape(text):
return (
text.replace("\\", "\\\\")
.replace("\r\n", "\\n")
.replace("\n", "\\n")
)
value = r"\nas"
text = '\n'.join(lines)
"#;
let files = vec![ProcessedFile::new(
"repro/escape.py".to_string(),
content.to_string(),
100,
0,
)];

let output = yek::concat_files(&files, &config).unwrap();
// The output must contain the exact content byte-for-byte, preserving literal \n sequences
assert!(output.contains(content));
// Verify that literal \n inside string literals was not rewritten into a physical newline
assert!(output.contains(r#".replace("\n", "\\n")"#));
assert!(output.contains(r#"r"\nas""#));
}

#[test]
fn test_concat_files_json_output_special_chars_in_filename() {
init_tracing();
Expand Down
Loading