mirror of https://github.com/halo-dev/halo
feat: unified formatted slug.
parent
020fe50be9
commit
cd2c3a17b3
|
@ -4,7 +4,6 @@ import lombok.Data;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import run.halo.app.model.dto.base.InputConverter;
|
import run.halo.app.model.dto.base.InputConverter;
|
||||||
import run.halo.app.model.entity.Category;
|
import run.halo.app.model.entity.Category;
|
||||||
import run.halo.app.utils.HaloUtils;
|
|
||||||
import run.halo.app.utils.SlugUtils;
|
import run.halo.app.utils.SlugUtils;
|
||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
@ -14,7 +13,8 @@ import javax.validation.constraints.Size;
|
||||||
* Category param.
|
* Category param.
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @date 3/21/19
|
* @author ryanwang
|
||||||
|
* @date 2019-03-21
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class CategoryParam implements InputConverter<Category> {
|
public class CategoryParam implements InputConverter<Category> {
|
||||||
|
@ -46,13 +46,8 @@ public class CategoryParam implements InputConverter<Category> {
|
||||||
@Override
|
@Override
|
||||||
public Category convertTo() {
|
public Category convertTo() {
|
||||||
// Handle default value
|
// Handle default value
|
||||||
if (StringUtils.isBlank(slugName)) {
|
|
||||||
slugName = SlugUtils.slugify(name);
|
|
||||||
|
|
||||||
if (StringUtils.isBlank(slugName)) {
|
slugName = StringUtils.isBlank(slugName) ? SlugUtils.slug(name) : SlugUtils.slug(slugName);
|
||||||
slugName = HaloUtils.initializeUrlIfBlank(slugName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return InputConverter.super.convertTo();
|
return InputConverter.super.convertTo();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
package run.halo.app.model.params;
|
package run.halo.app.model.params;
|
||||||
|
|
||||||
import cn.hutool.crypto.digest.BCrypt;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import run.halo.app.model.dto.base.InputConverter;
|
import run.halo.app.model.dto.base.InputConverter;
|
||||||
import run.halo.app.model.entity.Post;
|
import run.halo.app.model.entity.Post;
|
||||||
import run.halo.app.model.enums.PostCreateFrom;
|
import run.halo.app.model.enums.PostCreateFrom;
|
||||||
import run.halo.app.model.enums.PostStatus;
|
import run.halo.app.model.enums.PostStatus;
|
||||||
|
import run.halo.app.utils.SlugUtils;
|
||||||
|
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
import javax.validation.constraints.Size;
|
import javax.validation.constraints.Size;
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -20,7 +19,7 @@ import java.util.Set;
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
* @date 3/21/19
|
* @date 2019-03-21
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class PostParam implements InputConverter<Post> {
|
public class PostParam implements InputConverter<Post> {
|
||||||
|
@ -62,9 +61,8 @@ public class PostParam implements InputConverter<Post> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Post convertTo() {
|
public Post convertTo() {
|
||||||
if (StringUtils.isBlank(url)) {
|
url = StringUtils.isBlank(url) ? SlugUtils.slug(title) : SlugUtils.slug(url);
|
||||||
url = URLEncoder.encode(title.replace(".",""));
|
|
||||||
}
|
|
||||||
if (null == thumbnail) {
|
if (null == thumbnail) {
|
||||||
thumbnail = "";
|
thumbnail = "";
|
||||||
}
|
}
|
||||||
|
@ -74,9 +72,8 @@ public class PostParam implements InputConverter<Post> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Post post) {
|
public void update(Post post) {
|
||||||
if (StringUtils.isBlank(url)) {
|
url = StringUtils.isBlank(url) ? SlugUtils.slug(title) : SlugUtils.slug(url);
|
||||||
url = URLEncoder.encode(title.replace(".",""));
|
|
||||||
}
|
|
||||||
if (null == thumbnail) {
|
if (null == thumbnail) {
|
||||||
thumbnail = "";
|
thumbnail = "";
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
package run.halo.app.model.params;
|
package run.halo.app.model.params;
|
||||||
|
|
||||||
import cn.hutool.crypto.digest.BCrypt;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import run.halo.app.model.dto.base.InputConverter;
|
import run.halo.app.model.dto.base.InputConverter;
|
||||||
import run.halo.app.model.entity.Sheet;
|
import run.halo.app.model.entity.Sheet;
|
||||||
import run.halo.app.model.enums.PostStatus;
|
import run.halo.app.model.enums.PostStatus;
|
||||||
import run.halo.app.utils.HaloUtils;
|
import run.halo.app.utils.SlugUtils;
|
||||||
|
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
@ -18,7 +17,7 @@ import java.util.Date;
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
* @date 19-4-24
|
* @date 2019-4-24
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class SheetParam implements InputConverter<Sheet> {
|
public class SheetParam implements InputConverter<Sheet> {
|
||||||
|
@ -52,9 +51,7 @@ public class SheetParam implements InputConverter<Sheet> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Sheet convertTo() {
|
public Sheet convertTo() {
|
||||||
if (StringUtils.isBlank(url)) {
|
url = StringUtils.isBlank(url) ? SlugUtils.slug(title) : SlugUtils.slug(url);
|
||||||
url = title.replace(".","");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (null == thumbnail) {
|
if (null == thumbnail) {
|
||||||
thumbnail = "";
|
thumbnail = "";
|
||||||
|
@ -65,9 +62,7 @@ public class SheetParam implements InputConverter<Sheet> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Sheet sheet) {
|
public void update(Sheet sheet) {
|
||||||
if (StringUtils.isBlank(url)) {
|
url = StringUtils.isBlank(url) ? SlugUtils.slug(title) : SlugUtils.slug(url);
|
||||||
url = title.replace(".","");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (null == thumbnail) {
|
if (null == thumbnail) {
|
||||||
thumbnail = "";
|
thumbnail = "";
|
||||||
|
|
|
@ -4,7 +4,6 @@ import lombok.Data;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import run.halo.app.model.dto.base.InputConverter;
|
import run.halo.app.model.dto.base.InputConverter;
|
||||||
import run.halo.app.model.entity.Tag;
|
import run.halo.app.model.entity.Tag;
|
||||||
import run.halo.app.utils.HaloUtils;
|
|
||||||
import run.halo.app.utils.SlugUtils;
|
import run.halo.app.utils.SlugUtils;
|
||||||
|
|
||||||
import javax.validation.constraints.NotBlank;
|
import javax.validation.constraints.NotBlank;
|
||||||
|
@ -14,7 +13,8 @@ import javax.validation.constraints.Size;
|
||||||
* Tag param.
|
* Tag param.
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @date 3/20/19
|
* @author ryanwang
|
||||||
|
* @date 2019-03-20
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class TagParam implements InputConverter<Tag> {
|
public class TagParam implements InputConverter<Tag> {
|
||||||
|
@ -28,12 +28,8 @@ public class TagParam implements InputConverter<Tag> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Tag convertTo() {
|
public Tag convertTo() {
|
||||||
if (StringUtils.isBlank(slugName)) {
|
|
||||||
// Handle slug name
|
|
||||||
slugName = SlugUtils.slugify(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
slugName = HaloUtils.initializeUrlIfBlank(slugName);
|
slugName = StringUtils.isBlank(slugName) ? SlugUtils.slug(name) : SlugUtils.slug(slugName);
|
||||||
|
|
||||||
return InputConverter.super.convertTo();
|
return InputConverter.super.convertTo();
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ import run.halo.app.service.BackupService;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
import run.halo.app.service.PostService;
|
import run.halo.app.service.PostService;
|
||||||
import run.halo.app.service.PostTagService;
|
import run.halo.app.service.PostTagService;
|
||||||
|
import run.halo.app.utils.SlugUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -213,7 +214,7 @@ public class BackupServiceImpl implements BackupService {
|
||||||
public static String sanitizeFilename(final String unsanitized) {
|
public static String sanitizeFilename(final String unsanitized) {
|
||||||
return unsanitized.
|
return unsanitized.
|
||||||
replaceAll("[^(a-zA-Z0-9\\u4e00-\\u9fa5\\.)]", "").
|
replaceAll("[^(a-zA-Z0-9\\u4e00-\\u9fa5\\.)]", "").
|
||||||
replaceAll("[\\?\\\\/:|<>\\*\\[\\]\\(\\)\\$%\\{\\}@~]", "").
|
replaceAll("[\\?\\\\/:|<>\\*\\[\\]\\(\\)\\$%\\{\\}@~\\.]", "").
|
||||||
replaceAll("\\s", "");
|
replaceAll("\\s", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package run.halo.app.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.text.StrBuilder;
|
import cn.hutool.core.text.StrBuilder;
|
||||||
import cn.hutool.core.util.RandomUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
@ -17,6 +16,7 @@ import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import run.halo.app.event.logger.LogEvent;
|
import run.halo.app.event.logger.LogEvent;
|
||||||
|
import run.halo.app.event.post.PostSaveBeforeEvent;
|
||||||
import run.halo.app.event.post.PostVisitEvent;
|
import run.halo.app.event.post.PostVisitEvent;
|
||||||
import run.halo.app.model.dto.CategoryDTO;
|
import run.halo.app.model.dto.CategoryDTO;
|
||||||
import run.halo.app.model.dto.TagDTO;
|
import run.halo.app.model.dto.TagDTO;
|
||||||
|
@ -30,7 +30,10 @@ import run.halo.app.model.vo.PostDetailVO;
|
||||||
import run.halo.app.model.vo.PostListVO;
|
import run.halo.app.model.vo.PostListVO;
|
||||||
import run.halo.app.repository.PostRepository;
|
import run.halo.app.repository.PostRepository;
|
||||||
import run.halo.app.service.*;
|
import run.halo.app.service.*;
|
||||||
import run.halo.app.utils.*;
|
import run.halo.app.utils.DateUtils;
|
||||||
|
import run.halo.app.utils.MarkdownUtils;
|
||||||
|
import run.halo.app.utils.ServiceUtils;
|
||||||
|
import run.halo.app.utils.SlugUtils;
|
||||||
|
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
|
@ -49,6 +52,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
@Deprecated
|
||||||
public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostService {
|
public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostService {
|
||||||
|
|
||||||
private final PostRepository postRepository;
|
private final PostRepository postRepository;
|
||||||
|
@ -256,8 +260,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
if (null == tag) {
|
if (null == tag) {
|
||||||
tag = new Tag();
|
tag = new Tag();
|
||||||
tag.setName(ele);
|
tag.setName(ele);
|
||||||
String slugName = SlugUtils.slugify(ele);
|
tag.setSlugName(SlugUtils.slug(ele));
|
||||||
tag.setSlugName(HaloUtils.initializeUrlIfBlank(slugName));
|
|
||||||
tag = tagService.create(tag);
|
tag = tagService.create(tag);
|
||||||
}
|
}
|
||||||
tagIds.add(tag.getId());
|
tagIds.add(tag.getId());
|
||||||
|
@ -267,8 +270,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
if (null == category) {
|
if (null == category) {
|
||||||
category = new Category();
|
category = new Category();
|
||||||
category.setName(ele);
|
category.setName(ele);
|
||||||
String slugName = SlugUtils.slugify(ele);
|
category.setSlugName(SlugUtils.slug(ele));
|
||||||
category.setSlugName(HaloUtils.initializeUrlIfBlank(slugName));
|
|
||||||
category.setDescription(ele);
|
category.setDescription(ele);
|
||||||
category = categoryService.create(category);
|
category = categoryService.create(category);
|
||||||
}
|
}
|
||||||
|
@ -290,7 +292,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StrUtil.isEmpty(post.getUrl())) {
|
if (StrUtil.isEmpty(post.getUrl())) {
|
||||||
post.setUrl(DateUtil.format(new Date(), "yyyyMMddHHmmss" + RandomUtil.randomNumbers(5)));
|
post.setUrl(SlugUtils.slug(post.getTitle()));
|
||||||
}
|
}
|
||||||
|
|
||||||
post.setOriginalContent(markdown);
|
post.setOriginalContent(markdown);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package run.halo.app.utils;
|
package run.halo.app.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
@ -11,7 +12,8 @@ import java.util.regex.Pattern;
|
||||||
* Slugify utilities.
|
* Slugify utilities.
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @date 3/20/19
|
* @author ryanwang
|
||||||
|
* @date 2019-03-20
|
||||||
*/
|
*/
|
||||||
public class SlugUtils {
|
public class SlugUtils {
|
||||||
|
|
||||||
|
@ -25,6 +27,7 @@ public class SlugUtils {
|
||||||
* @return slug string
|
* @return slug string
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
|
@Deprecated
|
||||||
public static String slugify(@NonNull String input) {
|
public static String slugify(@NonNull String input) {
|
||||||
Assert.hasText(input, "Input string must not be blank");
|
Assert.hasText(input, "Input string must not be blank");
|
||||||
|
|
||||||
|
@ -33,4 +36,21 @@ public class SlugUtils {
|
||||||
String slug = NON_LATIN.matcher(normalized).replaceAll("");
|
String slug = NON_LATIN.matcher(normalized).replaceAll("");
|
||||||
return slug.toLowerCase(Locale.ENGLISH);
|
return slug.toLowerCase(Locale.ENGLISH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Slugify string.
|
||||||
|
*
|
||||||
|
* @param input input string must not be blank
|
||||||
|
* @return slug string
|
||||||
|
*/
|
||||||
|
public static String slug(@NonNull String input) {
|
||||||
|
Assert.hasText(input, "Input string must not be blank");
|
||||||
|
String slug = input.
|
||||||
|
replaceAll("[^(a-zA-Z0-9\\u4e00-\\u9fa5\\.\\-)]", "").
|
||||||
|
replaceAll("[\\?\\\\/:|<>\\*\\[\\]\\(\\)\\$%\\{\\}@~\\.]", "").
|
||||||
|
replaceAll("\\s", "")
|
||||||
|
.toLowerCase(Locale.ENGLISH);
|
||||||
|
return StrUtil.isNotEmpty(slug) ? slug : String.valueOf(System.currentTimeMillis());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
|
@ -18,4 +17,18 @@ public class SlugUtilsTest {
|
||||||
|
|
||||||
Assert.assertThat(slugResult, equalTo("hello-world"));
|
Assert.assertThat(slugResult, equalTo("hello-world"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void slugTest() {
|
||||||
|
String slug = SlugUtils.slug("一二三四 +/~!@#$%^&*()_+ - hello-world");
|
||||||
|
|
||||||
|
Assert.assertThat(slug, equalTo("一二三四-hello-world"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nullSlugTest() {
|
||||||
|
String slug = SlugUtils.slug("+/~!@#$%^&*()_+");
|
||||||
|
|
||||||
|
System.out.println("slug:" + slug);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue