Skip to content

Commit 0adb15f

Browse files
committed
fix: markdown format 中文宽度处理
1 parent b0fab6d commit 0adb15f

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

src/markdown/table.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use comrak::{
55
parse_document,
66
};
77
use ropey::RopeSlice;
8-
use tracing::info;
98
use unicode_width::UnicodeWidthStr;
109

1110
/// 格式化 Markdown 表格
@@ -121,8 +120,6 @@ fn parse_tables(rope: RopeSlice, start_line: Position) -> Vec<Table> {
121120
range: table_range,
122121
};
123122

124-
info!("TABLE: {:?}", table);
125-
126123
Some(table)
127124
})
128125
.collect()
@@ -163,15 +160,15 @@ fn extract_row_cells<'a>(row_node: &'a AstNode<'a>, rope: RopeSlice) -> Vec<Stri
163160

164161
for cell in row_node.children() {
165162
if let NodeValue::TableCell = cell.data.borrow().value {
166-
let sourcepos = cell.data.borrow().sourcepos;
163+
let pos = cell.data.borrow().sourcepos;
167164

168-
let start_byte = sourcepos.start.column - 1;
169-
let end_byte = sourcepos.end.column;
165+
let start_byte = pos.start.column - 1;
166+
let end_byte = pos.end.column;
170167

171168
// 从 RopeSlice 中提取单元格文本
172169
if let Some(slice) = rope
173-
.line(sourcepos.start.line - 1)
174-
.get_slice(start_byte..end_byte)
170+
.line(pos.start.line - 1)
171+
.get_byte_slice(start_byte..end_byte)
175172
{
176173
cells.push(slice.to_string().trim().to_string());
177174
} else {
@@ -198,9 +195,10 @@ fn calculate_column_widths(
198195
.cloned()
199196
.chain(vec![header.to_vec()])
200197
.for_each(|row| {
201-
row.iter()
202-
.enumerate()
203-
.for_each(|(i, cell)| widths[i] = widths[i].max(cell.width()));
198+
row.iter().enumerate().for_each(|(i, cell)| {
199+
// info!("WIDTH: {cell} {}", cell.width());
200+
widths[i] = widths[i].max(cell.width())
201+
});
204202
});
205203

206204
widths
@@ -213,18 +211,17 @@ fn format_row(cells: &[String], col_widths: &[usize], alignments: &[TableAlignme
213211
.zip(col_widths)
214212
.zip(alignments)
215213
.map(|((cell, width), alignment)| {
214+
let cell_width = cell.width();
215+
let pad = width - cell_width;
216216
// 根据对齐方式格式化单元格
217217
match alignment {
218-
TableAlignment::Left => format!(" {:<width$} ", cell, width = width),
219-
TableAlignment::Right => format!(" {:>width$} ", cell, width = width),
218+
TableAlignment::Right => format!(" {}{cell} ", " ".repeat(pad)),
220219
TableAlignment::Center => {
221-
let cell_width = cell.width();
222-
223220
if cell_width >= *width {
224221
format!(" {} ", cell,)
225222
} else {
226-
let left_pad = (width - cell_width) / 2;
227-
let right_pad = width - cell_width - left_pad;
223+
let left_pad = pad / 2;
224+
let right_pad = pad - left_pad;
228225
format!(
229226
" {}{}{} ",
230227
" ".repeat(left_pad),
@@ -233,7 +230,7 @@ fn format_row(cells: &[String], col_widths: &[usize], alignments: &[TableAlignme
233230
)
234231
}
235232
}
236-
_ => format!(" {:<width$} ", cell, width = width), // 默认左对齐
233+
_ => format!(" {cell}{} ", " ".repeat(pad)),
237234
}
238235
})
239236
.collect();

0 commit comments

Comments
 (0)