Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Afterwards, register the mapper as a viable implementation for the IOutputFieldValueMapper service in your DI container. Using this approach, you could index more complex types such as Optimizely blocks.

Example (of the mapper responsible for handling Optimizely’s complex data)

Creation

Code Block
languagec#
public class OptiOutputFieldValueMapper : IOutputFieldValueMapper
{
    private static readonly Type[] ValidTypes =
    { 
        typeof(ContentReference), typeof(XhtmlString)
    };

    public virtual List<object> Map(object source, PropertyInfo property)
    {
        var value = property.GetValue(source);

        switch (value)
        {
            case null:
                return new List<object> { string.Empty };

            case ContentReference contentLink:
                return new List<object> { contentLink.ToString() };

            case XhtmlString xhtmlString:
                return new List<object> { Regex.Replace(xhtmlString.ToHtmlString(), "<.*?>", string.Empty) };

            default:
                throw new NotSupportedException($"The decorated property {property.Name} of type " +
                                                $"{property.PropertyType.Name} is not supported.");
        }
    }

    public virtual bool CanMap(PropertyInfo property)
    {
        return ValidTypes.Contains(property.PropertyType);
    }
}

...