mdbook-epub 0.5.3

An EPUB renderer for mdbook.
Documentation
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
use std::collections::HashMap;
use std::path::MAIN_SEPARATOR_STR;

use const_format::concatcp;
use html_parser::{Dom, Element, Node};
use mdbook_core::book::BookItem;
use mdbook_renderer::RenderContext;
use pulldown_cmark::{Event, Tag};
use tracing::{debug, trace, warn};
use url::Url;

use crate::resources::asset::{Asset, AssetKind};
use crate::{Error, path_io, utils};

// Internal constants for reveling 'upper folder' paths in resource links inside MD
pub(crate) const UPPER_PARENT: &str = concatcp!("..", MAIN_SEPARATOR_STR);
pub(crate) const UPPER_PARENT_LINUX: &str = concatcp!("..", "/");
pub(crate) const UPPER_PARENT_STARTS_SLASH: &str =
    concatcp!(MAIN_SEPARATOR_STR, "..", MAIN_SEPARATOR_STR);
pub(crate) const UPPER_PARENT_STARTS_SLASH_LINUX: &str = concatcp!("/", "..", "/");
// That is a source URL for embedded resource we want to skip from processing
pub(crate) const EMBEDDED_URL_START: &str = "data:image"; // only

#[cfg(not(target_os = "windows"))]
pub(crate) const UPPER_FOLDER_PATHS: &[&str] =
    &[MAIN_SEPARATOR_STR, UPPER_PARENT, UPPER_PARENT_LINUX];

#[cfg(target_os = "windows")]
pub(crate) const UPPER_FOLDER_PATHS: &[&str] =
    &["/", MAIN_SEPARATOR_STR, UPPER_PARENT, UPPER_PARENT_LINUX];

/// Find all resources in book and put them into HashMap.
/// The key is a link, value is a composed Asset
pub(crate) fn find(ctx: &RenderContext) -> Result<HashMap<String, Asset>, Error> {
    let mut assets: HashMap<String, Asset> = HashMap::new();
    debug!("Finding resources by:\n{:?}", ctx.config);
    let src_dir = path_io(
        ctx.root.join(&ctx.config.book.src).canonicalize(),
        &ctx.config.book.src,
    )?;

    debug!(
        "Start iteration over a [{:?}] sections in src_dir = {:?}",
        ctx.book.items.len(),
        src_dir
    );
    for section in ctx.book.iter() {
        match *section {
            BookItem::Chapter(ref ch) => {
                let mut assets_count = 0;
                debug!("Searching links and assets for: '{}'", ch);
                if ch.path.is_none() {
                    debug!("'{}' is a draft chapter and should be no content.", ch.name);
                    continue;
                }
                for link in find_assets_in_markdown(&ch.content)? {
                    debug!("'{}' finding Asset...", &link);
                    let asset = if let Ok(url) = Url::parse(&link) {
                        Asset::from_url(&link, url, &ctx.destination)
                    } else {
                        let result = Asset::from_local(&link, &src_dir, ch.path.as_ref().unwrap());
                        if let Err(Error::AssetOutsideSrcDir(_)) = result {
                            warn!("Asset '{link}' is outside source dir '{src_dir:?}' and ignored");
                            continue;
                        };
                        result
                    }?;

                    // that is CORRECT generation way
                    debug!(
                        "Check relative path assets chapter: '{}' for\n{}",
                        ch.name, asset
                    );
                    match asset.source {
                        // local asset kind
                        AssetKind::Local(_) => {
                            let relative = asset.location_on_disk.strip_prefix(&src_dir);
                            match relative {
                                Ok(_relative_link_path) => {
                                    let link_key = asset.original_link.clone();
                                    if let std::collections::hash_map::Entry::Vacant(e) =
                                        assets.entry(link_key.to_owned())
                                    {
                                        debug!(
                                            "Adding asset by link '{:?}' : {}",
                                            link_key, &asset
                                        );
                                        e.insert(asset);
                                        assets_count += 1;
                                    } else {
                                        debug!("Skipped asset for '{}'", link_key);
                                    }
                                }
                                _ => {
                                    // skip incorrect resource/image link outside of book /SRC/ folder
                                    warn!(
                                        "Sorry, we can't add 'Local asset' that is outside of book's /src/ folder, {:?}",
                                        &asset
                                    );
                                }
                            }
                        }
                        AssetKind::Remote(_) => {
                            // remote asset kind
                            let link_key = asset.original_link.clone();
                            debug!("Adding Remote asset by link '{}' : {}", link_key, &asset);
                            assets.insert(link_key, asset);
                            assets_count += 1;
                        }
                        AssetKind::Embedded => {
                            debug!("Embedded asset '{}' by Event", &asset.original_link);
                        }
                    };
                }
                debug!(
                    "Found '{}' links and assets inside '{}'",
                    assets_count, ch.name
                );
            }
            BookItem::Separator => trace!("Skip separator."),
            BookItem::PartTitle(ref title) => trace!("Skip part title: {}.", title),
        }
    }
    debug!("Added '{}' links and assets in total", assets.len());
    Ok(assets)
}

// Look up resources in nested HTML element
fn find_assets_in_nested_html_tags(element: &Element) -> Result<Vec<String>, Error> {
    let mut found_asset = Vec::new();

    if element.name == "img"
        && let Some(dest) = &element.attributes["src"]
        && !dest.trim().starts_with(EMBEDDED_URL_START)
    // skip EMBEDDED url
    {
        found_asset.push(dest.clone());
    }
    for item in &element.children {
        if let Node::Element(nested_element) = item {
            found_asset.extend(find_assets_in_nested_html_tags(nested_element)?);
        }
    }

    Ok(found_asset)
}

// Look up resources in chapter md content
fn find_assets_in_markdown(chapter_src_content: &str) -> Result<Vec<String>, Error> {
    let mut found_asset = Vec::new();

    let pull_down_parser = utils::create_new_pull_down_parser(chapter_src_content);
    // that will process chapter content and find assets
    for event in pull_down_parser {
        match event {
            Event::Start(Tag::Image {
                link_type: _,
                dest_url,
                title: _,
                id: _,
            }) => {
                if !dest_url.trim().starts_with(EMBEDDED_URL_START) {
                    debug!("Push asset with: '{}'", &dest_url);
                    found_asset.push(dest_url.to_string());
                } else {
                    debug!("Skip EMBEDDED asset: '{}'", &dest_url);
                }
            }
            Event::Html(html) | Event::InlineHtml(html) => {
                let content = html.to_owned().into_string();

                if let Ok(dom) = Dom::parse(&content) {
                    for item in dom.children {
                        if let Node::Element(ref element) = item {
                            found_asset.extend(find_assets_in_nested_html_tags(element)?);
                        }
                    }
                }
            }
            _ => {}
        }
    }

    found_asset.sort();
    found_asset.dedup();
    if !found_asset.is_empty() {
        trace!("Assets found in content : {:?}", found_asset);
    }
    Ok(found_asset)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{Value, json};
    use std::path::{Path, PathBuf};
    use tempfile::TempDir;

    #[test]
    fn test_find_images() {
        let parent_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/long_book_example/src");
        let upper_parent_dir =
            Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/long_book_example");
        let src =
            "![Image 1](./rust-logo.png)\n[a link](to/nowhere) ![Image 2][2]\n\n[2]: reddit.svg\n\
            \n\n<img alt=\"Rust Logo in html\" src=\"reddit.svg\" class=\"center\" style=\"width: 20%;\" />\n\n\
            \n\n![Image 4](assets/rust-logo.png)\n[a link](to/nowhere)
            ![Image 4](../third_party/wikimedia/Epub_logo_color.svg)\n[a link](to/nowhere)";
        let should_be = vec![
            upper_parent_dir
                .join("third_party/wikimedia/Epub_logo_color.svg")
                .canonicalize()
                .unwrap(),
            parent_dir.join("rust-logo.png").canonicalize().unwrap(),
            parent_dir
                .join("assets/rust-logo.png")
                .canonicalize()
                .unwrap(),
            parent_dir.join("reddit.svg").canonicalize().unwrap(),
        ];

        let got = find_assets_in_markdown(src)
            .unwrap()
            .into_iter()
            .map(|a| parent_dir.join(a).canonicalize().unwrap())
            .collect::<Vec<_>>();

        assert_eq!(got, should_be);
    }

    #[test]
    fn test_find_local_asset() {
        let link = "./rust-logo.png";
        // link and link2 - are the same asset
        let link2 = "assets/rust-logo.png";
        // not_found_link3 path won't be found because it's outside of src/
        let not_found_link3 = "../third_party/wikimedia/Epub_logo_color.svg";

        let tmp_dir = TempDir::new().unwrap();
        let temp = tmp_dir.path().join("mdbook-epub");
        let dest_dir = temp.as_path().to_string_lossy().to_string();
        let chapters = json!([{
            "Chapter": {
            "name": "Chapter 1",
            "content": format!("# Chapter 1\r\n\r\n![Image]({link})\r\n![Image]({link2})\r\n![Image]({not_found_link3})"),
            "number": [1],
            "sub_items": [],
            "path": "chapter_1.md",
            "parent_names": []}
        }]);
        let ctx = ctx_with_chapters(&chapters, &dest_dir).unwrap();

        let mut assets = find(&ctx).unwrap();
        assert_eq!(2, assets.len());

        fn assert_asset(a: Asset, link: &str, ctx: &RenderContext) {
            let link_as_path = utils::normalize_path(&PathBuf::from(link));
            let mut src_path = PathBuf::from(&ctx.config.book.src);
            if link.starts_with(UPPER_PARENT) || link.starts_with(UPPER_PARENT_STARTS_SLASH) {
                src_path.pop();
            }

            let filename = link_as_path.as_path().to_str().unwrap();
            let absolute_location = PathBuf::from(&ctx.root)
                .join(&src_path)
                .join(&link_as_path)
                .canonicalize()
                .expect("Asset Location is not found");

            let source = AssetKind::Local(PathBuf::from(link));
            let should_be = Asset::new(link.to_string(), filename, absolute_location, source);
            assert_eq!(a, should_be);
        }
        trace!("All = {:?}", assets);
        assert_asset(assets.remove(link).unwrap(), link, &ctx);
        assert_asset(assets.remove(link2).unwrap(), link2, &ctx);
    }

    #[test]
    fn test_find_remote_asset() {
        let link = "https://www.rust-lang.org/static/images/rust-logo-blk.svg";
        let link2 = "https://www.rust-lang.org/static/images/rust-logo-blk.png";
        let link_parsed = Url::parse(link).unwrap();
        let link_parsed2 = Url::parse(link2).unwrap();
        let tmp_dir = TempDir::new().unwrap();
        let temp = tmp_dir.path().join("mdbook-epub");
        let dest_dir = temp.as_path().to_string_lossy().to_string();
        let chapters = json!([
        {"Chapter": {
            "name": "Chapter 1",
            "content": format!("# Chapter 1\r\n\r\n![Image]({link})\r\n<a href=\"\"><img  src=\"{link2}\"></a>"),
            "number": [1],
            "sub_items": [],
            "path": "chapter_1.md",
            "parent_names": []}}]);
        let ctx = ctx_with_chapters(&chapters, &dest_dir).unwrap();

        let mut assets = find(&ctx).unwrap();
        assert_eq!(2, assets.len());

        for (key, value) in assets.clone().into_iter() {
            trace!("{} / {:?}", key, &value);
            match value.source {
                AssetKind::Remote(internal_url) => {
                    let key_to_remove = value.location_on_disk.to_str().unwrap();
                    // let got = assets.remove(key_to_remove).unwrap();
                    let got = assets.remove(key.clone().as_str()).unwrap();
                    let filename = if key_to_remove.contains(".svg") {
                        PathBuf::from("").join(utils::hash_link(&link_parsed))
                    } else {
                        PathBuf::from("").join(utils::hash_link(&link_parsed2))
                    };
                    let absolute_location = temp.as_path().join(&filename);
                    let source = AssetKind::Remote(internal_url);
                    let should_be = Asset::new(key, filename, absolute_location, source);
                    assert_eq!(got, should_be);
                }
                _ => {
                    // only remote urls are processed here for simplicity
                    panic!("Should not be here... only remote urls are used here")
                }
            }
        }
    }

    #[test]
    fn test_find_draft_chapter_without_error() {
        let tmp_dir = TempDir::new().unwrap();
        let temp = tmp_dir.path().join("mdbook-epub");
        let dest_dir = temp.as_path().to_string_lossy().to_string();
        let chapters = json!([
        {"Chapter": {
            "name": "Chapter 1",
            "content": "",
            "number": [1],
            "sub_items": [],
            "path": null,
            "parent_names": []}}]);
        let ctx = ctx_with_chapters(&chapters, &dest_dir).unwrap();
        assert!(find(&ctx).unwrap().is_empty());
    }

    #[test]
    #[should_panic(expected = "Asset was not found")]
    fn test_find_asset_fail_when_chapter_dir_not_exist() {
        panic!(
            "{}",
            Asset::from_local(
                "a.png",
                Path::new("tests\\dummy\\src"),
                Path::new("ch\\a.md")
            )
            .unwrap_err()
            .to_string()
        );
    }

    #[cfg(not(target_os = "windows"))]
    #[test]
    #[should_panic(expected = "Asset was not found")]
    fn test_find_asset_fail_when_chapter_dir_not_exist_linux() {
        panic!(
            "{}",
            Asset::from_local(
                "a.png",
                Path::new("tests/long_book_example/src"),
                Path::new("ch/a.md")
            )
            .unwrap_err()
            .to_string()
        );
    }

    #[cfg(not(target_os = "windows"))]
    #[test]
    #[should_panic(
        expected = "Asset was not found: 'wikimedia' by 'tests/long_book_example/third_party/a.md/wikimedia', error = No such file or directory (os error 2)"
    )]
    fn test_find_asset_fail_when_it_is_a_dir() {
        panic!(
            "{}",
            Asset::from_local(
                "wikimedia",
                Path::new("tests/long_book_example"),
                Path::new("third_party/a.md")
            )
            .unwrap_err()
            .to_string()
        );
    }

    #[cfg(target_os = "windows")]
    #[test]
    #[should_panic(
    //expected = "Asset was not found: 'wikimedia' by 'tests\\dummy\\third_party\\a.md\\wikimedia', error = Системе не удается найти указанный путь. (os error 3)"
    expected = "Asset was not found: 'wikimedia' by 'tests\\dummy\\third_party\\a.md\\wikimedia', error = The system cannot find the path specified. (os error 3)"
    )]
    fn test_find_asset_fail_when_it_is_a_dir_windows() {
        panic!(
            "{}",
            Asset::from_local(
                "wikimedia",
                Path::new("tests\\dummy"),
                Path::new("third_party\\a.md")
            )
            .unwrap_err()
            .to_string()
        );
    }

    fn ctx_with_chapters(
        chapters: &Value,
        destination: &str,
    ) -> Result<RenderContext, mdbook_core::errors::Error> {
        let json_ctx = json!({
            "version": mdbook_core::MDBOOK_VERSION,
            "root": "tests/long_book_example",
            "book": {"items": chapters, "__non_exhaustive": null},
            "config": {
                "book": {"authors": [], "language": "en", "text-direction": "ltr",
                    "src": "src", "title": "DummyBook"},
                "output": {"epub": {"curly-quotes": true}}},
            "destination": destination
        });
        RenderContext::from_json(json_ctx.to_string().as_bytes())
    }

    #[test]
    fn test_compute_asset_path_by_src_and_link_to_full_path() {
        let book_source_root_dir =
            Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/long_book_example/src");
        let mut book_chapter_dir = book_source_root_dir.clone();
        book_chapter_dir.push(Path::new("chapter_1.md"));

        let link = "./asset1.jpg";
        let asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_chapter_dir);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        let full_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            full_path.as_path(),
            Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("tests/long_book_example/src")
                .join("asset1.jpg")
        );
    }

    #[test]
    fn test_remove_prefixes() {
        let link_string = String::from("assets/verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, UPPER_FOLDER_PATHS);
        assert_eq!("assets/verify.jpeg", link_string);

        let link_string = String::from("/assets/verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, UPPER_FOLDER_PATHS);
        assert_eq!("assets/verify.jpeg", link_string);

        let link_string = String::from("../../assets/verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, UPPER_FOLDER_PATHS);
        assert_eq!("../assets/verify.jpeg", link_string);
        let new_link = Asset::remove_prefixes(link_string, UPPER_FOLDER_PATHS);
        assert_eq!("assets/verify.jpeg", new_link);

        let upper_folder_path = &[UPPER_PARENT_LINUX, UPPER_PARENT, MAIN_SEPARATOR_STR, "/"];
        let link_string = String::from("assets/verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, upper_folder_path);
        assert_eq!("assets/verify.jpeg", link_string);

        let link_string = String::from("/assets/verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, upper_folder_path);
        assert_eq!("assets/verify.jpeg", link_string);

        let link_string = String::from("../../assets/verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, upper_folder_path);
        assert_eq!("../assets/verify.jpeg", link_string);
        let new_link = Asset::remove_prefixes(link_string, upper_folder_path);
        assert_eq!("assets/verify.jpeg", new_link);
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn test_remove_prefixes_windows() {
        let link_string = String::from("assets\\verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, UPPER_FOLDER_PATHS);
        assert_eq!("assets\\verify.jpeg", link_string);

        let link_string = String::from("\\assets\\verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, UPPER_FOLDER_PATHS);
        assert_eq!("assets\\verify.jpeg", link_string);

        let link_string = String::from("..\\..\\assets\\verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, UPPER_FOLDER_PATHS);
        assert_eq!("..\\assets\\verify.jpeg", link_string);
        let new_link = Asset::remove_prefixes(link_string, UPPER_FOLDER_PATHS);
        assert_eq!("assets\\verify.jpeg", new_link);

        let upper_folder_path = &[UPPER_PARENT_LINUX, UPPER_PARENT, MAIN_SEPARATOR_STR, &"/"];
        let link_string = String::from("assets\\verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, upper_folder_path);
        assert_eq!("assets\\verify.jpeg", link_string);

        let link_string = String::from("/assets\\verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, upper_folder_path);
        assert_eq!("assets\\verify.jpeg", link_string);

        let link_string = String::from("..\\..\\assets\\verify.jpeg");
        let link_string = Asset::remove_prefixes(link_string, upper_folder_path);
        assert_eq!("..\\assets\\verify.jpeg", link_string);
        let new_link = Asset::remove_prefixes(link_string, upper_folder_path);
        assert_eq!("assets\\verify.jpeg", new_link);
    }

    #[test]
    fn test_compute_asset_path_by_src_and_link() {
        let mut book_or_chapter_src = ["media", "book", "src"].iter().collect::<PathBuf>();

        let mut link = "./asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path().as_os_str(),
            ["media", "book", "src", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
                .as_os_str()
        );

        link = "asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        link = "../upper/assets/asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "upper", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        link = "assets/asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        link = "./assets/asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        book_or_chapter_src = ["media", "book", "src", "chapter1"]
            .iter()
            .collect::<PathBuf>();

        link = "../assets/asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        book_or_chapter_src = ["media", "book", "src", "chapter1", "inner"]
            .iter()
            .collect::<PathBuf>();
        link = "../../assets/asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn test_compute_asset_path_by_src_and_link_windows() {
        let mut book_or_chapter_src = ["media", "book", "src"].iter().collect::<PathBuf>();

        let mut link = ".\\asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path().as_os_str(),
            (["media", "book", "src", "asset1.jpg"])
                .iter()
                .collect::<PathBuf>()
                .as_os_str()
        );

        link = "asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        link = "..\\upper\\assets\\asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "upper", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        link = "assets\\asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        link = ".\\assets\\asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        book_or_chapter_src = ["media", "book", "src", "chapter1"]
            .iter()
            .collect::<PathBuf>();

        link = "..\\assets\\asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );

        book_or_chapter_src = ["media", "book", "src", "chapter1", "inner"]
            .iter()
            .collect::<PathBuf>();
        link = "..\\..\\assets\\asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(
            asset_path.as_path(),
            ["media", "book", "src", "assets", "asset1.jpg"]
                .iter()
                .collect::<PathBuf>()
        );
    }

    #[cfg(not(target_os = "windows"))]
    #[test]
    fn test_incorrect_compute_asset_path_by_src_and_link() {
        let book_or_chapter_src = ["media", "book", "src"].iter().collect::<PathBuf>();

        let link = "/assets/asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(asset_path.as_path(), Path::new("/assets/asset1.jpg"));

        let link = "/../assets/asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(asset_path.as_path(), Path::new("/assets/asset1.jpg"));
    }

    #[cfg(target_os = "windows")]
    #[test]
    fn test_incorrect_compute_asset_path_by_src_and_link_windows() {
        let book_or_chapter_src = ["media", "book", "src"].iter().collect::<PathBuf>();

        let link = "\\assets\\asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(asset_path.as_path(), Path::new("/assets/asset1.jpg"));

        let link = "\\..\\assets/asset1.jpg";
        let mut asset_path = Asset::compute_asset_path_by_src_and_link(link, &book_or_chapter_src);
        let normalized_link = utils::normalize_path(PathBuf::from(link).as_path());
        asset_path = asset_path.join(normalized_link); // compose final result
        assert_eq!(asset_path.as_path(), Path::new("/assets/asset1.jpg"));
    }
}