slug update

This commit is contained in:
aisensiy 2017-08-25 11:19:26 +08:00
parent 6e473f9838
commit aa4f40ba74
2 changed files with 40 additions and 1 deletions

View File

@ -57,6 +57,6 @@ public class Article {
}
private String toSlug(String title) {
return title.toLowerCase().replace(' ', '-');
return title.toLowerCase().replaceAll("[\\&|[\\uFE30-\\uFFA0]|\\|\\”|\\s\\?\\,\\.]+", "-");
}
}

View File

@ -0,0 +1,39 @@
package io.spring.core.article;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class ArticleTest {
@Test
public void should_get_right_slug() throws Exception {
Article article = new Article("a new title", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("a-new-title"));
}
@Test
public void should_get_right_slug_with_number_in_title() throws Exception {
Article article = new Article("a new title 2", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("a-new-title-2"));
}
@Test
public void should_get_lower_case_slug() throws Exception {
Article article = new Article("A NEW TITLE", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("a-new-title"));
}
@Test
public void should_handle_other_language() throws Exception {
Article article = new Article("中文:标题", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("中文-标题"));
}
@Test
public void should_handle_commas() throws Exception {
Article article = new Article("what?the.hell,w", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("what-the-hell-w"));
}
}