toolkit for mdBook [mirror of my GitHub repo] docs.tonywu.dev/mdbookkit/
permalinks rust-analyzer mdbook
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(permalinks): preserve url query and fragment when converting path

Tony Wu c08108f0 3030d012

+65 -13
+39 -2
crates/mdbook-permalinks/src/main.rs
··· 238 238 file_url 239 239 }; 240 240 241 + let (file_url, suffix) = UrlSuffix::take(file_url); 242 + 241 243 let Ok(path) = file_url.to_file_path() else { 242 244 link.status = LinkStatus::Ignored; 243 245 return; ··· 289 291 if !always_link { 290 292 if link.link.starts_with('/') { 291 293 // mdbook doesn't support absolute paths like VS Code does 292 - link.link = page_url.make_relative(&file_url).unwrap().into(); 294 + link.link = page_url 295 + .make_relative(&suffix.restored(file_url)) 296 + .unwrap() 297 + .into(); 293 298 link.status = LinkStatus::Rewritten; 294 299 } else { 295 300 link.status = LinkStatus::Published; ··· 299 304 300 305 match env.vcs.link.to_link(&relative_to_repo) { 301 306 Ok(href) => { 307 + link.link = suffix.restored(href).as_str().to_owned().into(); 302 308 link.status = LinkStatus::Permalink; 303 - link.link = href.as_str().to_owned().into(); 304 309 } 305 310 Err(err) => link.status = LinkStatus::Error(format!("{err}")), 306 311 } ··· 512 517 { 513 518 let url = Url::deserialize(deserializer)?; 514 519 Ok(Self::from(url)) 520 + } 521 + } 522 + 523 + #[must_use] 524 + struct UrlSuffix { 525 + query: Option<String>, 526 + fragment: Option<String>, 527 + } 528 + 529 + impl UrlSuffix { 530 + fn take(mut url: Url) -> (Url, Self) { 531 + let query = url.query().map(|s| s.to_owned()); 532 + let fragment = url.fragment().map(|s| s.to_owned()); 533 + url.set_query(None); 534 + url.set_fragment(None); 535 + (url, Self { query, fragment }) 536 + } 537 + 538 + fn restored(self, mut url: Url) -> Url { 539 + let Self { query, fragment } = self; 540 + 541 + match (url.query(), &query) { 542 + (Some(_), None) => {} 543 + _ => url.set_query(query.as_deref()), 544 + } 545 + 546 + match (url.fragment(), &fragment) { 547 + (Some(_), None) => {} 548 + _ => url.set_fragment(fragment.as_deref()), 549 + } 550 + 551 + url 515 552 } 516 553 } 517 554
+5 -1
crates/mdbook-permalinks/src/tests/links.md
··· 2 2 3 3 [Cargo.toml](../../../../Cargo.toml) 4 4 5 - ![error reporting](../../../../docs/src/rustdoc-link/media/error-reporting.png) 5 + ![error reporting](../../../../docs/src/permalinks/media/error-reporting.png) 6 6 7 7 # absolute paths 8 8 9 9 [LICENSE-APACHE.md](/LICENSE-APACHE.md) 10 10 11 11 [links.md](/crates/mdbook-permalinks/src/tests/links.md#absolute-paths) 12 + 13 + # fragments 14 + 15 + [book.toml](/docs/book.toml#L40-44) 12 16 13 17 # book files 14 18
+1 -1
crates/mdbook-permalinks/src/tests/snaps/_stderr.ignored.snap
··· 4 4 expression: report 5 5 --- 6 6 info: link is ignored as it is not supported 7 - ╭─[crates/mdbook-permalinks/src/tests/links.md:37:10] 7 + ╭─[crates/mdbook-permalinks/src/tests/links.md:41:10] 8 8 9 9 │ ignored: <https://example.com/book/links> 10 10 · ────────────────────────────────
+1 -1
crates/mdbook-permalinks/src/tests/snaps/_stderr.no-such-path.snap
··· 4 4 expression: report 5 5 --- 6 6 warning: file does not exist at path 7 - ╭─[crates/mdbook-permalinks/src/tests/links.md:23:1] 7 + ╭─[crates/mdbook-permalinks/src/tests/links.md:27:1] 8 8 9 9 │ [Cargo.lock](../../Cargo.lock) 10 10 · ───────────────┬──────────────
+1 -1
crates/mdbook-permalinks/src/tests/snaps/_stderr.not-checked-in.snap
··· 4 4 expression: report 5 5 --- 6 6 warning: path to a file outside source control 7 - ╭─[crates/mdbook-permalinks/src/tests/links.md:25:1] 7 + ╭─[crates/mdbook-permalinks/src/tests/links.md:29:1] 8 8 9 9 │ [`//LICENSE-MIT.md`](//LICENSE-MIT.md) 10 10 · ───────────────────┬──────────────────
+11 -4
crates/mdbook-permalinks/src/tests/snaps/_stderr.permalink.snap
··· 10 10 · ──────────────────┬───────────────── 11 11 · ╰── link: https://github.com/lorem/ipsum/tree/dolor/Cargo.toml 12 12 13 - │ ![error reporting](../../../../docs/src/rustdoc-link/media/error-reporting.png) 14 - · ───────────────────────────────────────┬─────────────────────────────────────── 15 - · ╰── link: https://github.com/lorem/ipsum/tree/dolor/docs/src/rustdoc-link/media/error-reporting.png 13 + │ ![error reporting](../../../../docs/src/permalinks/media/error-reporting.png) 14 + · ──────────────────────────────────────┬────────────────────────────────────── 15 + · ╰── link: https://github.com/lorem/ipsum/tree/dolor/docs/src/permalinks/media/error-reporting.png 16 16 17 17 │ # absolute paths 18 18 ··· 21 21 · ╰── link: https://github.com/lorem/ipsum/tree/dolor/LICENSE-APACHE.md 22 22 23 23 ╰──── 24 - ╭─[crates/mdbook-permalinks/src/tests/links.md:47:1] 24 + ╭─[crates/mdbook-permalinks/src/tests/links.md:15:1] 25 + 26 + │ [book.toml](/docs/book.toml#L40-44) 27 + · ─────────────────┬───────────────── 28 + · ╰── link: https://github.com/lorem/ipsum/tree/dolor/docs/book.toml#L40-44 29 + 30 + ╰──── 31 + ╭─[crates/mdbook-permalinks/src/tests/links.md:51:1] 25 32 26 33 │ [permalink](https://github.com/lorem/ipsum/tree/HEAD/LICENSE-APACHE.md) 27 34 · ───────────────────────────────────┬───────────────────────────────────
+1 -1
crates/mdbook-permalinks/src/tests/snaps/_stderr.published.snap
··· 4 4 expression: report 5 5 --- 6 6 info: link to book page or file 7 - ╭─[crates/mdbook-permalinks/src/tests/links.md:15:1] 7 + ╭─[crates/mdbook-permalinks/src/tests/links.md:19:1] 8 8 9 9 │ [Links](./links.md) 10 10 · ─────────┬─────────
+1 -1
crates/mdbook-permalinks/src/tests/snaps/_stderr.rewritten.snap
··· 12 12 · │ link: #absolute-paths 13 13 14 14 ╰──── 15 - ╭─[crates/mdbook-permalinks/src/tests/links.md:31:8] 15 + ╭─[crates/mdbook-permalinks/src/tests/links.md:35:8] 16 16 17 17 │ found: <https://example.org/book/tests/links> 18 18 · ───────────────────┬──────────────────
+5 -1
crates/mdbook-permalinks/src/tests/snaps/links.snap
··· 7 7 8 8 [Cargo.toml](https://github.com/lorem/ipsum/tree/dolor/Cargo.toml) 9 9 10 - ![error reporting](https://github.com/lorem/ipsum/tree/dolor/docs/src/rustdoc-link/media/error-reporting.png) 10 + ![error reporting](https://github.com/lorem/ipsum/tree/dolor/docs/src/permalinks/media/error-reporting.png) 11 11 12 12 # absolute paths 13 13 14 14 [LICENSE-APACHE.md](https://github.com/lorem/ipsum/tree/dolor/LICENSE-APACHE.md) 15 15 16 16 [links.md](#absolute-paths) 17 + 18 + # fragments 19 + 20 + [book.toml](https://github.com/lorem/ipsum/tree/dolor/docs/book.toml#L40-44) 17 21 18 22 # book files 19 23