Browse Source

移除多余引用

懒得勤快 6 years ago
parent
commit
90346a098f

+ 8 - 38
Masuit.LuceneEFCore.SearchEngine/Extensions/StringHelpers.cs

@@ -1,6 +1,6 @@
-using HtmlAgilityPack;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using System.Linq;
+using System.Text.RegularExpressions;
 
 namespace Masuit.LuceneEFCore.SearchEngine.Extensions
 {
@@ -22,45 +22,15 @@ namespace Masuit.LuceneEFCore.SearchEngine.Extensions
         }
 
         /// <summary>
-        /// 移除html标签
+        /// 去除html标签后并截取字符串
         /// </summary>
-        /// <param name="html"></param>
+        /// <param name="html">源html</param>
         /// <returns></returns>
-        public static string RemoveUnwantedTags(this string html)
+        public static string RemoveHtmlTag(this string html)
         {
-            if (string.IsNullOrEmpty(html))
-            {
-                return string.Empty;
-            }
-
-            var document = new HtmlDocument();
-            document.LoadHtml(html);
-
-            var nodes = new Queue<HtmlNode>(document.DocumentNode.SelectNodes("./*|./text()"));
-
-            while (nodes.Count > 0)
-            {
-                var node = nodes.Dequeue();
-                var parentNode = node.ParentNode;
-
-                if (node.Name != "#text")
-                {
-                    var childNodes = node.SelectNodes("./*|./text()");
-
-                    if (childNodes != null)
-                    {
-                        foreach (var child in childNodes)
-                        {
-                            nodes.Enqueue(child);
-                            parentNode.InsertBefore(child, node);
-                        }
-                    }
-
-                    parentNode.RemoveChild(node);
-                }
-            }
-
-            return document.DocumentNode.InnerHtml;
+            string strText = Regex.Replace(html, "<[^>]+>", "");
+            strText = Regex.Replace(strText, "&[^;]+;", "");
+            return strText;
         }
     }
 }

+ 1 - 1
Masuit.LuceneEFCore.SearchEngine/LuceneIndexableBaseEntity.cs

@@ -56,7 +56,7 @@ namespace Masuit.LuceneEFCore.SearchEngine
                     foreach (LuceneIndexAttribute attr in attrs)
                     {
                         string name = !string.IsNullOrEmpty(attr.Name) ? attr.Name : propertyInfo.Name;
-                        string value = attr.IsHtml ? propertyValue.ToString().RemoveUnwantedTags() : propertyValue.ToString();
+                        string value = attr.IsHtml ? propertyValue.ToString().RemoveHtmlTag() : propertyValue.ToString();
                         doc.Add(new Field(name, value, attr.Store, attr.Index));
                     }
                 }

+ 0 - 1
Masuit.LuceneEFCore.SearchEngine/Masuit.LuceneEFCore.SearchEngine.csproj

@@ -12,7 +12,6 @@
     <PackageId>Masuit.LuceneEFCore.SearchEngine_int</PackageId>
   </PropertyGroup>
   <ItemGroup>
-    <PackageReference Include="HtmlAgilityPack" Version="1.8.14" />
     <PackageReference Include="JieBa.Lucene.Analyzer" Version="1.0.1" />
     <PackageReference Include="Lucene.Net" Version="4.8.0-beta00005" />
     <PackageReference Include="Lucene.Net.QueryParser" Version="4.8.0-beta00005" />