Skip to content

Commit 9219c7d

Browse files
JacobWang-bcFosol
authored andcommitted
Enhance MarkKeywords method to support raw tag or styling options. help client use outlook email client to see highlight words. (bcgov#2483)
1 parent 949a4d1 commit 9219c7d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

libs/net/elastic/Extensions/StringExtensions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,17 @@ public static IEnumerable<string> ExtractKeywords(this string search, string que
159159

160160
/// <summary>
161161
/// Add html mark tags to each keyword.
162+
/// If markRawTag is false, we will use span with style. this is useful for email clients like Outlook.
163+
/// If markRawTag is true, we will use the raw tag name.
162164
/// </summary>
163165
/// <param name="text"></param>
164166
/// <param name="keywords"></param>
165-
/// <param name="mark"></param>
167+
/// <param name="tagName"></param>
168+
/// <param name="markRawTag"></param>
166169
/// <returns></returns>
167-
public static string MarkKeywords(this string text, IEnumerable<string> keywords, string tagName = "mark")
170+
public static string MarkKeywords(this string text, IEnumerable<string> keywords, string tagName = "mark", bool markRawTag = false)
168171
{
172+
const string highlightStyle = "background-color:#fff59e;color:inherit;";
169173
var values = String.Join("|", keywords.Where(v => !String.IsNullOrWhiteSpace(v)));
170174
return Regex.Replace(text, $@"\b({values})", match =>
171175
{
@@ -178,7 +182,11 @@ public static string MarkKeywords(this string text, IEnumerable<string> keywords
178182
values = values.Distinct().Where(v => !String.IsNullOrWhiteSpace(v)).ToList();
179183
var result = String.Join("", values);
180184
if (values.Count != 0)
185+
{
186+
if (!String.IsNullOrWhiteSpace(tagName) && !markRawTag && tagName.Equals("mark", StringComparison.OrdinalIgnoreCase))
187+
return $"<span style=\"{highlightStyle}\">{result}</span>";
181188
return $"<{tagName}>{result}</{tagName}>";
189+
}
182190
return result;
183191
}, RegexOptions.IgnoreCase);
184192
}

0 commit comments

Comments
 (0)