Skip to content

Commit 0fa40f4

Browse files
committed
MarkdownConverter
1 parent 3cb3946 commit 0fa40f4

4 files changed

Lines changed: 32 additions & 5 deletions

File tree

src/main/java/alexp/blog/model/Comment.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package alexp.blog.model;
22

3-
import com.github.rjeschke.txtmark.Processor;
3+
import alexp.blog.service.MarkdownConverter;
44
import org.hibernate.validator.constraints.NotBlank;
55

66
import javax.persistence.*;
@@ -44,7 +44,7 @@ public String getCommentText() {
4444
}
4545

4646
public String getCommentTextHtml() {
47-
return Processor.process(getCommentText(), true);
47+
return MarkdownConverter.toHtml(getCommentText());
4848
}
4949

5050
public Date getDateTime() {

src/main/java/alexp/blog/model/Post.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package alexp.blog.model;
22

3-
import com.github.rjeschke.txtmark.Processor;
3+
import alexp.blog.service.MarkdownConverter;
44
import org.hibernate.validator.constraints.NotBlank;
55
import org.springframework.util.StringUtils;
66

@@ -60,11 +60,11 @@ public boolean hasShortTextPart() {
6060
}
6161

6262
public String shortTextPartHtml() {
63-
return Processor.process(getShortTextPart(), true);
63+
return MarkdownConverter.toHtml(getShortTextPart());
6464
}
6565

6666
public String fullPostTextHtml() {
67-
return Processor.process(getFullPostText().replace(shortPartSeparator(), ""), true);
67+
return MarkdownConverter.toHtml(getFullPostText().replace(shortPartSeparator(), ""));
6868
}
6969

7070
public Long getId() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package alexp.blog.service;
2+
3+
import com.github.rjeschke.txtmark.Processor;
4+
5+
public class MarkdownConverter {
6+
7+
public static String toHtml(String input) {
8+
return Processor.process(input, true);
9+
}
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package alexp.blog.service;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.*;
6+
import static org.hamcrest.MatcherAssert.assertThat;
7+
8+
public class MarkdownConverterTest {
9+
10+
@Test
11+
public void shouldReturnHtmlAndEscapeScript() {
12+
String html = MarkdownConverter.toHtml("hello **world**! <script>alert('pwnd');</script>");
13+
14+
assertThat(html, containsString("<strong>"));
15+
assertThat(html, not(containsString("<script>")));
16+
}
17+
}

0 commit comments

Comments
 (0)