Skip to content
Merged
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
57 changes: 42 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ pub fn split_cells(line: &str) -> Vec<String> {
if ch == '\\' {
if let Some(&next) = chars.peek() {
if next == '|' {
current.push('|');
chars.next();
cells.push(current.trim().to_string());
current.clear();
continue;
}
Comment on lines 35 to 42
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Incorrect handling of escaped pipe characters

The change to the escape handling logic is incorrect. When \| is encountered in a markdown table cell, it represents a literal pipe character that should be included in the cell content, not a cell delimiter. The current implementation incorrectly treats \| as a cell boundary.

Apply this diff to fix the escape handling:

             if let Some(&next) = chars.peek() {
                 if next == '|' {
                     chars.next();
-                    cells.push(current.trim().to_string());
-                    current.clear();
+                    current.push('|');
                     continue;
                 }
             }

This ensures that \| is correctly interpreted as a literal pipe character within the cell content.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if ch == '\\' {
if let Some(&next) = chars.peek() {
if next == '|' {
current.push('|');
chars.next();
cells.push(current.trim().to_string());
current.clear();
continue;
}
if ch == '\\' {
if let Some(&next) = chars.peek() {
if next == '|' {
chars.next();
current.push('|');
continue;
}
}
}
🤖 Prompt for AI Agents
In src/lib.rs around lines 35 to 42, the current code incorrectly treats the
escaped pipe sequence `\|` as a cell delimiter instead of a literal pipe
character. To fix this, modify the escape handling logic so that when `\|` is
encountered, it appends a literal pipe character to the current cell content
rather than splitting the cell. This involves consuming the escape character and
the pipe, then adding a pipe character to the current string instead of pushing
and clearing it.

}
Expand Down Expand Up @@ -94,6 +95,7 @@ pub fn reflow_table(lines: &[String]) -> Vec<String> {

let raw = trimmed.join(" ");
let chunks: Vec<&str> = SENTINEL_RE.split(&raw).collect();
let split_within_line = chunks.len() > trimmed.len();
let mut cells = Vec::new();
for (idx, chunk) in chunks.iter().enumerate() {
let mut ch = (*chunk).to_string();
Expand Down Expand Up @@ -123,48 +125,73 @@ pub fn reflow_table(lines: &[String]) -> Vec<String> {
// positions when checking for consistency across rows.
let max_cols = rows.iter().map(Vec::len).max().unwrap_or(0);

if rows.iter().any(|r| {
let count = r.len();
count != 0 && count != max_cols
}) {
return lines.to_vec();
}

let mut cleaned = Vec::new();
for mut row in rows {
row.retain(|c| !c.is_empty());
while row.len() < max_cols {
row.push(String::new());
}
cleaned.push(row);
}

if !split_within_line
&& cleaned
.iter()
.map(Vec::len)
.collect::<std::collections::HashSet<_>>()
.len()
> 1
{
return lines.to_vec();
}

let mut widths = vec![0; max_cols];
for row in &cleaned {
for (idx, cell) in row.iter().enumerate() {
widths[idx] = widths[idx].max(cell.len());
}
}

cleaned
let out: Vec<String> = cleaned
.into_iter()
.map(|row| {
let padded: Vec<String> = row
.into_iter()
.enumerate()
.map(|(i, c)| format!("{:<width$}", c, width = widths[i]))
.collect();
format!("| {} |", padded.join(" | "))
format!("{}| {} |", indent, padded.join(" | "))
})
.collect();

if let Some(sep) = sep_line {
let mut sep_cells: Vec<String> = split_cells(&sep);
while sep_cells.len() < widths.len() {
sep_cells.push(String::new());
}
let sep_padded: Vec<String> = sep_cells
.iter()
.enumerate()
.map(|(i, cell)| {
let trimmed = cell.trim();
let left = trimmed.starts_with(':');
let right = trimmed.ends_with(':');
let mut dashes = "-".repeat(widths[i].max(3));
if left {
dashes.remove(0);
dashes.insert(0, ':');
}
if right {
dashes.pop();
dashes.push(':');
}
dashes
})
.collect();
let sep_line_out = format!("{}| {} |", indent, sep_padded.join(" | "));
if let Some(first) = out.first().cloned() {
let mut with_sep = vec![first, format!("{}{}", indent, sep)];
let mut with_sep = vec![first, sep_line_out];
with_sep.extend(out.into_iter().skip(1));
return with_sep;
}
return vec![format!("{}{}", indent, sep)];
return vec![sep_line_out];
}

out
Expand Down
Loading