markdown-formatter 0.0.13

Flavored Markdown (ZH) content formatter
Documentation
use regex::Regex;

pub fn add_space_between_words_and_chinese_symbols(
    input: String,
    remove_extra_space: bool,
) -> String {
    let mut content =
        Regex::new(r#"(?P<word1>[\u4e00-\u9fff])(?P<word2>[a-zA-Z0-9.\(\[ !?'"\#<,_=+-])"#)
            .unwrap()
            .replace_all(&input, r"${word1} ${word2}")
            .to_string();

    content = Regex::new(r#"(?P<word1>[a-zA-Z0-9.\)\]!?'"\#>,_=+-])(?P<word2>[\u4e00-\u9fff])"#)
        .unwrap()
        .replace_all(&content, r"${word1} ${word2}")
        .to_string();

    if remove_extra_space {
        content =
            Regex::new(r#"(?P<word1>[\u4e00-\u9fff]) +(?P<word2>[a-zA-Z0-9.\(\[!?'"\#<,_=+-])"#)
                .unwrap()
                .replace_all(&content, r"${word1} ${word2}")
                .to_string();
        content =
            Regex::new(r#"(?P<word1>[a-zA-Z0-9.\)\]!?'"\#>,_=+-]) +(?P<word2>[\u4e00-\u9fff])"#)
                .unwrap()
                .replace_all(&content, r"${word1} ${word2}")
                .to_string();
    }

    content
}

pub fn remove_white_space_around_enclosed_url(input: String) -> String {
    Regex::new(r"<url>\s*(?P<uri>[^\s]+)\s*</url>")
        .unwrap()
        .replace_all(&input, r"<url>${uri}</url>")
        .to_string()
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_add_space() {
        let mut input = String::from("我是David(大卫)");
        assert_eq!(
            add_space_between_words_and_chinese_symbols(input, true),
            "我是 David(大卫)"
        );

        input = String::from("我是大卫(David)");
        assert_eq!(
            add_space_between_words_and_chinese_symbols(input, true),
            "我是大卫 (David)"
        );

        input =
            String::from("你认识一个叫David的北京人吗?他来北京十年了,完全变成了native speaker!");
        assert_eq!(
            add_space_between_words_and_chinese_symbols(input, true),
            "你认识一个叫 David 的北京人吗?他来北京十年了,完全变成了 native speaker!"
        );

        input = String::from("这个项目的  deadline是哪一天?");
        assert_eq!(
            add_space_between_words_and_chinese_symbols(input, true),
            "这个项目的 deadline 是哪一天?"
        );
    }

    #[test]
    fn test_remove_white_space_around_enclosed_url() {
        let input = "<url> https://abc.com/#h1</url>";
        assert_eq!(
            remove_white_space_around_enclosed_url(input.to_string()),
            "<url>https://abc.com/#h1</url>"
        );

        
        let input = "测试链接 <url> https://abc.com/#h1</url>";
        assert_eq!(
            remove_white_space_around_enclosed_url(input.to_string()),
            "测试链接 <url>https://abc.com/#h1</url>"
        );
    }
}