2018-02-28 15:47:00 +08:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"bytes"
|
2018-02-28 15:47:00 +08:00
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
2018-02-28 15:47:00 +08:00
|
|
|
|
)
|
|
|
|
|
|
2021-10-09 18:22:43 +08:00
|
|
|
|
func StripTags(s string) string {
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
|
|
|
|
//将HTML标签全转换成小写
|
|
|
|
|
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
|
|
|
|
|
src := re.ReplaceAllStringFunc(s, strings.ToLower)
|
|
|
|
|
|
|
|
|
|
//去除STYLE
|
|
|
|
|
re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
|
|
|
|
|
src = re.ReplaceAllString(src, "")
|
|
|
|
|
|
|
|
|
|
//去除SCRIPT
|
|
|
|
|
re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
|
|
|
|
|
src = re.ReplaceAllString(src, "")
|
|
|
|
|
|
|
|
|
|
//去除所有尖括号内的HTML代码,并换成换行符
|
|
|
|
|
re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
|
|
|
|
|
src = re.ReplaceAllString(src, "\n")
|
|
|
|
|
|
|
|
|
|
//去除连续的换行符
|
|
|
|
|
re, _ = regexp.Compile("\\s{2,}")
|
|
|
|
|
src = re.ReplaceAllString(src, "\n")
|
|
|
|
|
|
|
|
|
|
return src
|
|
|
|
|
}
|
2021-10-09 18:22:43 +08:00
|
|
|
|
|
2023-11-20 17:31:45 +08:00
|
|
|
|
// 自动提取文章摘要
|
2021-10-09 18:22:43 +08:00
|
|
|
|
func AutoSummary(body string, l int) string {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
|
|
|
|
//匹配图片,如果图片语法是在代码块中,这里同样会处理
|
|
|
|
|
re := regexp.MustCompile(`<p>(.*?)</p>`)
|
|
|
|
|
|
|
|
|
|
contents := re.FindAllString(body, -1)
|
|
|
|
|
|
|
|
|
|
if len(contents) <= 0 {
|
2021-10-09 18:22:43 +08:00
|
|
|
|
return ""
|
2018-07-25 14:46:56 +08:00
|
|
|
|
}
|
|
|
|
|
content := ""
|
2021-10-09 18:22:43 +08:00
|
|
|
|
for _, s := range contents {
|
|
|
|
|
b := strings.Replace(StripTags(s), "\n", "", -1)
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
|
|
|
|
if l <= 0 {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
l = l - len([]rune(b))
|
|
|
|
|
|
|
|
|
|
content += b
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return content
|
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
|
2023-11-20 17:31:45 +08:00
|
|
|
|
// 安全处理HTML文档,过滤危险标签和属性.
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func SafetyProcessor(html string) string {
|
|
|
|
|
|
|
|
|
|
//安全过滤,移除危险标签和属性
|
|
|
|
|
if docQuery, err := goquery.NewDocumentFromReader(bytes.NewBufferString(html)); err == nil {
|
|
|
|
|
docQuery.Find("script").Remove()
|
|
|
|
|
docQuery.Find("form").Remove()
|
|
|
|
|
docQuery.Find("link").Remove()
|
|
|
|
|
docQuery.Find("applet").Remove()
|
|
|
|
|
docQuery.Find("frame").Remove()
|
|
|
|
|
docQuery.Find("meta").Remove()
|
2021-10-09 18:22:43 +08:00
|
|
|
|
if !conf.GetEnableIframe() {
|
|
|
|
|
docQuery.Find("iframe").Remove()
|
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
docQuery.Find("*").Each(func(i int, selection *goquery.Selection) {
|
|
|
|
|
|
|
|
|
|
if href, ok := selection.Attr("href"); ok && strings.HasPrefix(href, "javascript:") {
|
|
|
|
|
selection.SetAttr("href", "#")
|
|
|
|
|
}
|
|
|
|
|
if src, ok := selection.Attr("src"); ok && strings.HasPrefix(src, "javascript:") {
|
|
|
|
|
selection.SetAttr("src", "#")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selection.RemoveAttr("onafterprint").
|
|
|
|
|
RemoveAttr("onbeforeprint").
|
|
|
|
|
RemoveAttr("onbeforeunload").
|
|
|
|
|
RemoveAttr("onload").
|
|
|
|
|
RemoveAttr("onclick").
|
|
|
|
|
RemoveAttr("onkeydown").
|
|
|
|
|
RemoveAttr("onkeypress").
|
|
|
|
|
RemoveAttr("onkeyup").
|
|
|
|
|
RemoveAttr("ondblclick").
|
|
|
|
|
RemoveAttr("onmousedown").
|
|
|
|
|
RemoveAttr("onmousemove").
|
|
|
|
|
RemoveAttr("onmouseout").
|
|
|
|
|
RemoveAttr("onmouseover").
|
|
|
|
|
RemoveAttr("onmouseup")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//处理外链
|
|
|
|
|
docQuery.Find("a").Each(func(i int, contentSelection *goquery.Selection) {
|
|
|
|
|
if src, ok := contentSelection.Attr("href"); ok {
|
|
|
|
|
if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") {
|
|
|
|
|
if conf.BaseUrl != "" && !strings.HasPrefix(src, conf.BaseUrl) {
|
|
|
|
|
contentSelection.SetAttr("target", "_blank")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-09-14 10:24:41 +08:00
|
|
|
|
//添加文档标签包裹
|
2023-06-07 10:04:11 +08:00
|
|
|
|
if selector := docQuery.Find("div.whole-article-wrap").First(); selector.Size() <= 0 {
|
|
|
|
|
docQuery.Find("body").Children().WrapAllHtml("<div class=\"whole-article-wrap\"></div>")
|
2018-09-14 10:24:41 +08:00
|
|
|
|
}
|
|
|
|
|
//解决文档内容缺少包裹标签的问题
|
|
|
|
|
if selector := docQuery.Find("div.markdown-article").First(); selector.Size() <= 0 {
|
|
|
|
|
if selector := docQuery.Find("div.markdown-toc").First(); selector.Size() > 0 {
|
|
|
|
|
docQuery.Find("div.markdown-toc").NextAll().WrapAllHtml("<div class=\"markdown-article\"></div>")
|
2023-11-20 17:31:45 +08:00
|
|
|
|
} else if selector := docQuery.Find("dir.toc").First(); selector.Size() > 0 {
|
|
|
|
|
docQuery.Find("dir.toc").NextAll().WrapAllHtml("<div class=\"markdown-article\"></div>")
|
2018-09-14 10:24:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
|
|
|
|
|
if html, err := docQuery.Html(); err == nil {
|
2021-10-09 18:22:43 +08:00
|
|
|
|
return strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(html), "<html><head></head><body>"), "</body></html>")
|
2018-09-13 18:19:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return html
|
2021-10-09 18:22:43 +08:00
|
|
|
|
}
|