Created
April 16, 2018 20:20
-
-
Save bmcdavid/c60f3f3683f9c0235f52cf435a150856 to your computer and use it in GitHub Desktop.
Example to search Episerver blocks in XhtmlString properties, requires Vulcan.Core > 3.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using EPiServer.Core; | |
using EPiServer.Core.Html.StringParsing; | |
using EPiServer.Security; | |
using EPiServer.ServiceLocation; | |
using TcbInternetSolutions.Vulcan.Core; | |
using TcbInternetSolutions.Vulcan.Core.Extensions; | |
namespace WSOL.Web.Business.Vulcan | |
{ | |
[ServiceConfiguration(typeof(IVulcanIndexingModifier), Lifecycle = ServiceInstanceScope.Singleton)] | |
public class CustomIndexModifier : IVulcanIndexingModifier | |
{ | |
public void ProcessContent(IVulcanIndexingModifierArgs args) | |
{ | |
var contents = new List<string>(); | |
var properties = args.Content.GetType().GetProperties(); | |
foreach (var p in properties) | |
{ | |
// Property to string conversions | |
if (p.PropertyType != typeof(XhtmlString)) continue; | |
var value = p.GetValue(args.Content); | |
var references = GetStringReferences(value as XhtmlString); | |
var searchArea = new ContentArea(); | |
foreach(var reference in references) | |
searchArea.Items.Add(new ContentAreaItem(reference)); | |
contents.Add(searchArea.GetContentAreaContents()); | |
} | |
if (contents.Count == 0) return; | |
var stringContents = string.Join(separator: " ", values: contents); | |
if (string.IsNullOrWhiteSpace(stringContents)) return; | |
args.AdditionalItems["__xhtmlStringSearchContents"] = stringContents; | |
} | |
// https://world.episerver.com/blogs/Per-Magne-Skuseth/Dates/2016/1/episerver-find-index-blocks-in-xhtmlstring/ | |
private static IEnumerable<ContentFragment> GetStringReferences(XhtmlString xhtmlString) | |
{ | |
if (xhtmlString?.Fragments?.Any() != true) return Enumerable.Empty<ContentFragment>(); | |
var contentLinks = new List<ContentFragment>(); | |
foreach (var fragment in xhtmlString.Fragments.GetFilteredFragments(PrincipalInfo.AnonymousPrincipal)) | |
{ | |
if (!(fragment is ContentFragment)) continue; | |
var contentFragment = fragment as ContentFragment; | |
if (ContentReference.IsNullOrEmpty(contentFragment.ContentLink)) continue; | |
contentLinks.Add(contentFragment); | |
} | |
return contentLinks; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment