diff --git a/MarkdownParse.java b/MarkdownParse.java index 5ebf83aba..3fc184075 100644 --- a/MarkdownParse.java +++ b/MarkdownParse.java @@ -13,9 +13,19 @@ public static ArrayList getLinks(String markdown) { int currentIndex = 0; while(currentIndex < markdown.length()) { int openBracket = markdown.indexOf("[", currentIndex); + if (markdown.substring(openBracket-1, openBracket+1).equals("![")) { + int closeParen = markdown.indexOf(")", openBracket); + currentIndex = closeParen + 1; + continue; + } int closeBracket = markdown.indexOf("]", openBracket); + if (closeBracket < 0 ) break; int openParen = markdown.indexOf("(", closeBracket); int closeParen = markdown.indexOf(")", openParen); + if (openParen+1 == closeParen) { + currentIndex = closeParen + 1; + continue; + } toReturn.add(markdown.substring(openParen + 1, closeParen)); currentIndex = closeParen + 1; } diff --git a/MarkdownParseTest.java b/MarkdownParseTest.java new file mode 100644 index 000000000..1daa47690 --- /dev/null +++ b/MarkdownParseTest.java @@ -0,0 +1,58 @@ + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + + +import org.junit.*; + +public class MarkdownParseTest { + + @Test + public void addition() { + // “assertEquals()” has two arguments. Will pass if the first arguement + // is equal to the second + assertEquals(2, 1 + 1); + } + + @Test + public void testParse() throws IOException { + List exp = List.of("https://something.com", "some-thing.html"); + Path file = Path.of("test-file.md"); + String content = Files.readString(file); + ArrayList act = MarkdownParse.getLinks(content); + assertEquals(exp, act); + } + + @Test + public void testImage() throws IOException { + ArrayList exp = new ArrayList(); + Path file = Path.of("testImage.md"); + String content = Files.readString(file); + ArrayList act = MarkdownParse.getLinks(content); + assertEquals(exp, act); + } + + @Test + public void testParen() throws IOException { + List exp = List.of("https://angeliazddl.github.io/markdown-parser/"); + Path file = Path.of("testParen.md"); + String content = Files.readString(file); + ArrayList act = MarkdownParse.getLinks(content); + assertEquals(exp, act); + } + + @Test + public void testBracket() throws IOException { + List exp = List.of("https://angeliazddl.github.io/markdown-parser/"); + Path file = Path.of("testBracket.md"); + String content = Files.readString(file); + ArrayList act = MarkdownParse.getLinks(content); + assertEquals(exp, act); + } + +} diff --git a/_config.yml b/_config.yml new file mode 100644 index 000000000..b84971359 --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-leap-day \ No newline at end of file diff --git a/test-file.md b/test-file.md index 0f9fbc913..40760ea0d 100644 --- a/test-file.md +++ b/test-file.md @@ -1,4 +1,4 @@ # Title [link1](https://something.com) -[link2](some-thing.html) +[link2](some-thing.html) \ No newline at end of file diff --git a/testBracket.md b/testBracket.md new file mode 100644 index 000000000..a22400749 --- /dev/null +++ b/testBracket.md @@ -0,0 +1,3 @@ +# CSE15L + +[back](https://angeliazddl.github.io/markdown-parser/) [ \ No newline at end of file diff --git a/testImage.md b/testImage.md new file mode 100644 index 000000000..1328e90af --- /dev/null +++ b/testImage.md @@ -0,0 +1,3 @@ +# CSE15L + +![Sunset](sunset.jpg) \ No newline at end of file diff --git a/testParen.md b/testParen.md new file mode 100644 index 000000000..72092fbad --- /dev/null +++ b/testParen.md @@ -0,0 +1,5 @@ +# CSE15L + +[link]() + +[back](https://angeliazddl.github.io/markdown-parser/) \ No newline at end of file