1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use lazy_regex::{lazy_regex, Lazy};
use mdbook::{
    book::{Book, Chapter},
    errors::Error,
    preprocess::{CmdPreprocessor, Preprocessor, PreprocessorContext},
    BookItem,
};
use regex::{Captures, Regex};
use std::{collections::HashMap, io};

static WIKILINK_REGEX: Lazy<Regex> =
    lazy_regex!(r"\[\[(?P<link>[^\]\|]+)(?:\|(?P<title>[^\]]+))?\]\]");

pub fn handle_preprocessing(pre: impl Preprocessor) -> Result<(), Error> {
    let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;

    if ctx.mdbook_version != mdbook::MDBOOK_VERSION {
        eprintln!(
            "Warning: The {} plugin was built against version {} of mdbook, \
             but we're being called from version {}",
            pre.name(),
            mdbook::MDBOOK_VERSION,
            ctx.mdbook_version
        );
    }

    let processed_book = pre.run(&ctx, book)?;
    //serde_json::to_writer_pretty(io::stderr(), &processed_book);
    serde_json::to_writer(io::stdout(), &processed_book)?;

    Ok(())
}

fn chapter(it: &BookItem) -> Option<&Chapter> {
    if let BookItem::Chapter(ch) = it {
        Some(ch)
    } else {
        None
    }
}

fn normalize_string(s: &str) -> String {
    s.replace(" ", "%20")
        .replace("<", "&lt;")
        .replace(">", "&gt;")
}

pub struct WikiLinks;

impl Preprocessor for WikiLinks {
    fn name(&self) -> &str {
        "wikilink-preprocessor"
    }

    fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book, Error> {
        let chapters = book
            .iter()
            .filter_map(chapter)
            .filter_map(|it| {
                let path = it.path.as_ref()?.with_extension("");
                let path = path.to_str().unwrap();

                let key = normalize_string(path);

                Some((key, it.name.clone()))
            })
            .collect::<HashMap<_, _>>();

        book.for_each_mut(|it| {
            if let BookItem::Chapter(chapter) = it {
                chapter.content = WIKILINK_REGEX
                    .replace_all(&chapter.content, |it: &Captures| -> String {
                        let link_internals = normalize_string(it.get(1).unwrap().as_str());
                        let file = link_internals.to_string();

                        let link = it
                            .get(2)
                            .map(|it| it.as_str().trim().to_string())
                            .unwrap_or_else(|| {
                                if let Some(name) = chapters.get(&file) {
                                    format!("[{}](</{}.md>)", name, &file)
                                } else {
                                    link_internals
                                }
                            });

                        link
                    })
                    .to_string();
            }
        });

        Ok(book)
    }
}

#[cfg(test)]
mod tests {
    use crate::{normalize_string, WIKILINK_REGEX};

    #[test]
    fn normalize_string_symbols() {
        let cases = [
            ("/Folder/My File <>.md", "/Folder/My%20File%20&lt;&gt;.md"),
            (
                "/👩‍🌾 Gardening Tips/🪴 Sowing<Your>Garden/🎯  Create Custom Dashboards.md", 
                "/👩‍🌾%20Gardening%20Tips/🪴%20Sowing&lt;Your&gt;Garden/🎯%20%20Create%20Custom%20Dashboards.md"
            ),
        ];

        for (case, expected) in cases {
            assert_eq!(normalize_string(case), expected)
        }
    }

    #[test]
    fn extract_link_regex() {
        let cases = [
            ("[[Link]]", "Link"),
            ("[[🪴 Sowing<Your>Garden]]", "🪴 Sowing<Your>Garden"),
            (
                "[[/Templates/🪴 Sowing<Your>Garden]]",
                "/Templates/🪴 Sowing<Your>Garden",
            ),
        ];

        for (case, expected) in cases {
            let got = WIKILINK_REGEX
                .captures(case)
                .unwrap()
                .name("link")
                .unwrap()
                .as_str();
            assert_eq!(got.trim(), expected);
        }
    }

    #[test]
    fn extract_title_regex() {
        let cases = [
            ("[[Link | My New Link]]", "My New Link"),
            ("[[🪴 Sowing<Your>Garden | 🪴 Emoji Link]]", "🪴 Emoji Link"),
            ("[[🪴 Sowing<Your>Garden | 🪴/Emoji/Link]]", "🪴/Emoji/Link"),
        ];

        for (case, expected) in cases {
            let got = WIKILINK_REGEX
                .captures(case)
                .unwrap()
                .name("title")
                .unwrap()
                .as_str();
            assert_eq!(got.trim(), expected)
        }
    }
}