[][src]Function parse_hyperlinks::parser::take_hyperlink

pub fn take_hyperlink(
    i: &str
) -> IResult<&str, (Cow<'_, str>, Cow<'_, str>, Cow<'_, str>)>

Consumes the input until it finds a Markdown, RestructuredText, Asciidoc or HTML hyperlink or link reference definition. Returns Ok(remaining_input, (link_text_or_label, link_destination, link_title). The parser recognizes stand alone links and link reference definitions.

Limitations:

Link reference labels are never resolved into link text. This limitation only concerns this function and the function first_hyperlink(). All other parsers are not affected.

Very often this limitation has no effect at all. This is the case, when the link text and the link label are identical:

abc [link text/label] abc

[link text/label]: /url "title"

But in general, the link text and the link label can be different:

abc [link text][link label] abc

[link label]: /url "title"

When a link reference definition is found, the parser outputs it's link label instead of the link text, which is strictly speaking only correct when both are identical. Note, the same applies to RestructuredText's link reference definitions too.

Another limitation is that ReStructuredText's anonymous links are not supported.

Basic usage

use parse_hyperlinks::parser::take_hyperlink;
use std::borrow::Cow;

let i = r#"[a]: b 'c'
           .. _d: e
           ---[f](g 'h')---`i <j>`_---
           ---<a href="l" title="m">k</a>"#;

let (i, r) = take_hyperlink(i).unwrap();
assert_eq!(r, (Cow::from("a"), Cow::from("b"), Cow::from("c")));
let (i, r) = take_hyperlink(i).unwrap();
assert_eq!(r, (Cow::from("d"), Cow::from("e"), Cow::from("")));
let (i, r) = take_hyperlink(i).unwrap();
assert_eq!(r, (Cow::from("f"), Cow::from("g"), Cow::from("h")));
let (i, r) = take_hyperlink(i).unwrap();
assert_eq!(r, (Cow::from("i"), Cow::from("j"), Cow::from("")));
let (i, r) = take_hyperlink(i).unwrap();
assert_eq!(r, (Cow::from("k"), Cow::from("l"), Cow::from("m")));

The parser might silently consume some additional bytes after the actual finding: This happens, when directly after a finding a md_link_ref or rst_link_ref appears. These must be ignored, as they are only allowed at the beginning of a line. The skip has to happen at this moment, as the next parser does not know if the first byte it gets, is it at the beginning of a line or not.