Removed styled text in tables and add support to code into tables

main
kirbylife 2024-07-31 00:16:32 -06:00
parent fc2d95137f
commit f427ab127f
1 changed files with 37 additions and 9 deletions

View File

@ -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::Table(_)) => state.start_table_building(),
md::Event::Start(md::Tag::TableHead) => state.add_table_row(), 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::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::Emphasis) => state.toggle_emphasis(),
md::Event::Start(md::Tag::Strong) => state.toggle_strong(), md::Event::Start(md::Tag::Strong) => state.toggle_strong(),
md::Event::Start(md::Tag::Strikethrough) => unimplemented!("strikethrough disabled"), md::Event::Start(md::Tag::Strikethrough) => unimplemented!("strikethrough disabled"),
@ -150,25 +150,37 @@ impl State {
self.table.push(vec![]); 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) { fn start_block_quote(&mut self) {
self.pending_node_type = NodeType::Quote; self.pending_node_type = NodeType::Quote;
} }
fn start_code_block(&mut self) { fn start_code_block(&mut self) {
if !self.building_table {
self.pending_node_type = NodeType::Preformatted; self.pending_node_type = NodeType::Preformatted;
} }
}
fn start_list_item(&mut self) { fn start_list_item(&mut self) {
self.pending_node_type = NodeType::ListItem; self.pending_node_type = NodeType::ListItem;
} }
fn toggle_emphasis(&mut self) { fn toggle_emphasis(&mut self) {
if !self.building_table {
self.add_text("_"); self.add_text("_");
} }
}
fn toggle_strong(&mut self) { fn toggle_strong(&mut self) {
if !self.building_table {
self.add_text("**"); self.add_text("**");
} }
}
fn start_link(&mut self) { fn start_link(&mut self) {
self.link_text_stack.push(String::new()); self.link_text_stack.push(String::new());
@ -191,7 +203,7 @@ impl State {
self.building_table = false; self.building_table = false;
self.table = vec![]; self.table = vec![];
self.finish_node(); self.pending_node_type = NodeType::Preformatted;
self.pending_node_content += &table.to_string(); self.pending_node_content += &table.to_string();
self.finish_node(); self.finish_node();
} }
@ -245,7 +257,9 @@ impl State {
fn add_text(&mut self, text: &str) { fn add_text(&mut self, text: &str) {
if self.building_table { if self.building_table {
if let Some(last_row) = self.table.last_mut() { 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("<br>").collect::<Vec<&str>>().join("\n"));
}
} }
} else { } else {
for link_text in &mut self.link_text_stack { for link_text in &mut self.link_text_stack {
@ -256,10 +270,24 @@ impl State {
} }
fn add_inline_code(&mut self, code: &str) { fn add_inline_code(&mut self, code: &str) {
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("<br>")
.collect::<Vec<&str>>()
.join("\n"),
);
}
}
} else {
self.pending_node_content += "`"; self.pending_node_content += "`";
self.pending_node_content += code; self.pending_node_content += code;
self.pending_node_content += "`"; self.pending_node_content += "`";
} }
}
fn add_rule(&mut self) { fn add_rule(&mut self) {
self.add_text("-----"); self.add_text("-----");