markdown_composer/extensions/github.rs
1//! Markdown extensions defined by GitHub.
2//!
3//! The full specification can be found at https://github.github.com/gfm/.
4
5use std::fmt;
6
7/// A checkmark list item.
8#[derive(Clone, Debug, Default)]
9pub struct CheckmarkItem<'a> {
10 /// The state of the item.
11 ///
12 /// `True` if the item is checked, `false` otherwise.
13 pub checked: bool,
14 /// The text of the checkmark item.
15 pub text: &'a str,
16}
17
18impl<'a> CheckmarkItem<'a> {
19 /// Creates a new default checkmark item.
20 pub fn new() -> Self {
21 Self::default()
22 }
23
24 /// Creates a mew checkmark item with the given values.
25 pub fn from(text: &'a str, checked: bool) -> Self {
26 Self { text, checked }
27 }
28}
29
30impl fmt::Display for CheckmarkItem<'_> {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 let checkmark = if self.checked { "x" } else { " " };
33 write!(f, "[{}] {}", checkmark, self.text)
34 }
35}
36
37/// An extension trait for strikethrough transformations.
38pub trait Strikethrough {
39 /// Transforms the given text to be strikethrough.
40 ///
41 /// # Example
42 ///
43 /// ```rust
44 /// use markdown_composer::extensions::github::Strikethrough;
45 ///
46 /// let text = "text";
47 /// let striked = text.to_strikethrough();
48 /// assert_eq!(striked, "~text~");
49 /// ```
50 fn to_strikethrough(&self) -> String;
51}
52
53impl<T> Strikethrough for T
54where
55 T: AsRef<str>,
56{
57 fn to_strikethrough(&self) -> String {
58 format!("~{}~", self.as_ref())
59 }
60}