diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml new file mode 100644 index 000000000..8a4f83500 --- /dev/null +++ b/.github/workflows/github-actions-demo.yml @@ -0,0 +1,17 @@ +name: GitHub Actions Demo +on: + push: + branches: [main] + pull_request: + branches: [main] + workflow_dispatch: +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run JUnit Tests + run: | + javac -cp .:lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar MarkdownParseTest.java + java -cp .:lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar org.junit.runner.JUnitCore MarkdownParseTest + diff --git a/.gitignore b/.gitignore index a1c2a238a..9ca780152 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ .mtj.tmp/ # Package Files # -*.jar *.war *.nar *.ear diff --git a/MarkdownParse.java b/MarkdownParse.java index 5ebf83aba..6d00e4168 100644 --- a/MarkdownParse.java +++ b/MarkdownParse.java @@ -12,13 +12,42 @@ public static ArrayList getLinks(String markdown) { // 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 openBracket = markdown.indexOf("[", currentIndex); + if (openBracket == -1) { + break; + } + + //Checks if there is an "!" that denotes an image link before the open bracket and ends the loop if there is. + + if (openBracket != 0) { + if (markdown.substring(openBracket - 1, openBracket).equals("!")) { + currentIndex = openBracket + 1; + continue; + } + } + int closeBracket = markdown.indexOf("]", openBracket); + if (closeBracket == -1) { + break; + } int openParen = markdown.indexOf("(", closeBracket); + + if (openParen == -1 || openParen != closeBracket + 1) { + break; + } + int closeParen = markdown.indexOf(")", openParen); + + + if (closeParen == -1) { + break; + } + toReturn.add(markdown.substring(openParen + 1, closeParen)); + currentIndex = closeParen + 1; } + //41, 67 return toReturn; } diff --git a/MarkdownParseTest.java b/MarkdownParseTest.java new file mode 100644 index 000000000..c0471a2db --- /dev/null +++ b/MarkdownParseTest.java @@ -0,0 +1,108 @@ +import java.util.ArrayList; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Files; +import static org.junit.Assert.*; +import org.junit.*; +import java.util.List; + +public class MarkdownParseTest { + @Test + public void addition() { + assertEquals(2, 1 + 1); + } + @Test + public void testFile() throws IOException { + Path fileName = Path.of("./test-file.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of("https://something.com", "some-thing.html"), links); + } + + @Test + public void testFile2() throws IOException { + Path fileName = Path.of("./test-file2.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of("https://something.com", "some-thing.html"), links); + } + + @Test + public void testFile3() throws IOException { + Path fileName = Path.of("./test-file3.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of("https://something.com", "some-thing.html"), links); + } + + @Test + public void testFile4() throws IOException { + Path fileName = Path.of("./test-file4.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of("https://something.com", "some-thing.html"), links); + } + + @Test + public void testFiles2() throws IOException { + Path fileName = Path.of("./testfiles/test-file2.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of("https://something.com", "some-page.html"), links); + } + + @Test + public void testFiles3() throws IOException { + Path fileName = Path.of("./testfiles/test-file3.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of(), links); + } + + @Test + public void testFiles4() throws IOException { + Path fileName = Path.of("./testfiles/test-file4.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of(), links); + } + + @Test + public void testFiles5() throws IOException { + Path fileName = Path.of("./testfiles/test-file5.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of(), links); + } + + @Test + public void testFiles6() throws IOException { + Path fileName = Path.of("./testfiles/test-file6.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of(), links); + } + + @Test + public void testFiles7() throws IOException { + Path fileName = Path.of("./testfiles/test-file7.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of(), links); + } + + @Test + public void testFiles8() throws IOException { + Path fileName = Path.of("./testfiles/test-file8.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of("a link on the first line"), links); + } + @Test + public void fileNotHere() throws IOException { + Path fileName = Path.of("./testfiles/test-file9.md"); + String content = Files.readString(fileName); + ArrayList links = MarkdownParse.getLinks(content); + assertEquals(List.of(), links); + } +} diff --git a/lib/hamcrest-core-1.3.jar b/lib/hamcrest-core-1.3.jar new file mode 100644 index 000000000..9d5fe16e3 Binary files /dev/null and b/lib/hamcrest-core-1.3.jar differ diff --git a/lib/junit-4.13.2.jar b/lib/junit-4.13.2.jar new file mode 100644 index 000000000..6da55d8b8 Binary files /dev/null and b/lib/junit-4.13.2.jar differ diff --git a/test-file2.md b/test-file2.md new file mode 100644 index 000000000..0db6d3e51 --- /dev/null +++ b/test-file2.md @@ -0,0 +1,5 @@ +#Title + +![image](invalid) +[link1](https://something.com) +[link2](some-thing.html) salad \ No newline at end of file diff --git a/test-file3.md b/test-file3.md new file mode 100644 index 000000000..f7da278df --- /dev/null +++ b/test-file3.md @@ -0,0 +1,4 @@ +# Title + +[link1](https://something.com) +[link2](some-thing.html) [ n diff --git a/test-file4.md b/test-file4.md new file mode 100644 index 000000000..29bf3f92a --- /dev/null +++ b/test-file4.md @@ -0,0 +1,7 @@ +# Title + +[link1](https://something.com) +[link2](some-thing.html) +[ + +](onomotopoeia \ No newline at end of file diff --git a/testfiles/test-file2.md b/testfiles/test-file2.md new file mode 100644 index 000000000..24fac4add --- /dev/null +++ b/testfiles/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/testfiles/test-file3.md b/testfiles/test-file3.md new file mode 100644 index 000000000..031b99a6d --- /dev/null +++ b/testfiles/test-file3.md @@ -0,0 +1,5 @@ +# title + +[] + +more text here \ No newline at end of file diff --git a/testfiles/test-file4.md b/testfiles/test-file4.md new file mode 100644 index 000000000..083728b57 --- /dev/null +++ b/testfiles/test-file4.md @@ -0,0 +1,3 @@ +# title + +[]link goes here! diff --git a/testfiles/test-file5.md b/testfiles/test-file5.md new file mode 100644 index 000000000..03d95aa8f --- /dev/null +++ b/testfiles/test-file5.md @@ -0,0 +1,7 @@ +# title + +[stuff] + +paragraph + +(page.com) \ No newline at end of file diff --git a/testfiles/test-file6.md b/testfiles/test-file6.md new file mode 100644 index 000000000..18e1f6ff3 --- /dev/null +++ b/testfiles/test-file6.md @@ -0,0 +1,3 @@ +# title + +![link](page.com) \ No newline at end of file diff --git a/testfiles/test-file7.md b/testfiles/test-file7.md new file mode 100644 index 000000000..23f9c68a7 --- /dev/null +++ b/testfiles/test-file7.md @@ -0,0 +1 @@ +)[ \ No newline at end of file diff --git a/testfiles/test-file8.md b/testfiles/test-file8.md new file mode 100644 index 000000000..8ed07127a --- /dev/null +++ b/testfiles/test-file8.md @@ -0,0 +1,2 @@ +[](a link on the first line) +[ \ No newline at end of file diff --git a/testfiles/test-file9.md b/testfiles/test-file9.md new file mode 100644 index 000000000..e69de29bb