File tree 2 files changed +28
-9
lines changed
src/test/java/org/nibor/autolink
2 files changed +28
-9
lines changed Original file line number Diff line number Diff line change @@ -53,16 +53,17 @@ Extracting links:
53
53
``` java
54
54
import org.nibor.autolink.* ;
55
55
56
- String input = " wow, so example: http ://test.com" ;
57
- LinkExtractor linkExtractor = LinkExtractor . builder()
58
- .linkTypes(EnumSet . of(LinkType . URL, LinkType . WWW , LinkType . EMAIL ))
56
+ var input = " two links: https ://test.com and https://example .com" ;
57
+ var linkExtractor = LinkExtractor . builder()
58
+ .linkTypes(EnumSet . of(LinkType . URL)) // limit to URLs
59
59
.build();
60
- Iterable<LinkSpan > links = linkExtractor. extractLinks(input);
61
- LinkSpan link = links. iterator(). next();
62
- link. getType(); // LinkType.URL
63
- link. getBeginIndex(); // 17
64
- link. getEndIndex(); // 32
65
- input. substring(link. getBeginIndex(), link. getEndIndex()); // "http://test.com"
60
+ var links = new ArrayList<> ();
61
+ for (var span : linkExtractor. extractLinks(input)) {
62
+ var link = input. substring(span. getBeginIndex(), span. getEndIndex());
63
+ links. add(link);
64
+ }
65
+
66
+ links; // List.of("https://test.com", "https://example.com")
66
67
```
67
68
68
69
Note that by default all supported types of links are extracted. If
Original file line number Diff line number Diff line change 2
2
3
3
import org .junit .Test ;
4
4
5
+ import java .util .ArrayList ;
5
6
import java .util .EnumSet ;
7
+ import java .util .List ;
8
+ import java .util .stream .Stream ;
6
9
7
10
import static org .junit .Assert .assertEquals ;
8
11
@@ -35,6 +38,21 @@ public void linkify() {
35
38
assertEquals ("wow <a href=\" http://test.com\" >http://test.com</a> such linked" , sb .toString ());
36
39
}
37
40
41
+ @ Test
42
+ public void extractLinks () {
43
+ var input = "hey https://test.com and https://example.com" ;
44
+ var linkExtractor = LinkExtractor .builder ()
45
+ .linkTypes (EnumSet .of (LinkType .URL ))
46
+ .build ();
47
+ var links = new ArrayList <>();
48
+ for (var linkSpan : linkExtractor .extractLinks (input )) {
49
+ var link = input .substring (linkSpan .getBeginIndex (), linkSpan .getEndIndex ());
50
+ links .add (link );
51
+ }
52
+
53
+ assertEquals (List .of ("https://test.com" , "https://example.com" ), links );
54
+ }
55
+
38
56
// Mocked here to not have to depend on owasp-java-encoder in tests
39
57
private static class Encode {
40
58
public static String forHtmlAttribute (String text ) {
You can’t perform that action at this time.
0 commit comments