diff --git a/MarkdownParse.java b/MarkdownParse.java index 5ebf83aba..cd8a9ad9f 100644 --- a/MarkdownParse.java +++ b/MarkdownParse.java @@ -1,5 +1,3 @@ -//https://howtodoinjava.com/java/io/java-read-file-to-string-examples/ - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -13,11 +11,26 @@ public static ArrayList getLinks(String markdown) { int currentIndex = 0; while(currentIndex < markdown.length()) { int openBracket = markdown.indexOf("[", currentIndex); - int closeBracket = markdown.indexOf("]", openBracket); - int openParen = markdown.indexOf("(", closeBracket); - int closeParen = markdown.indexOf(")", openParen); + if(openBracket==-1){ + break; + } + if(markdown.substring(currentIndex,openBracket).contains("!")){ + int closeBracket = markdown.indexOf("]",openBracket); + int openParen = markdown.indexOf("(",closeBracket); + int closeParen = markdown.indexOf(")",openBracket); + currentIndex = closeParen + 1; + continue; + } + int closeBracket = markdown.indexOf("]",openBracket); + int openParen = markdown.indexOf("(",closeBracket); + if(openParen==-1){ + break; + } + int closeParen = markdown.indexOf(")",openBracket); + toReturn.add(markdown.substring(openParen + 1, closeParen)); currentIndex = closeParen + 1; + } return toReturn; @@ -29,5 +42,6 @@ public static void main(String[] args) throws IOException { String content = Files.readString(fileName); ArrayList links = getLinks(content); System.out.println(links); + } -} +} \ No newline at end of file diff --git a/MarkdownParseTest.java b/MarkdownParseTest.java new file mode 100644 index 000000000..376d59245 --- /dev/null +++ b/MarkdownParseTest.java @@ -0,0 +1,10 @@ +import static org.junit.Assert.*; +import org.junit.*; +public class MarkdownParseTest { + + @Test + public void addition() { + assertEquals(2, 1 + 1); + } + +} \ No newline at end of file diff --git a/test-file.md b/test-file.md index 0f9fbc913..42b177fd7 100644 --- a/test-file.md +++ b/test-file.md @@ -1,4 +1,4 @@ # Title [link1](https://something.com) -[link2](some-thing.html) +![image](some-thing.html) \ No newline at end of file diff --git a/test-file2.md b/test-file2.md new file mode 100644 index 000000000..24fac4add --- /dev/null +++ b/test-file2.md @@ -0,0 +1,6 @@ +# Title + +[a link!](https://something.com) +[another link!](some-page.html) + +some paragraph text after the links \ No newline at end of file diff --git a/test-file3.md b/test-file3.md new file mode 100644 index 000000000..031b99a6d --- /dev/null +++ b/test-file3.md @@ -0,0 +1,5 @@ +# title + +[] + +more text here \ No newline at end of file diff --git a/test-file4.md b/test-file4.md new file mode 100644 index 000000000..083728b57 --- /dev/null +++ b/test-file4.md @@ -0,0 +1,3 @@ +# title + +[]link goes here! diff --git a/test-file5.md b/test-file5.md new file mode 100644 index 000000000..03d95aa8f --- /dev/null +++ b/test-file5.md @@ -0,0 +1,7 @@ +# title + +[stuff] + +paragraph + +(page.com) \ No newline at end of file diff --git a/test-file6.md b/test-file6.md new file mode 100644 index 000000000..18e1f6ff3 --- /dev/null +++ b/test-file6.md @@ -0,0 +1,3 @@ +# title + +![link](page.com) \ No newline at end of file diff --git a/test-file7.md b/test-file7.md new file mode 100644 index 000000000..23f9c68a7 --- /dev/null +++ b/test-file7.md @@ -0,0 +1 @@ +)[ \ No newline at end of file diff --git a/test-file8.md b/test-file8.md new file mode 100644 index 000000000..8ed07127a --- /dev/null +++ b/test-file8.md @@ -0,0 +1,2 @@ +[](a link on the first line) +[ \ No newline at end of file diff --git a/test.md b/test.md new file mode 100644 index 000000000..1931754ad --- /dev/null +++ b/test.md @@ -0,0 +1,3 @@ +# test +[link](question(ddada).com) +![image](questionddd.com) \ No newline at end of file