diff --git a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java index b58d17f78..ee36ee138 100644 --- a/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BasePostServiceImpl.java @@ -7,7 +7,6 @@ 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; @@ -24,6 +23,7 @@ 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; +import run.halo.app.utils.HaloUtils; import run.halo.app.utils.MarkdownUtils; import run.halo.app.utils.ServiceUtils; @@ -40,6 +40,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC; * Base post service implementation. * * @author johnniang + * @author ryanwang * @date 2019-04-24 */ @Slf4j @@ -275,7 +276,7 @@ public abstract class BasePostServiceImpl extends Abstrac // Set summary if (StringUtils.isBlank(basePostSimpleDTO.getSummary())) { - // TODO build post summary + basePostSimpleDTO.setSummary(generateSummary(post.getFormatContent())); } return basePostSimpleDTO; @@ -347,12 +348,14 @@ public abstract class BasePostServiceImpl extends Abstrac } @NonNull - protected String convertToSummary(@Nullable String markdownContent) { + protected String generateSummary(@NonNull String htmlContent) { + Assert.notNull(htmlContent, "html content must not be null"); + + String text = HaloUtils.cleanHtmlTag(htmlContent); + // Get summary length Integer summaryLength = optionService.getByPropertyOrDefault(PostProperties.SUMMARY_LENGTH, Integer.class, 150); - // TODO build summary. - return ""; + return StringUtils.substring(text, 0, summaryLength); } - } diff --git a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java index 7493bfa89..dd9d52e30 100644 --- a/src/main/java/run/halo/app/service/impl/PostServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/PostServiceImpl.java @@ -463,7 +463,7 @@ public class PostServiceImpl extends BasePostServiceImpl implements PostSe PostListVO postListVO = new PostListVO().convertFrom(post); if (StringUtils.isBlank(postListVO.getSummary())) { - // TODO Set summary + postListVO.setSummary(generateSummary(post.getFormatContent())); } Optional.ofNullable(tagListMap.get(post.getId())).orElseGet(LinkedList::new); diff --git a/src/main/java/run/halo/app/utils/HaloUtils.java b/src/main/java/run/halo/app/utils/HaloUtils.java index 5aeb6a35d..ab58e4ad4 100755 --- a/src/main/java/run/halo/app/utils/HaloUtils.java +++ b/src/main/java/run/halo/app/utils/HaloUtils.java @@ -8,7 +8,6 @@ import org.springframework.util.Assert; import java.net.InetAddress; import java.net.UnknownHostException; -import java.nio.file.Path; import java.util.UUID; import static run.halo.app.model.support.HaloConst.FILE_SEPARATOR; @@ -17,11 +16,14 @@ import static run.halo.app.model.support.HaloConst.FILE_SEPARATOR; * Common utils * * @author ryanwang + * @author johnniang * @date : 2017/12/22 */ @Slf4j public class HaloUtils { + private static final String RE_HTML_MARK = "(<[^<]*?>)|(<[\\s]*?/[^<]*?>)|(<[^<]*?/[\\s]*?>)"; + /** * Desensitizes the plain text. * @@ -192,4 +194,14 @@ public class HaloUtils { } return machineAddress.getHostAddress(); } + + /** + * Clean all html tag + * + * @param content html document + * @return text before cleaned + */ + public static String cleanHtmlTag(String content) { + return content.replaceAll(RE_HTML_MARK, ""); + } }