Return post summary for listing

pull/146/head
johnniang 2019-05-06 19:01:18 +08:00
parent 1e15f21e88
commit 30f21c50ab
4 changed files with 66 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import run.halo.app.exception.AlreadyExistsException;
@ -17,7 +18,9 @@ import run.halo.app.model.dto.post.BasePostMinimalDTO;
import run.halo.app.model.dto.post.BasePostSimpleDTO;
import run.halo.app.model.entity.BasePost;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.properties.PostProperties;
import run.halo.app.repository.base.BasePostRepository;
import run.halo.app.service.OptionService;
import run.halo.app.service.base.AbstractCrudService;
import run.halo.app.service.base.BasePostService;
import run.halo.app.utils.DateUtils;
@ -44,9 +47,13 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
private final BasePostRepository<POST> basePostRepository;
public BasePostServiceImpl(BasePostRepository<POST> basePostRepository) {
private final OptionService optionService;
public BasePostServiceImpl(BasePostRepository<POST> basePostRepository,
OptionService optionService) {
super(basePostRepository);
this.basePostRepository = basePostRepository;
this.optionService = optionService;
}
@Override
@ -264,7 +271,14 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
public BasePostSimpleDTO convertToSimple(POST post) {
Assert.notNull(post, "Post must not be null");
return new BasePostSimpleDTO().convertFrom(post);
BasePostSimpleDTO basePostSimpleDTO = new BasePostSimpleDTO().convertFrom(post);
// Set summary
if (StringUtils.isBlank(basePostSimpleDTO.getSummary())) {
basePostSimpleDTO.setSummary(convertToSummary(post.getOriginalContent()));
}
return basePostSimpleDTO;
}
@Override
@ -331,4 +345,17 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
throw new AlreadyExistsException("The post url " + post.getUrl() + " has been exist");
}
}
@NonNull
protected String convertToSummary(@Nullable String markdownContent) {
// Render text content
String textContent = MarkdownUtils.renderText(markdownContent);
// Get summary length
Integer summaryLength = optionService.getByPropertyOrDefault(PostProperties.SUMMARY_LENGTH, Integer.class, 150);
// Set summary
return StringUtils.substring(textContent, 0, summaryLength);
}
}

View File

@ -67,8 +67,9 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
PostTagService postTagService,
PostCategoryService postCategoryService,
PostCommentService postCommentService,
ApplicationEventPublisher eventPublisher) {
super(postRepository);
ApplicationEventPublisher eventPublisher,
OptionService optionService) {
super(postRepository, optionService);
this.postRepository = postRepository;
this.tagService = tagService;
this.categoryService = categoryService;
@ -333,6 +334,11 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
return postPage.map(post -> {
PostListVO postListVO = new PostListVO().convertFrom(post);
if (StringUtils.isBlank(postListVO.getSummary())) {
// Set summary
postListVO.setSummary(convertToSummary(post.getOriginalContent()));
}
Optional.ofNullable(tagListMap.get(post.getId())).orElseGet(LinkedList::new);
// Set tags

View File

@ -10,6 +10,7 @@ import run.halo.app.model.entity.Sheet;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.SheetListVO;
import run.halo.app.repository.SheetRepository;
import run.halo.app.service.OptionService;
import run.halo.app.service.SheetCommentService;
import run.halo.app.service.SheetService;
import run.halo.app.utils.ServiceUtils;
@ -35,8 +36,9 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet> implements Shee
public SheetServiceImpl(SheetRepository sheetRepository,
ApplicationEventPublisher eventPublisher,
SheetCommentService sheetCommentService) {
super(sheetRepository);
SheetCommentService sheetCommentService,
OptionService optionService) {
super(sheetRepository, optionService);
this.sheetRepository = sheetRepository;
this.eventPublisher = eventPublisher;
this.sheetCommentService = sheetCommentService;

View File

@ -1,6 +1,6 @@
package run.halo.app.utils;
import run.halo.app.model.support.HaloConst;
import org.apache.commons.lang3.StringUtils;
import org.commonmark.Extension;
import org.commonmark.ext.front.matter.YamlFrontMatterExtension;
import org.commonmark.ext.front.matter.YamlFrontMatterVisitor;
@ -8,6 +8,10 @@ import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.renderer.text.TextContentRenderer;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import run.halo.app.model.support.HaloConst;
import java.util.Collections;
import java.util.List;
@ -42,6 +46,11 @@ public class MarkdownUtils {
*/
private static final HtmlRenderer RENDERER = HtmlRenderer.builder().extensions(EXTENSIONS_YAML).extensions(EXTENSIONS_TABLE).build();
/**
* Render text content
*/
private static final TextContentRenderer TEXT_CONTENT_RENDERER = TextContentRenderer.builder().extensions(EXTENSIONS_YAML).extensions(EXTENSIONS_TABLE).build();
/**
* Render Markdown content
*
@ -67,6 +76,21 @@ public class MarkdownUtils {
return renderContent;
}
/**
* Render text content.
*
* @param markdownContent markdown content
* @return text content or empty string if markdown content is blank
*/
@NonNull
public static String renderText(@Nullable String markdownContent) {
if (StringUtils.isBlank(markdownContent)) {
return "";
}
return TEXT_CONTENT_RENDERER.render(PARSER.parse(markdownContent));
}
/**
* Get front-matter
*