Compare commits

...

2 Commits

1 changed files with 13 additions and 7 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnidecodeSharpFork;
@ -170,7 +171,8 @@ namespace LibSearchBox
foreach ((int, string) token in tokenizer.IterateTokens())
{
ConcurrentDictionary<int, List<int>> tokenQuery = index.Query(token.Item2);
if (!tokenQuery.ContainsKey(pageId)) continue; // Don't bother if this page doesn't contain this token
if (tokenQuery == null || !tokenQuery.ContainsKey(pageId))
continue; // Don't bother if this page doesn't contain this token
offsets.AddRange(tokenQuery[pageId].Select((int offset) => new SearchOffset(token.Item2, offset)));
}
offsets.Sort((SearchOffset x, SearchOffset y) => x.Offset - y.Offset);
@ -228,10 +230,14 @@ namespace LibSearchBox
if (settings.Html) {
result = WebUtility.HtmlEncode(result);
string resultSearchable = result.Unidecode().ToLower();
foreach ((int, string) nextToken in tokenizer.IterateTokens()) {
// TODO: Insert html tags here
throw new NotImplementedException("HTML tag insertion hasn't been implemented yet");
foreach((int, string) nextToken in tokenizer.IterateTokens()) {
result = Regex.Replace(
result,
Regex.Escape(nextToken.Item2),
$"<strong class='query-term'>{nextToken.Item2}</strong>",
RegexOptions.IgnoreCase // Also ignores accents, apparently
);
}
}
@ -241,9 +247,9 @@ namespace LibSearchBox
);
// Add the separator at the beginning and end if we aren't at the bounds of the source document
if (snippets.First().Item1 > 0)
if (snippets.Count > 0 && snippets.First().Item1 > 0)
snippetsText.Insert(0, "");
if (snippets.Last().Item2 < source.Length)
if (snippets.Count > 0 && snippets.Last().Item2 < source.Length)
snippetsText.Add("");
return string.Join(