pub(crate) fn extract_markdown(input: &str, include_verbatim: bool) -> Vec<RawUri> {
    // In some cases it is undesirable to extract links from within code blocks,
    // which is why we keep track of entries and exits while traversing the input.
    let mut inside_code_block = false;

    let parser = Parser::new(input);
    parser
        .filter_map(|event| match event {
            // A link. The first field is the link type, the second the destination URL and the third is a title.
            Event::Start(Tag::Link(_, uri, _)) => {
                Some(vec![RawUri {
                    text: uri.to_string(),
                    // Emulate `<a href="...">` tag here to be compatible with
                    // HTML links. We might consider using the actual Markdown
                    // `LinkType` for better granularity in the future
                    element: Some("a".to_string()),
                    attribute: Some("href".to_string()),
                }])
            }
            // An image. The first field is the link type, the second the destination URL and the third is a title.
            Event::Start(Tag::Image(_, uri, _)) => {
                Some(vec![RawUri {
                    text: uri.to_string(),
                    // Emulate `<img src="...">` tag here to be compatible with
                    // HTML links. We might consider using the actual Markdown
                    // `LinkType` for better granularity in the future
                    element: Some("img".to_string()),
                    attribute: Some("src".to_string()),
                }])
            }
            // A code block (inline or fenced).
            Event::Start(Tag::CodeBlock(_)) => {
                inside_code_block = true;
                None
            }
            Event::End(Tag::CodeBlock(_)) => {
                inside_code_block = false;
                None
            }

            // A text node.
            Event::Text(txt) => {
                if inside_code_block && !include_verbatim {
                    None
                } else {
                    Some(extract_plaintext(&txt))
                }
            }

            // An HTML node
            Event::Html(html) => {
                // This won't exclude verbatim links right now, because HTML gets passed in chunks
                // by pulldown_cmark. So excluding `<pre>` and `<code>` is not handled right now.
                Some(extract_html(&html.to_string(), include_verbatim))
            }

            // An inline code node.
            Event::Code(code) => {
                if include_verbatim {
                    Some(extract_plaintext(&code))
                } else {
                    None
                }
            }

            // Silently skip over other events
            _ => None,
        })
        .flatten()
        .collect()
}
/// Extract unparsed URL strings from a Markdown string.
Extract unparsed URL strings from a Markdown string.
Extract unparsed URL strings from a Markdown string .
pub(crate) fn extract_markdown(input: &str, include_verbatim: bool) -> Vec<RawUri> {
    // In some cases it is undesirable to extract links from within code blocks,
    // which is why we keep track of entries and exits while traversing the input.
    let mut inside_code_block = false;
    let parser = Parser::new(input);
    parser
        .filter_map(|event| match event {
            // A link. The first field is the link type, the second the destination URL and the third is a title.
            Event::Start(Tag::Link(_, uri, _)) => {
                Some(vec![RawUri {
                    text: uri.to_string(),
                    // Emulate `<a href="...">` tag here to be compatible with
                    // HTML links. We might consider using the actual Markdown
                    // `LinkType` for better granularity in the future
                    element: Some("a".to_string()),
                    attribute: Some("href".to_string()),
                }])
            }
            // An image. The first field is the link type, the second the destination URL and the third is a title.
            Event::Start(Tag::Image(_, uri, _)) => {
                Some(vec![RawUri {
                    text: uri.to_string(),
                    // Emulate `<img src="...">` tag here to be compatible with
                    // HTML links. We might consider using the actual Markdown
                    // `LinkType` for better granularity in the future
                    element: Some("img".to_string()),
                    attribute: Some("src".to_string()),
                }])
            }
            // A code block (inline or fenced).
            Event::Start(Tag::CodeBlock(_)) => {
                inside_code_block = true;
                None
            }
            Event::End(Tag::CodeBlock(_)) => {
                inside_code_block = false;
                None
            }
            // A text node.
            Event::Text(txt) => {
                if inside_code_block && !include_verbatim {
                    None
                } else {
                    Some(extract_plaintext(&txt))
                }
            }
            // An HTML node
            Event::Html(html) => {
                // This won't exclude verbatim links right now, because HTML gets passed in chunks
                // by pulldown_cmark. So excluding `<pre>` and `<code>` is not handled right now.
                Some(extract_html(&html.to_string(), include_verbatim))
            }
            // An inline code node.
            Event::Code(code) => {
                if include_verbatim {
                    Some(extract_plaintext(&code))
                } else {
                    None
                }
            }
            // Silently skip over other events
            _ => None,
        })
        .flatten()
        .collect()
}
pub ( crate ) fn extract_markdown ( input : & str , include_verbatim : bool ) -> Vec < RawUri > { // In some cases it is undesirable to extract links from within code blocks, // which is why we keep track of entries and exits while traversing the input. let mut inside_code_block = false ; let parser = Parser :: new ( input ) ; parser . filter_map ( | event | match event { // A link. The first field is the link type, the second the destination URL and the third is a title. Event :: Start ( Tag :: Link ( _ , uri , _ ) ) => { Some ( vec ! [ RawUri { text uri to_string ( ) // Emulate `<a href="...">` tag here to be compatible with // HTML links. We might consider using the actual Markdown // `LinkType` for better granularity in the future element Some ( " " to_string ( ) ) attribute Some ( " " to_string ( ) ) } ] ) } // An image. The first field is the link type, the second the destination URL and the third is a title. Event :: Start ( Tag :: Image ( _ , uri , _ ) ) => { Some ( vec ! [ RawUri { text uri to_string ( ) // Emulate `<img src="...">` tag here to be compatible with // HTML links. We might consider using the actual Markdown // `LinkType` for better granularity in the future element Some ( " " to_string ( ) ) attribute Some ( " " to_string ( ) ) } ] ) } // A code block (inline or fenced). Event :: Start ( Tag :: CodeBlock ( _ ) ) => { inside_code_block = true ; None } Event :: End ( Tag :: CodeBlock ( _ ) ) => { inside_code_block = false ; None } // A text node. Event :: Text ( txt ) => { if inside_code_block && ! include_verbatim { None } else { Some ( extract_plaintext ( & txt ) ) } } // An HTML node Event :: Html ( html ) => { // This won't exclude verbatim links right now, because HTML gets passed in chunks // by pulldown_cmark. So excluding `<pre>` and `<code>` is not handled right now. Some ( extract_html ( & html . to_string ( ) , include_verbatim ) ) } // An inline code node. Event :: Code ( code ) => { if include_verbatim { Some ( extract_plaintext ( & code ) ) } else { None } } // Silently skip over other events _ => None , } ) . flatten ( ) . collect ( ) }
Extract unparsed URL strings from a Markdown string.
Extract unparsed URL strings from a Markdown string .
input
&str
include_verbatim
bool