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
67 lines (59 loc) · 2.53 KB
/
MarkdownParse.java
File metadata and controls
67 lines (59 loc) · 2.53 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
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
import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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 read link upto next )
int currentIndex = 0;
while(currentIndex < markdown.length()) {
int openBracket = markdown.indexOf("[", currentIndex);
int closeBracket = markdown.indexOf("]", Math.max(currentIndex, openBracket));
int openParen = markdown.indexOf("(", Math.max(currentIndex, closeBracket));
int closeParen = markdown.indexOf(")", Math.max(currentIndex, openParen));
if(openBracket == -1 && closeBracket == -1 && openParen == -1 && closeParen == -1){break;}
if(openBracket == -1 || closeBracket == -1 || openParen == -1 || closeParen == -1){
int max1 = Math.max(openBracket, closeBracket);
int max2 = Math.max(openParen, closeParen);
currentIndex = Math.max(max1, max2) +1;
}else{
String s = markdown.substring(openParen+1, closeParen);
if(!(s.contains("(") || s.contains("["))){
if(openParen == closeBracket + 1){
try{
if(markdown.charAt(openBracket-1) != '!'){
toReturn.add(s);
}
}catch(IndexOutOfBoundsException e){
toReturn.add(s);
}
}
}
currentIndex = closeParen + 1;
}
}
return toReturn;
}
public static void main(String[] args) throws IOException {
Parser parser = Parser.builder().build();
Path fileName = Path.of(args[0]);
String content = Files.readString(fileName);
Node document = parser.parse(content);
LinkVisitor visitor = new LinkVisitor();
document.accept(visitor);
System.out.println(visitor.links);
}
}
class LinkVisitor extends AbstractVisitor {
public ArrayList<String> links = new ArrayList<String>();
@Override
public void visit(Link text){
links.add(text.getDestination());
visitChildren(text);
}
}