diff options
| author | Laurenz <laurmaedje@gmail.com> | 2024-03-04 15:51:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-04 14:51:22 +0000 |
| commit | e3bd39c9d156a4a02a8b7398ed5769100a3d877a (patch) | |
| tree | f793037b515289f158a2b69dce3b516127c6b3fa /docs/src | |
| parent | b005dc37e5f7e2f519edc1f607f6ba3ab810ec26 (diff) | |
Add support for shortcut links in docs (#3547)
Diffstat (limited to 'docs/src')
| -rw-r--r-- | docs/src/html.rs | 28 | ||||
| -rw-r--r-- | docs/src/lib.rs | 3 |
2 files changed, 28 insertions, 3 deletions
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<PageModel> { /// 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<String>; /// Produce an URL for an image file. |
