diff --git a/db.go b/db.go index 13b4582..4bac6f5 100644 --- a/db.go +++ b/db.go @@ -355,7 +355,7 @@ var reg = regexp.MustCompile(setting.Conf.Identifier) func GenerateExcerptAndRender(artc *Article) { index := reg.FindStringIndex(artc.Content) if len(index) > 0 { - artc.Excerpt = artc.Content[0:index[0]] + artc.Excerpt = IgnoreHtmlTag(artc.Content[0:index[0]]) } else { uc := []rune(artc.Content) length := setting.Conf.Length diff --git a/elasticsearch.go b/elasticsearch.go index 4d0e8df..6a61a24 100644 --- a/elasticsearch.go +++ b/elasticsearch.go @@ -142,7 +142,7 @@ func ElasticsearchSimple(q string, size, from int) *ESSearchResult { func ElasticIndex(artc *Article) error { mapping := map[string]interface{}{ "title": artc.Title, - "content": artc.Content, + "content": IgnoreHtmlTag(artc.Content), "slug": artc.Slug, "tags": artc.Tags, "create_time": artc.CreateTime, diff --git a/helper.go b/helper.go index c3abe91..5d9d5cb 100644 --- a/helper.go +++ b/helper.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "fmt" "io" + "regexp" ) const ( @@ -24,3 +25,13 @@ func EncryptPasswd(name, pass string) string { func VerifyPasswd(origin, name, input string) bool { return origin == EncryptPasswd(name, input) } + +func IgnoreHtmlTag(src string) string { + //去除所有尖括号内的HTML代码 + re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") + src = re.ReplaceAllString(src, "") + + //去除换行符 + re, _ = regexp.Compile("\\s{1,}") + return re.ReplaceAllString(src, "") +}