From e3bd39c9d156a4a02a8b7398ed5769100a3d877a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 4 Mar 2024 15:51:22 +0100 Subject: Add support for shortcut links in docs (#3547) --- docs/src/html.rs | 28 ++++++++++++++++++++++++++-- docs/src/lib.rs | 3 ++- 2 files changed, 28 insertions(+), 3 deletions(-) (limited to 'docs/src') diff --git a/docs/src/html.rs b/docs/src/html.rs index 912ad367..bb6204a3 100644 --- a/docs/src/html.rs +++ b/docs/src/html.rs @@ -59,9 +59,27 @@ impl Html { | md::Options::ENABLE_STRIKETHROUGH | md::Options::ENABLE_HEADING_ATTRIBUTES; + // Convert `[foo]` to `[foo]($foo)`. + let mut link = |broken: md::BrokenLink| { + assert_eq!( + broken.link_type, + md::LinkType::Shortcut, + "unsupported link type: {:?}", + broken.link_type, + ); + + Some(( + format!("${}", broken.reference.trim_matches('`')).into(), + broken.reference.into_string().into(), + )) + }; + let ids = Arena::new(); let mut handler = Handler::new(text, resolver, nesting, &ids); - let mut events = md::Parser::new_ext(text, options).peekable(); + let mut events = + md::Parser::new_with_broken_link_callback(text, options, Some(&mut link)) + .peekable(); + let iter = std::iter::from_fn(|| loop { let mut event = events.next()?; handler.peeked = events.peek().and_then(|event| match event { @@ -199,7 +217,13 @@ impl<'a> Handler<'a> { // Rewrite links. md::Event::Start(md::Tag::Link(ty, dest, _)) => { assert!( - matches!(ty, md::LinkType::Inline | md::LinkType::Reference), + matches!( + ty, + md::LinkType::Inline + | md::LinkType::Reference + | md::LinkType::ShortcutUnknown + | md::LinkType::Autolink + ), "unsupported link type: {ty:?}", ); diff --git a/docs/src/lib.rs b/docs/src/lib.rs index 9ec5b087..3856b1be 100644 --- a/docs/src/lib.rs +++ b/docs/src/lib.rs @@ -92,7 +92,8 @@ pub fn provide(resolver: &dyn Resolver) -> Vec { /// Resolve consumer dependencies. pub trait Resolver { - /// Try to resolve a link that the system cannot resolve itself. + /// Try to resolve a link. If this returns `None`, the system will try to + /// resolve the link itself. fn link(&self, link: &str) -> Option; /// Produce an URL for an image file. -- cgit v1.2.3