forked from nidhidhamnani/markdown-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdownParse.java
More file actions
39 lines (38 loc) · 1.58 KB
/
MarkdownParse.java
File metadata and controls
39 lines (38 loc) · 1.58 KB
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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
public class MarkdownParse {
public static ArrayList<String> getLinks(String markdown) {
ArrayList<String> toReturn = new ArrayList<>();
// find the next [, then find the ], then find the (, then take up to
// the next )
int currentIndex = 0;
while(currentIndex < markdown.length()) {
int nextOpenBracket = markdown.indexOf("[", currentIndex);
System.out.format("%d\t%d\t%s\n", currentIndex, nextOpenBracket, toReturn);
int nextCloseBracket = markdown.indexOf("]", nextOpenBracket);
int openParen = markdown.indexOf("(", nextCloseBracket);
int closeParen = markdown.indexOf(")", openParen);
if(nextOpenBracket == -1 || nextCloseBracket == -1
|| closeParen == -1 || openParen == -1) {
return toReturn;
}
String potentialLink = markdown.substring(openParen + 1, closeParen);
if(potentialLink.indexOf(" ") == -1 && potentialLink.indexOf("\n") == -1) {
toReturn.add(potentialLink);
currentIndex = closeParen + 1;
}
else {
currentIndex = currentIndex + 1;
}
}
return toReturn;
}
public static void main(String[] args) throws IOException {
Path fileName = Path.of(args[0]);
String contents = Files.readString(fileName);
ArrayList<String> links = getLinks(contents);
System.out.println(links);
}
}