Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlightL committed Apr 28, 2021
2 parents 96dff2a + f2353d0 commit bece03e
Show file tree
Hide file tree
Showing 52 changed files with 19,168 additions and 293 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,6 @@
<version>1.13.1</version>
</dependency>

<!-- Hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.7</version>
</dependency>

<!-- git 命令相关 -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public enum ConfigEnum {
EMAIL_USERNAME("email_username", "", "发送邮件用户名"),
EMAIL_PASSWORD("email_password", "", "发送邮件密码"),

TWIKOO_ENV_ID("twikoo_env_id", "", "twikoo环境 id")
TWIKOO_ENV_ID("twikoo_env_id", "", "twikoo环境 id"),
EDITOR_TYPE("editor_type", "markdown", "编辑器类型"),
;

private String name;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/light/hexo/business/admin/model/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public class Post implements Serializable {
*/
private String content;

/**
* 文章内容(html 格式)
*/
private String contentHtml;

/**
* 作者
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,10 @@ public interface PostService extends BaseService<Post>, EventService {
*/
List<Post> findTopList() throws GlobalException;

/**
* 获取 contentHtml 为空的文章列表(定时器任务专用)
* @return
* @throws GlobalException
*/
List<Post> listEmptyHtml() throws GlobalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ protected Example getExample(BaseRequest request) {
return example;
}

@Override
public int saveModel(Post model) throws GlobalException {
return super.saveModel(model);
}

@Override
public int updateModel(Post model) throws GlobalException {
return super.updateModel(model);
}

@Override
public PageInfo<Post> findPage(BaseRequest<Post> request) throws GlobalException {

Expand Down Expand Up @@ -200,9 +190,18 @@ public void savePost(Post post) throws GlobalException {
post.setCoverUrl(coverUrl);
}

boolean isMarkdown = this.configService.getConfigValue(ConfigEnum.EDITOR_TYPE.getName()).equals("markdown");
String content = post.getContent();

// 摘要
post.setSummary(this.interceptContent(post.getContent()))
.setSummaryHtml(this.interceptContentHtml(post.getContent()));
post.setSummary(this.interceptContent(content, isMarkdown))
.setSummaryHtml(this.interceptContentHtml(content, isMarkdown));

if (isMarkdown) {
post.setContentHtml(MarkdownUtil.md2html(post.getContent()));
} else {
post.setContentHtml(this.transformHtml(post.getContent()));
}

LocalDateTime now = LocalDateTime.now();
if (post.getPublish() != null) {
Expand Down Expand Up @@ -263,9 +262,18 @@ public void editPost(Post post) throws GlobalException {
ExceptionUtil.throwEx(HexoExceptionEnum.ERROR_POST_TITLE_REPEAT);
}

boolean isMarkdown = this.configService.getConfigValue(ConfigEnum.EDITOR_TYPE.getName()).equals("markdown");
String content = post.getContent();

// 摘要
post.setSummary(this.interceptContent(post.getContent()))
.setSummaryHtml(this.interceptContentHtml(post.getContent()));
post.setSummary(this.interceptContent(content, isMarkdown))
.setSummaryHtml(this.interceptContentHtml(content, isMarkdown));

if (isMarkdown) {
post.setContentHtml(MarkdownUtil.md2html(post.getContent()));
} else {
post.setContentHtml(this.transformHtml(post.getContent()));
}

LocalDateTime now = LocalDateTime.now();
if (post.getPublish() != null) {
Expand Down Expand Up @@ -711,6 +719,15 @@ public List<Post> findTopList() throws GlobalException {
return postList;
}

@Override
public List<Post> listEmptyHtml() throws GlobalException {
Example example = Example.builder(Post.class)
.select("id", "content")
.where(Sqls.custom().andEqualTo("contentHtml", ""))
.build();
return this.getBaseMapper().selectByExample(example);
}

@Override
public EventEnum getEventType() {
return EventEnum.POST;
Expand Down Expand Up @@ -739,7 +756,7 @@ public void dealWithEvent(BaseEvent event) {
this.updateModel(data);
}

private String interceptContent(String content) {
private String interceptContent(String content, boolean isMarkdown) {
StringBuilder sb = new StringBuilder();
int index = content.indexOf("<!---->");
if (index > -1) {
Expand All @@ -749,7 +766,8 @@ private String interceptContent(String content) {
}
}

String html = MarkdownUtil.md2html(content);
String html = isMarkdown ? MarkdownUtil.md2html(content) : content;

Elements elements = Jsoup.parse(html).select("p");
if (elements.size() > 3) {
List<Element> elementList = elements.subList(0, 3);
Expand All @@ -770,7 +788,7 @@ private String interceptContent(String content) {
return result;
}

private String interceptContentHtml(String content) {
private String interceptContentHtml(String content, boolean isMarkdown) {
StringBuilder sb = new StringBuilder();
int index = content.indexOf("<!---->");
if (index > -1) {
Expand All @@ -780,11 +798,11 @@ private String interceptContentHtml(String content) {
}
}

String html = MarkdownUtil.md2html(content);
String html = isMarkdown ? MarkdownUtil.md2html(content) : content;
Document document = Jsoup.parse(html);
Elements elements = document.select("body");
Element element = elements.get(0);
Elements children = element.children();
Element body = elements.get(0);
Elements children = body.children();
if (children.size() > 3) {
List<Element> elementList = children.subList(0, 3);
elementList.forEach(i -> {
Expand All @@ -799,6 +817,94 @@ private String interceptContentHtml(String content) {
return sb.toString();
}

private String transformHtml(String html) {
Document document = Jsoup.parse(html);

// 转换 img
Elements imgElements = document.select("img");
for (Element img : imgElements) {
String src = img.attr("src");
img.removeAttr("src").attr("data-original", src).addClass("lazyload");
img.wrap("<a class='fancybox' href='" + src + "' data-fancybox='gallery'></a>");
}

// 转换 table
Elements tableElements = document.select("table");
for (Element table : tableElements) {
table.attr("class", "table");
Elements trs = table.select("tr");
int index = 0;
StringBuilder sb = new StringBuilder();
for (Element tr : trs) {
Elements tds = tr.select("td");
if (index == 0) {
// thead
sb.append("<thead><tr>");
for (Element td : tds) {
sb.append("<th>").append(td.text()).append("</th>");
}
sb.append("</tr></thead><tbody>");
} else {
// 处理 tbody
sb.append("<tr>");
for (Element td : tds) {
sb.append("<td>").append(td.text()).append("</td>");
}
sb.append("</tr>");
}
index++;
}
sb.append("</tbody>");
table.html(sb.toString());
}

// 转换 pre
Elements preElements = document.select("pre");
for (Element pre : preElements) {

String data = pre.text().replaceAll("\r", "");
String[] split = data.split("\n");
int linNum = 1;

Element figure = new Element("figure");
figure.attr("class", "highlight");
Element table = new Element("table");
Element tbody = new Element("tbody");
Element tr = new Element("tr");

Element lineTd = new Element("td");
lineTd.attr("class", "gutter");
Element linePre = new Element("pre");
for (String s : split) {
Element span = new Element("span");
span.attr("class", "line");
span.text((linNum++) + "");
linePre.appendChild(span).appendElement("br");
}
lineTd.appendChild(linePre);
tr.appendChild(lineTd);

Element contentTd = new Element("td");
contentTd.attr("class", "code");
Element contentPre = new Element("pre");
for (String s : split) {
Element span = new Element("span");
span.attr("class", "line");
span.text(s);
contentPre.appendChild(span).appendElement("br");
}
contentTd.appendChild(contentPre);
tr.appendChild(contentTd);

tbody.appendChild(tr);
table.appendChild(tbody);
figure.appendChild(table);
pre.replaceWith(figure);
}

return document.toString();
}

private void saveTags(Post post, boolean isEdit) {
if (StringUtils.isBlank(post.getTags())) {
return;
Expand Down Expand Up @@ -833,8 +939,8 @@ private List<Post> packageToList(List<Map<String, Object>> recordList) throws Un
post.setTitle(objectMap.get("title").toString())
.setAuthor(author)
.setContent(objectMap.get("content").toString())
.setSummary(this.interceptContent(post.getContent()))
.setSummaryHtml(this.interceptContentHtml(post.getContent()));
.setSummary(this.interceptContent(post.getContent(), true))
.setSummaryHtml(this.interceptContentHtml(post.getContent(), true));

// 设置封面
int num = new Random().nextInt(COVER_NUM);
Expand Down Expand Up @@ -911,8 +1017,8 @@ private List<Post> packageToList(File[] files) {
post.setTitle(titleStr.substring(titleStr.indexOf(":") + 1).trim())
.setAuthor(author)
.setContent(sb.toString())
.setSummary(this.interceptContent(post.getContent()))
.setSummaryHtml(this.interceptContentHtml(post.getContent()));
.setSummary(this.interceptContent(post.getContent(), true))
.setSummaryHtml(this.interceptContentHtml(post.getContent(), true));

// 设置封面
int num = new Random().nextInt(COVER_NUM);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.light.hexo.business.admin.service.impl;

import cn.hutool.core.io.FileUtil;
import com.light.hexo.business.admin.config.BlogProperty;
import com.light.hexo.business.admin.constant.HexoExceptionEnum;
import com.light.hexo.business.admin.mapper.ThemeMapper;
Expand Down Expand Up @@ -248,8 +247,8 @@ public void fetchTheme(String themeUrl) throws GlobalException {
ExceptionUtil.throwEx(HexoExceptionEnum.ERROR_THEME_URL_WRONG);
}

String ext = FileUtil.extName(themeUrl);
if (!"git".equals(ext)) {
int extIndex = themeUrl.lastIndexOf(".git");
if (extIndex < 0) {
ExceptionUtil.throwEx(HexoExceptionEnum.ERROR_THEME_URL_WRONG);
}

Expand Down
57 changes: 57 additions & 0 deletions src/main/java/com/light/hexo/business/admin/task/PostTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.light.hexo.business.admin.task;

import com.light.hexo.business.admin.constant.ConfigEnum;
import com.light.hexo.business.admin.constant.StateEnum;
import com.light.hexo.business.admin.model.Backup;
import com.light.hexo.business.admin.model.Post;
import com.light.hexo.business.admin.service.PostService;
import com.light.hexo.common.util.MarkdownUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @Author MoonlightL
* @ClassName: PostTask
* @ProjectName hexo-boot
* @Description: 文章定时器
* @DateTime 2021/4/27 17:03
*/
@Component
@Slf4j
public class PostTask {

@Autowired
private PostService postService;

private ExecutorService executorService = Executors.newFixedThreadPool(2);

/**
* 扫描 contentHtml 内容为空的文章(兼容老版本)
*/
@Scheduled(cron = "0 0/1 * * * ?")
public void checkContentHtml() {
List<Post> postList = this.postService.listEmptyHtml();
if (CollectionUtils.isEmpty(postList)) {
return;
}

for (Post post : postList) {
this.executorService.submit(() -> {
Post tmp = new Post();
tmp.setId(post.getId());
tmp.setContentHtml(MarkdownUtil.md2html(post.getContent())).setUpdateTime(LocalDateTime.now());
this.postService.updateModel(tmp);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.light.hexo.business.admin.web.controller;

import cn.hutool.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.light.hexo.common.component.file.*;
import com.light.hexo.common.exception.GlobalException;
import com.light.hexo.common.exception.GlobalExceptionEnum;
Expand All @@ -11,18 +9,15 @@
import com.light.hexo.common.model.bing.WebPic;
import com.light.hexo.common.util.ExceptionUtil;
import com.light.hexo.common.util.HttpClientUtil;
import com.light.hexo.common.util.JsonUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @Author MoonlightL
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/light/hexo/common/model/PostRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PostRequest extends BaseRequest<Post> {
private String title;

/**
* 文章内容(markdown 格式)
* 文章内容(原始数据,markdown 或 html 格式)
*/
@NotEmpty(message = "文章内容不能为空", groups = {BaseRequest.Save.class, BaseRequest.Update.class})
private String content;
Expand Down
Loading

0 comments on commit bece03e

Please sign in to comment.