From f427ab127f63aa3c3382f504f5629c58985f976e Mon Sep 17 00:00:00 2001 From: kirbylife Date: Wed, 31 Jul 2024 00:16:32 -0600 Subject: [PATCH] Removed styled text in tables and add support to code into tables --- src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f2d0516..c438b4e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ pub fn convert(markdown_text: &str) -> String { md::Event::Start(md::Tag::Table(_)) => state.start_table_building(), md::Event::Start(md::Tag::TableHead) => state.add_table_row(), md::Event::Start(md::Tag::TableRow) => state.add_table_row(), - md::Event::Start(md::Tag::TableCell) => (), + md::Event::Start(md::Tag::TableCell) => state.add_table_cell(), md::Event::Start(md::Tag::Emphasis) => state.toggle_emphasis(), md::Event::Start(md::Tag::Strong) => state.toggle_strong(), md::Event::Start(md::Tag::Strikethrough) => unimplemented!("strikethrough disabled"), @@ -150,12 +150,20 @@ impl State { self.table.push(vec![]); } + fn add_table_cell(&mut self) { + if let Some(last) = self.table.last_mut() { + last.push(String::new()); + } + } + fn start_block_quote(&mut self) { self.pending_node_type = NodeType::Quote; } fn start_code_block(&mut self) { - self.pending_node_type = NodeType::Preformatted; + if !self.building_table { + self.pending_node_type = NodeType::Preformatted; + } } fn start_list_item(&mut self) { @@ -163,11 +171,15 @@ impl State { } fn toggle_emphasis(&mut self) { - self.add_text("_"); + if !self.building_table { + self.add_text("_"); + } } fn toggle_strong(&mut self) { - self.add_text("**"); + if !self.building_table { + self.add_text("**"); + } } fn start_link(&mut self) { @@ -191,7 +203,7 @@ impl State { self.building_table = false; self.table = vec![]; - self.finish_node(); + self.pending_node_type = NodeType::Preformatted; self.pending_node_content += &table.to_string(); self.finish_node(); } @@ -245,7 +257,9 @@ impl State { fn add_text(&mut self, text: &str) { if self.building_table { if let Some(last_row) = self.table.last_mut() { - last_row.push(text.to_string()); + if let Some(last_cell) = last_row.last_mut() { + last_cell.push_str(&text.split("
").collect::>().join("\n")); + } } } else { for link_text in &mut self.link_text_stack { @@ -256,9 +270,23 @@ impl State { } fn add_inline_code(&mut self, code: &str) { - self.pending_node_content += "`"; - self.pending_node_content += code; - self.pending_node_content += "`"; + if self.building_table { + if let Some(last_row) = self.table.last_mut() { + if let Some(last_cell) = last_row.last_mut() { + last_cell.push_str( + &code + .replace("\\_", "_") + .split("
") + .collect::>() + .join("\n"), + ); + } + } + } else { + self.pending_node_content += "`"; + self.pending_node_content += code; + self.pending_node_content += "`"; + } } fn add_rule(&mut self) {