!7 md预览过滤格式

* 加图标
* utf-8 md过滤格式
* Merge branch 'master' of https://gitee.com/zyplayer/zyplayer-doc
* jar打包 打包版本 tree图标 预览过滤
This commit is contained in:
护身法
2023-01-04 09:49:05 +00:00
committed by 暮光:城中城
parent 3829a438e2
commit fe1da585ac
13 changed files with 163 additions and 84 deletions

View File

@@ -30,6 +30,7 @@ import com.zyplayer.doc.data.utils.CachePrefix;
import com.zyplayer.doc.data.utils.CacheUtil;
import com.zyplayer.doc.wiki.controller.vo.WikiPageContentVo;
import com.zyplayer.doc.wiki.controller.vo.WikiPageVo;
import com.zyplayer.doc.wiki.framework.common.MDToText;
import com.zyplayer.doc.wiki.framework.consts.SpaceType;
import com.zyplayer.doc.wiki.service.common.WikiPageAuthService;
import com.zyplayer.doc.wiki.service.git.GitService;
@@ -210,6 +211,10 @@ public class WikiPageController {
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
WikiPageContent pageContent = new WikiPageContent();
pageContent.setContent(content);
if(wikiPage.getEditorType()==2)
{
preview = MDToText.mdToText(preview);
}
pageContent.setPreview(preview);
// 数据库是varchar(16000)所以如果不开启es的话搜索超过16000的文章就搜不到~es存preview不截断
if (StringUtils.isNotBlank(preview) && preview.length() > 16000) {

View File

@@ -0,0 +1,55 @@
package com.zyplayer.doc.wiki.framework.common;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
public class MDToText {
/**
* 去除html代码中含有的标签
* @param htmlStr
* @return
*/
private static String delHtmlTags(String htmlStr) {
//定义script的正则表达式去除js可以防止注入
String scriptRegex="<script[^>]*?>[\\s\\S]*?<\\/script>";
//定义style的正则表达式去除style样式防止css代码过多时只截取到css样式代码
String styleRegex="<style[^>]*?>[\\s\\S]*?<\\/style>";
//定义HTML标签的正则表达式去除标签只提取文字内容
String htmlRegex="<[^>]+>";
//定义空格,回车,换行符,制表符
String spaceRegex = "\\s*|\t|\r|\n";
// 过滤script标签
htmlStr = htmlStr.replaceAll(scriptRegex, "");
// 过滤style标签
htmlStr = htmlStr.replaceAll(styleRegex, "");
// 过滤html标签
htmlStr = htmlStr.replaceAll(htmlRegex, "");
// 过滤空格等
htmlStr = htmlStr.replaceAll(spaceRegex, "");
return htmlStr.trim(); // 返回文本字符串
}
/**
* 获取HTML代码里的内容
* @param htmlStr
* @returnMARKDOWN取文本
*/
public static String getTextFromHtml(String htmlStr){
//去除html标签
htmlStr = delHtmlTags(htmlStr);
//去除空格" "
htmlStr = htmlStr.replaceAll(" ","");
return htmlStr;
}
public static String mdToText(String mdContent){
Parser parser = Parser.builder().build();
Node document = parser.parse(mdContent);
HtmlRenderer renderer = HtmlRenderer.builder().build();
String html= renderer.render(document);
return getTextFromHtml(html);
}
}

View File

@@ -24,6 +24,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Set;
@@ -158,7 +159,7 @@ public class GitService {
treeWalk.setRecursive(false);
ObjectId blobId = treeWalk.getObjectId(0);
ObjectLoader loader = repository.open(blobId);
return new String(loader.getBytes());
return new String(loader.getBytes(), Charset.forName("UTF-8"));
} catch (Exception e) {
log.error("获取git文件内容失败", e);
throw new ConfirmException("获取历史版本数据失败:" + e.getMessage());