mdbook-permalinks 2.0.1

Generate permalinks in mdBook using paths
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
#![warn(clippy::unwrap_used)]

use std::{
    collections::{HashMap, HashSet},
    fmt::Debug,
    str::FromStr,
};

use anyhow::{Context, Result};
use git2::Repository;
use mdbook_markdown::pulldown_cmark;
use mdbook_preprocessor::{Preprocessor, PreprocessorContext, book::Book};
use serde::Deserialize;
use tap::Tap;
use tracing::{Level, debug, info, info_span, span::EnteredSpan, trace, warn};
use url::Url;

use mdbookkit::{
    book::{BookConfigHelper, BookHelper, book_from_stdin},
    emit_debug, emit_error,
    error::{ExitProcess, OnWarning, has_severity},
    logging::Logging,
    ticker, ticker_item,
    url::{ExpectUrl, UrlFromPath},
};

use self::{
    link::{ContentHint, LinkStatus, PathStatus, RelativeLink},
    page::Pages,
    vcs::{Permalink, PermalinkFormat},
};

mod diagnostic;
mod link;
mod page;
#[cfg(test)]
mod tests;
mod vcs;

fn main() -> Result<()> {
    Logging::default().init();
    let _span = info_span!({ env!("CARGO_PKG_NAME") }).entered();
    let Program { command } = clap::Parser::parse();
    match command {
        None => mdbook().exit(emit_error!()),
        Some(Command::Supports { .. }) => {}
        #[cfg(feature = "_testing")]
        Some(Command::Describe) => {
            let desc = mdbookkit::docs::Reflect::default()
                .map_type::<UrlPrefix>("String")
                .describe::<Config>()?;
            print!("{desc}")
        }
    }
    Ok(())
}

fn mdbook() -> Result<()> {
    let (ctx, book) = book_from_stdin().context("Failed to read from mdBook")?;
    Permalinks.run(&ctx, book)?.to_stdout(&ctx)?;
    Ok(())
}

#[derive(clap::Parser, Debug, Clone)]
struct Program {
    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(clap::Subcommand, Debug, Clone)]
enum Command {
    #[clap(hide = true)]
    Supports { renderer: String },
    #[cfg(feature = "_testing")]
    #[clap(hide = true)]
    Describe,
}

struct Permalinks;

impl Preprocessor for Permalinks {
    fn name(&self) -> &str {
        PREPROCESSOR_NAME
    }

    fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result<Book> {
        match Environment::new(ctx) {
            Ok(Ok(env)) => {
                debug!("{env:#?}");
                env.run(ctx, book)
            }
            Ok(Err(err)) => {
                warn!("{:?}", err.context("Preprocessor will be disabled"));
                Ok(book)
            }
            Err(err) => Err(err).context("Failed to initialize"),
        }
    }
}

struct Environment {
    vcs: VersionControl,
    root_dir: Url,
    markdown: pulldown_cmark::Options,
    config: Config,
}

struct VersionControl {
    root: Url,
    link: Permalink,
    repo: Repository,
}

impl Preprocessor for Environment {
    fn name(&self) -> &str {
        PREPROCESSOR_NAME
    }

    fn run(&self, _: &PreprocessorContext, mut book: Book) -> Result<Book> {
        let mut content = Pages::new(self.markdown);

        for (path, ch) in book.iter_chapters() {
            let path = path
                .to_str()
                .context("only Unicode characters are supported")
                .with_context(|| format!("{path:?} contains unsupported characters"))?;
            let url = self.root_dir.join(path).expect_url();
            content
                .insert(url, &ch.content)
                .with_context(|| format!("Failed to parse {path:?}"))?;
        }

        self.resolve(&mut content);

        self.reporter(&content, |_| true)
            .name_display(|url| self.rel_path(url))
            .build()
            .to_stderr();

        content.log_stats();

        // bail before emitting changes
        self.config.fail_on_warnings.check()?;

        let mut result = book
            .iter_chapters()
            .map(|(path, _)| {
                let _span = info_span!("emit", key = ?path).entered();
                debug!("generating output");
                let key = path.to_str().expect("paths have been checked");
                let url = self.root_dir.join(key).expect_url();
                let out = content.emit(&url).context("Error generating output")?;
                Ok((path.clone(), out))
            })
            .collect::<Result<HashMap<_, _>>>()?;

        book.for_each_text_mut(|path, content| {
            if let Some(output) = result.remove(path) {
                *content = output;
            }
        });

        if has_severity(Level::WARN) {
            warn!("Finished with problems");
        } else {
            info!("Finished");
        }

        Ok(book)
    }
}

impl Environment {
    fn resolve(&self, content: &mut Pages<'_>) {
        self.validate();

        let page_paths = &content.paths(&self.root_dir);

        let ticker = ticker!(Level::INFO, "process", "processing links").entered();

        for (base, link) in content.links_mut() {
            let file_url = match if let Some(link) = link.link.strip_prefix('/') {
                self.vcs.root.join(link)
            } else {
                base.join(&link.link)
            } {
                Ok(url) => url,
                Err(e) => {
                    debug!("ignoring unparsable link {:?}: {e}", &*link.link);
                    link.status = LinkStatus::Ignored;
                    continue;
                }
            };

            let env = self;
            let page_url = base.as_ref();

            if let Some(book) = &env.config.book_url
                && let Some(path) = book.0.make_relative(&file_url)
                && !path.starts_with("../")
            {
                let dest = ResolveBook {
                    link,
                    file_url,
                    page_url,
                    page_paths,
                    path,
                };
                let _span = dest.span(&ticker);
                self.resolve_book(dest);
            } else if let Some((path, hint)) = env.vcs.link.to_path(&file_url)
                && let Ok(url) = env.vcs.root.join(&path)
            {
                let (_, url_suffix) = UrlSuffix::take(file_url);
                let dest = ResolveFile {
                    hint,
                    url_suffix,
                    is_vcs: true,
                    file_url: url,
                    page_url,
                    page_paths,
                    link,
                };
                let _span = dest.span(&ticker);
                self.resolve_file(dest);
            } else if file_url.scheme() == "file" {
                let (file_url, url_suffix) = UrlSuffix::take(file_url);
                let dest = ResolveFile {
                    hint: link.hint,
                    url_suffix,
                    is_vcs: false,
                    file_url,
                    page_url,
                    page_paths,
                    link,
                };
                let _span = dest.span(&ticker);
                self.resolve_file(dest);
            }
        }
    }

    fn resolve_file(
        &self,
        ResolveFile {
            file_url,
            page_url,
            page_paths,
            hint,
            url_suffix,
            is_vcs,
            link,
        }: ResolveFile,
    ) {
        if url_suffix.query.is_some() || url_suffix.fragment.is_some() {
            trace!(?url_suffix);
        }

        let relative_to_repo = match self.vcs.try_file(&file_url) {
            Ok(path) => path,
            Err(err) => {
                link.unreachable(vec![(file_url, err)]);
                return;
            }
        };

        let relative_to_book = self
            .root_dir
            .make_relative(&file_url)
            .expect("should be a file");

        trace!(?relative_to_book);

        let should_link = is_vcs.tap(|r| trace!(is_vcs = r))
            || relative_to_book
                .starts_with("../")
                .tap(|r| trace!(not_in_book = r))
            || (relative_to_book.ends_with(".md") && !page_paths.contains(&relative_to_book))
                .tap(|r| trace!(book_assets = r))
            || (self.config.always_link.iter())
                .any(|suffix| file_url.path().ends_with(suffix))
                .tap(|r| trace!(always_link = r));

        trace!(?should_link);

        if !should_link {
            if link.link.starts_with('/') {
                // mdBook doesn't support absolute paths like VS Code does
                let rewritten = page_url
                    .make_relative(&url_suffix.restored(file_url))
                    .expect("both should be file: urls");
                link.rewritten(rewritten);
            } else {
                link.unchanged();
            }
        } else {
            match self.vcs.link.to_link(&relative_to_repo.path, hint) {
                Ok(href) => {
                    link.permalink(url_suffix.restored(href).as_str().to_owned());
                }
                Err(err) => {
                    link.status = LinkStatus::Error(format!("{err}"));
                    debug!(status = ?link.status, link = ?&*link.link);
                }
            }
        }
    }

    /// Check hard-coded URLs to book content
    fn resolve_book(
        &self,
        ResolveBook {
            file_url,
            page_url,
            page_paths,
            path,
            link,
        }: ResolveBook,
    ) {
        let path = {
            let mut path = path;
            trace!(?path);
            if let Some(idx) = path.find('#') {
                path.truncate(idx);
                trace!(?path, "removing fragment");
            };
            if let Some(idx) = path.find('?') {
                path.truncate(idx);
                trace!(?path, "removing query");
            };
            trace!(?path);
            path
        };

        let mut not_found = vec![];

        let is_index = path.is_empty() || path.ends_with('/');

        trace!(is_index);

        let try_pages = {
            let path = path
                .strip_suffix(".html")
                .inspect(|_| trace!("removing .html suffix"))
                .unwrap_or(&path);
            // one does not simply avoid trailing slash issues...
            // https://github.com/slorber/trailing-slash-guide
            if is_index {
                &[
                    // enforce that index.html pages should consistently
                    // be addressed with a trailing slash
                    format!("{path}index.md"),
                    format!("{path}README.md"),
                ] as &[_]
            } else {
                &[
                    format!("{path}.md"),
                    // all major hosting providers implicitly redirect
                    // /folder to /folder/, so these are okay
                    format!("{path}/index.md"),
                    format!("{path}/README.md"),
                ]
            }
        };

        for page in try_pages {
            trace!("trying book page {page:?}");

            let file_url = (self.root_dir)
                .join(page)
                .expect("should be a valid url")
                .tap_mut(|u| u.set_query(file_url.query()))
                .tap_mut(|u| u.set_fragment(file_url.fragment()));

            if page_paths.contains(page) {
                let rewritten = page_url
                    .make_relative(&file_url)
                    .expect("both should be file: urls");
                link.rewritten(rewritten);
                return;
            }

            trace!("not found: {file_url}");
            not_found.push((file_url, PathStatus::NotInBook));
        }

        if !is_index {
            let try_file = self.root_dir.join(&path).expect("should be a valid url");

            match self.vcs.try_file(&try_file) {
                Ok(result) if !result.metadata.is_dir() => {
                    let file_url = try_file
                        .tap_mut(|u| u.set_query(file_url.query()))
                        .tap_mut(|u| u.set_fragment(file_url.fragment()));

                    let rewritten = page_url
                        .make_relative(&file_url)
                        .expect("both should be file: urls");

                    link.rewritten(rewritten);

                    return;
                }
                Ok(_) => {
                    // a directory may exist but not accessible
                    // due to having no index.html
                    not_found.push((try_file, PathStatus::NotInBook));
                }
                Err(err) => {
                    not_found.push((try_file, err));
                }
            }
        }

        link.unreachable(not_found);
    }

    #[inline]
    fn validate(&self) {
        debug_assert!(
            self.root_dir.as_str().ends_with('/'),
            "book_src should have a trailing slash, got {}",
            self.root_dir
        );
        debug_assert!(
            self.vcs.root.as_str().ends_with('/'),
            "vcs_root should have a trailing slash, got {}",
            self.vcs.root
        );
    }

    fn new(book: &PreprocessorContext) -> Result<Result<Self>> {
        let config = (book.config)
            .preprocessor(&[PREPROCESSOR_NAME, "mdbook-link-forever"])
            .inspect(emit_debug!("{:#?}"))
            .context("Failed to read preprocessor config from book.toml")?;

        let vcs = match VersionControl::try_from_git(&config, &book.config) {
            Ok(Ok(vcs)) => vcs,
            Ok(Err(err)) => return Ok(Err(err)),
            Err(err) => return Err(err),
        };

        let markdown = book.config.markdown_options();

        let root_dir = (book.root)
            .canonicalize()
            .context("Failed to locate book root")?
            .join(&book.config.book.src)
            .to_directory_url();

        Ok(Ok(Self {
            vcs,
            root_dir,
            markdown,
            config,
        }))
    }
}

struct ResolveFile<'a, 'r> {
    file_url: Url,
    page_url: &'a Url,
    page_paths: &'a HashSet<String>,
    hint: ContentHint,
    url_suffix: UrlSuffix,
    is_vcs: bool,
    link: &'a mut RelativeLink<'r>,
}

struct ResolveBook<'a, 'r> {
    file_url: Url,
    page_url: &'a Url,
    page_paths: &'a HashSet<String>,
    path: String,
    link: &'a mut RelativeLink<'r>,
}

impl<'a, 'r> ResolveFile<'a, 'r> {
    #[inline]
    fn span(&self, parent: impl Into<Option<tracing::Id>>) -> EnteredSpan {
        let Self {
            file_url,
            page_url,
            hint,
            link,
            ..
        } = self;
        if tracing::enabled!(Level::DEBUG) {
            ticker_item! {
                parent, Level::INFO, "file_link",
                %file_url, %page_url, ?hint,
                "{:?}", &*link.link
            }
        } else {
            ticker_item!(parent, Level::INFO, "file_link", "{:?}", &*link.link)
        }
        .entered()
    }
}

impl<'a, 'r> ResolveBook<'a, 'r> {
    #[inline]
    fn span(&self, parent: impl Into<Option<tracing::Id>>) -> EnteredSpan {
        let Self {
            file_url,
            page_url,
            path,
            link,
            ..
        } = self;
        if tracing::enabled!(Level::DEBUG) {
            ticker_item! {
                parent, Level::INFO, "book_link",
                %file_url, %page_url, ?path,
                "{:?}", &*link.link
            }
        } else {
            ticker_item!(parent, Level::INFO, "book_link", "{:?}", &*link.link)
        }
        .entered()
    }
}

/// Configuration for the preprocessor.
///
/// This is deserialized from book.toml.
///
/// Doc comments for attributes populate the `configuration.md` page in the docs.
#[derive(clap::Parser, Deserialize, Debug, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
struct Config {
    /// Use a custom link format for forges other than GitHub.
    ///
    /// Should be a string that contains the following placeholders that will be
    /// filled in at build time:
    ///
    /// - `{tree}` — will be `tree` if the generated URL is for a clickable link, or
    ///   `raw` if the URL is for a Markdown image
    /// - `{ref}` — the Git reference (tag or commit ID) resolved at build time
    /// - `{path}` — path to the linked file relative to repo root, without a leading `/`
    ///
    /// For example, the following configures generated links to use GitLab's format:
    ///
    /// ```toml
    /// repo-url-template = "https://gitlab.haskell.org/ghc/ghc/-/{tree}/{ref}/{path}"
    /// ```
    ///
    /// Note that information such as repo owner or name will not be filled in. If URLs to
    /// your Git forge require such items, you should hard-code them in the pattern.
    #[serde(default)]
    #[arg(long, value_name("FORMAT"), verbatim_doc_comment)]
    repo_url_template: Option<String>,

    // TODO: cannot use `output.html.site-url` because it is always `/` during `mdbook serve`
    /// Specify the canonical URL at which you deploy your book.
    ///
    /// For example:
    ///
    /// ```toml
    /// book-url = "https://me.github.io/my-awesome-crate/"
    /// ```
    ///
    /// Enables validation of hard-coded links to book pages. The preprocessor will
    /// warn you about links that are no longer valid at build time.
    #[serde(default)]
    #[arg(long, value_name("URL"), verbatim_doc_comment)]
    book_url: Option<UrlPrefix>,

    /// Convert some paths to permalinks even if they are under `src/`.
    ///
    /// By default, links to files in your book's `src/` directory will not be converted,
    /// since they are already copied to the output directory. If you want such files
    /// to always be rendered as permalinks, specify their file extensions here.
    ///
    /// For example, to use permalinks for Rust source files even if they are in the book's
    /// `src/` directory:
    ///
    /// ```toml
    /// always-link = [".rs"]
    /// ```
    #[serde(default)]
    #[arg(
        long,
        value_delimiter(','),
        value_name("EXTENSIONS"),
        verbatim_doc_comment
    )]
    always_link: Vec<String>,

    /// Exit with a non-zero status code when there are warnings.
    #[serde(default)]
    #[arg(long, value_enum, value_name("MODE"), default_value_t = Default::default())]
    fail_on_warnings: OnWarning,
}

#[derive(Clone)]
struct UrlPrefix(Url);

impl From<Url> for UrlPrefix {
    fn from(mut url: Url) -> Self {
        if !url.path().ends_with('/') {
            let path = format!("{}/", url.path());
            url.set_path(&path);
        }
        Self(url)
    }
}

impl FromStr for UrlPrefix {
    type Err = url::ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::from(s.parse::<Url>()?))
    }
}

impl<'de> Deserialize<'de> for UrlPrefix {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let url = Url::deserialize(deserializer)?;
        Ok(Self::from(url))
    }
}

impl Debug for UrlPrefix {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("UrlPrefix")
            .field(&format_args!("\"{}\"", self.0))
            .finish()
    }
}

#[must_use]
#[derive(Debug)]
struct UrlSuffix {
    query: Option<String>,
    fragment: Option<String>,
}

impl UrlSuffix {
    fn take(mut url: Url) -> (Url, Self) {
        let query = url.query().map(|s| s.to_owned());
        let fragment = url.fragment().map(|s| s.to_owned());
        url.set_query(None);
        url.set_fragment(None);
        (url, Self { query, fragment })
    }

    fn restored(self, mut url: Url) -> Url {
        let Self { query, fragment } = self;

        match (url.query(), &query) {
            (Some(_), None) => {}
            _ => url.set_query(query.as_deref()),
        }

        match (url.fragment(), &fragment) {
            (Some(_), None) => {}
            _ => url.set_fragment(fragment.as_deref()),
        }

        url
    }
}

impl std::fmt::Debug for Environment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let Self {
            vcs,
            root_dir,
            markdown,
            config: _,
        } = self;
        f.debug_struct("Environment")
            .field("root_dir", &format_args!("\"{root_dir}\""))
            .field("vcs", &vcs)
            .field("markdown", &markdown)
            .finish_non_exhaustive()
    }
}

impl std::fmt::Debug for VersionControl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let Self {
            root,
            link,
            repo: _,
        } = self;
        f.debug_struct("VersionControl")
            .field("root", &format_args!("\"{root}\""))
            .field("link", &link)
            .finish_non_exhaustive()
    }
}

static PREPROCESSOR_NAME: &str = env!("CARGO_PKG_NAME");