1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use pulldown_cmark::{Event, Parser, Tag};
use crate::{extract::plaintext::extract_plaintext, types::raw_uri::RawUri};
use super::html5gum::extract_html;
/// 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()
}
#[cfg(test)]
mod tests {
use super::*;
const MD_INPUT: &str = r#"
# Test
Some link in text [here](https://foo.com)
Code:
```bash
https://bar.com/123
```
or inline like `https://bar.org` for instance.
[example](http://example.com)
"#;
#[test]
fn test_skip_verbatim() {
let expected = vec![
RawUri {
text: "https://foo.com".to_string(),
element: Some("a".to_string()),
attribute: Some("href".to_string()),
},
RawUri {
text: "http://example.com".to_string(),
element: Some("a".to_string()),
attribute: Some("href".to_string()),
},
];
let uris = extract_markdown(MD_INPUT, false);
assert_eq!(uris, expected);
}
#[test]
fn test_include_verbatim() {
let expected = vec![
RawUri {
text: "https://foo.com".to_string(),
element: Some("a".to_string()),
attribute: Some("href".to_string()),
},
RawUri {
text: "https://bar.com/123".to_string(),
element: None,
attribute: None,
},
RawUri {
text: "https://bar.org".to_string(),
element: None,
attribute: None,
},
RawUri {
text: "http://example.com".to_string(),
element: Some("a".to_string()),
attribute: Some("href".to_string()),
},
];
let uris = extract_markdown(MD_INPUT, true);
assert_eq!(uris, expected);
}
#[test]
#[ignore]
fn test_skip_verbatim_html() {
let input = "
<code>
http://link.com
</code>
<pre>
Some pre-formatted http://pre.com
</pre>";
let expected = vec![];
let uris = extract_markdown(input, false);
assert_eq!(uris, expected);
}
}