mirror of https://github.com/halo-dev/halo
feat: 文章表添加字数统计字段,主题可使用${post.wordCount}调用 (#965)
parent
0dbbef919c
commit
41edec8213
|
@ -30,6 +30,8 @@ public class BasePostSimpleDTO extends BasePostMinimalDTO {
|
||||||
|
|
||||||
private Long likes;
|
private Long likes;
|
||||||
|
|
||||||
|
private Long wordCount;
|
||||||
|
|
||||||
public boolean isTopped() {
|
public boolean isTopped() {
|
||||||
return this.topPriority != null && this.topPriority > 0;
|
return this.topPriority != null && this.topPriority > 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ import java.util.Date;
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
|
* @author coor.top
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@Entity(name = "BasePost")
|
@Entity(name = "BasePost")
|
||||||
|
@ -151,6 +152,13 @@ public class BasePost extends BaseEntity {
|
||||||
@Column(name = "meta_description", length = 1023)
|
@Column(name = "meta_description", length = 1023)
|
||||||
private String metaDescription;
|
private String metaDescription;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Content word count
|
||||||
|
*/
|
||||||
|
@Column(name = "word_count")
|
||||||
|
@ColumnDefault("0")
|
||||||
|
private Long wordCount;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void prePersist() {
|
public void prePersist() {
|
||||||
super.prePersist();
|
super.prePersist();
|
||||||
|
@ -206,6 +214,10 @@ public class BasePost extends BaseEntity {
|
||||||
if (editorType == null) {
|
if (editorType == null) {
|
||||||
editorType = PostEditorType.MARKDOWN;
|
editorType = PostEditorType.MARKDOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (wordCount == null || wordCount < 0) {
|
||||||
|
wordCount = 0L;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -231,6 +231,11 @@ public abstract class BasePostServiceImpl<POST extends BasePost> extends Abstrac
|
||||||
public POST createOrUpdateBy(POST post) {
|
public POST createOrUpdateBy(POST post) {
|
||||||
Assert.notNull(post, "Post must not be null");
|
Assert.notNull(post, "Post must not be null");
|
||||||
|
|
||||||
|
String originalContent = post.getOriginalContent();
|
||||||
|
originalContent = run.halo.app.utils.StringUtils.htmlToString(originalContent);
|
||||||
|
|
||||||
|
post.setWordCount((long) originalContent.length());
|
||||||
|
|
||||||
// Render content
|
// Render content
|
||||||
if (post.getEditorType().equals(PostEditorType.MARKDOWN)) {
|
if (post.getEditorType().equals(PostEditorType.MARKDOWN)) {
|
||||||
post.setFormatContent(MarkdownUtils.renderHtml(post.getOriginalContent()));
|
post.setFormatContent(MarkdownUtils.renderHtml(post.getOriginalContent()));
|
||||||
|
|
|
@ -54,6 +54,7 @@ import static run.halo.app.model.support.HaloConst.URL_SEPARATOR;
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
* @author guqing
|
* @author guqing
|
||||||
* @author evanwang
|
* @author evanwang
|
||||||
|
* @author coor.top
|
||||||
* @date 2019-03-14
|
* @date 2019-03-14
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -560,6 +561,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
|
|
||||||
postListVO.setFullPath(buildFullPath(post));
|
postListVO.setFullPath(buildFullPath(post));
|
||||||
|
|
||||||
|
postListVO.setWordCount(post.getWordCount());
|
||||||
|
|
||||||
return postListVO;
|
return postListVO;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -618,6 +621,8 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
||||||
|
|
||||||
postListVO.setFullPath(buildFullPath(post));
|
postListVO.setFullPath(buildFullPath(post));
|
||||||
|
|
||||||
|
postListVO.setWordCount(post.getWordCount());
|
||||||
|
|
||||||
return postListVO;
|
return postListVO;
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package run.halo.app.utils;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @program: halo
|
||||||
|
* @description: processing strings Utlis
|
||||||
|
* @author: coor.top
|
||||||
|
* @create: 2020-07-14 01:24
|
||||||
|
**/
|
||||||
|
public class StringUtils {
|
||||||
|
|
||||||
|
private StringUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* html convert to string
|
||||||
|
*
|
||||||
|
* @param htmlStr
|
||||||
|
*/
|
||||||
|
public static String htmlToString(String htmlStr) {
|
||||||
|
htmlStr = htmlStr.trim().replaceAll("\"", "'"); //如果有双引号将其先转成单引号
|
||||||
|
String regExScript = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
|
||||||
|
String regExStyle = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
|
||||||
|
String regExHtml = "<[^>]+>"; // 定义HTML标签的正则表达式
|
||||||
|
|
||||||
|
Pattern pScript = Pattern.compile(regExScript, Pattern.CASE_INSENSITIVE);
|
||||||
|
Matcher mScript = pScript.matcher(htmlStr);
|
||||||
|
htmlStr = mScript.replaceAll(""); // 过滤script标签
|
||||||
|
|
||||||
|
Pattern pStyle = Pattern.compile(regExStyle, Pattern.CASE_INSENSITIVE);
|
||||||
|
Matcher mStyle = pStyle.matcher(htmlStr);
|
||||||
|
htmlStr = mStyle.replaceAll(""); // 过滤style标签
|
||||||
|
|
||||||
|
Pattern pHtml = Pattern.compile(regExHtml, Pattern.CASE_INSENSITIVE);
|
||||||
|
Matcher mHtml = pHtml.matcher(htmlStr);
|
||||||
|
htmlStr = mHtml.replaceAll(""); // 过滤html标签
|
||||||
|
|
||||||
|
return htmlStr;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue