Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retrieve string content from JSON properties with supplied names (Kentico)
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using CMS;
using CMS.DataEngine;
using CMS.DocumentEngine;
using Example.Linq;
using Example.Search;
using Newtonsoft.Json.Linq;
[assembly: RegisterModule(typeof(SearchModule))]
namespace Example.Search
{
public class SearchModule : Module
{
public SearchModule()
: base(nameof(SearchModule))
{
}
protected override void OnInit()
{
base.OnInit();
DocumentEvents.GetContent.Execute += OnGetPageContent;
}
private static void OnGetPageContent(object sender, DocumentSearchEventArgs e)
{
// Example property names to be added to index
var indexableProperties = new[] {"heading", "html", "text"};
var node = e.Node;
if (node == null)
{
return;
}
var pagebuilderJson = node.GetStringValue("DocumentPageBuilderWidgets", string.Empty);
if (string.IsNullOrWhiteSpace(pagebuilderJson))
{
return;
}
var rootNode = JToken.Parse(pagebuilderJson);
var searchTokens = GetSearchTokens(rootNode, indexableProperties);
var pagebuilderContent = string.Join(" ", searchTokens.Select(RemoveTags));
e.AddContent(pagebuilderContent);
}
private static IEnumerable<string> GetSearchTokens(JToken token, string[] propertyNames)
{
var searchTokens = new Stack<JToken>(token.Children());
while (searchTokens.Any())
{
var currentToken = searchTokens.Pop();
if (currentToken.Type == JTokenType.Property)
{
var currentProperty = (JProperty)currentToken;
var value = currentProperty.Value.ToString();
if (propertyNames.Contains(currentProperty.Name)
&& !string.IsNullOrWhiteSpace(value))
{
yield return value;
}
}
foreach (var child in currentToken)
{
searchTokens.Push(child);
}
}
}
// Remove HTML from rich text
private static string RemoveTags(string input)
{
return input == null
? string.Empty
: Regex.Replace(input, "<[^>]+>", " ")
.Replace("\n", " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment