diff --git a/src/app/browser/window/tab/item/page/content/text/markdown/tags.rs b/src/app/browser/window/tab/item/page/content/text/markdown/tags.rs index d3799ee8..9748680b 100644 --- a/src/app/browser/window/tab/item/page/content/text/markdown/tags.rs +++ b/src/app/browser/window/tab/item/page/content/text/markdown/tags.rs @@ -1,3 +1,4 @@ +mod bold; mod header; mod list; mod quote; @@ -6,6 +7,7 @@ mod title; use std::collections::HashMap; +use bold::Bold; use gtk::{ TextBuffer, TextTag, TextTagTable, gdk::RGBA, @@ -20,6 +22,7 @@ use title::Title; pub struct Tags { pub text_tag_table: TextTagTable, // Tags + pub bold: Bold, pub header: Header, pub list: TextTag, pub quote: Quote, @@ -47,6 +50,7 @@ impl Tags { Self { text_tag_table, // Tags + bold: Bold::new(), header: Header::new(), list, quote: Quote::new(), @@ -65,10 +69,16 @@ impl Tags { self.quote.render(buffer); + self.bold.render(buffer); + reference::render_images_links(&buffer, base, &link_color, links); reference::render_images(&buffer, base, &link_color, links); reference::render_links(&buffer, base, &link_color, links); - title.map(|ref s| reference::strip_tags(s)) // @TODO other tags + title.map(|mut s| { + s = reference::strip_tags(&s); + s = bold::strip_tags(&s); + s // @TODO other tags + }) } } diff --git a/src/app/browser/window/tab/item/page/content/text/markdown/tags/bold.rs b/src/app/browser/window/tab/item/page/content/text/markdown/tags/bold.rs new file mode 100644 index 00000000..8060c6ad --- /dev/null +++ b/src/app/browser/window/tab/item/page/content/text/markdown/tags/bold.rs @@ -0,0 +1,86 @@ +use gtk::{ + TextBuffer, TextTag, + WrapMode::Word, + prelude::{TextBufferExt, TextBufferExtManual}, +}; +use regex::Regex; + +const REGEX_BOLD: &str = r"\*\*(?P[^*]+)\*\*"; + +pub struct Bold(TextTag); + +impl Bold { + pub fn new() -> Self { + Self(TextTag::builder().weight(600).wrap_mode(Word).build()) + } + + /// Apply **bold** `Tag` to given `TextBuffer` + pub fn render(&self, buffer: &TextBuffer) { + assert!(buffer.tag_table().add(&self.0)); + + let (start, end) = buffer.bounds(); + let full_content = buffer.text(&start, &end, true).to_string(); + + let matches: Vec<_> = Regex::new(REGEX_BOLD) + .unwrap() + .captures_iter(&full_content) + .collect(); + + for cap in matches.into_iter().rev() { + let full_match = cap.get(0).unwrap(); + + let start_char_offset = full_content[..full_match.start()].chars().count() as i32; + let end_char_offset = full_content[..full_match.end()].chars().count() as i32; + + let mut start_iter = buffer.iter_at_offset(start_char_offset); + let mut end_iter = buffer.iter_at_offset(end_char_offset); + + buffer.delete(&mut start_iter, &mut end_iter); + buffer.insert_with_tags(&mut start_iter, &cap["text"], &[&self.0]) + } + } +} + +pub fn strip_tags(value: &str) -> String { + let mut result = String::from(value); + for cap in Regex::new(REGEX_BOLD) + .unwrap() + .captures_iter(&value) + .into_iter() + { + if let Some(m) = cap.get(0) { + result = result.replace(m.as_str(), &cap["text"]); + } + } + result +} + +#[test] +fn test_strip_tags() { + const VALUE: &str = r"Some **bold 1** and **bold 2** with ![img](https://link.com)"; + let mut result = String::from(VALUE); + for cap in Regex::new(REGEX_BOLD) + .unwrap() + .captures_iter(VALUE) + .into_iter() + { + if let Some(m) = cap.get(0) { + result = result.replace(m.as_str(), &cap["text"]); + } + } + assert_eq!( + result, + "Some bold 1 and bold 2 with ![img](https://link.com)" + ) +} + +#[test] +fn test_regex() { + let cap: Vec<_> = Regex::new(REGEX_BOLD) + .unwrap() + .captures_iter(r"Some **bold 1** and **bold 2** with ![img](https://link.com)") + .collect(); + + assert_eq!(&cap.get(0).unwrap()["text"], "bold 1"); + assert_eq!(&cap.get(1).unwrap()["text"], "bold 2"); +}