Skip to content

Instantly share code, notes, and snippets.

View Hangsolow's full-sized avatar

Alex Boesen Hangsolow

View GitHub Profile
@Hangsolow
Hangsolow / TextTokenFragment.cs
Created February 13, 2024 21:10
TextTokenFragment for episerver cms
public class TextTokenFragment(string textToken) : IStringFragment, IReferenceMap
{
public static readonly Guid ClassId = new("B50376E7-7850-4FEC-8211-CA603A43B907");
public static readonly string Class = "text-token";
public static readonly string TextTokenAttributeName = "data-text-token";
public string TextToken { get; } = textToken.Trim('"');
public string ReplaceText { get; set; }
@Hangsolow
Hangsolow / TextTokenFragmentElementHandler.cs
Last active February 13, 2024 21:11
TextTokenFragmentElementHandler for episerver cms
public class TextTokenFragmentElementHandler : IFragmentElementHandler
{
public bool ParseElement(FragmentParserContext context, ElementFragment element)
{
if (element is { HasAttributes: true, Token: ElementToken.Span } && FragmentHandlerHelper.ContainsClassId(element, TextTokenFragment.ClassId))
{
var textTokenAttribute = element.Attributes[TextTokenFragment.TextTokenAttributeName];
var token = new TextTokenFragment(textTokenAttribute.UnquotedValue);
context.FlushBufferedHtml();
context.ResultFragments.Add(token);
@Hangsolow
Hangsolow / CleaningContentSerializer.cs
Created April 25, 2020 15:35
An ContentApiSerializer that removes null/empty values and unused properties
public class CleaningContentSerializer : IContentApiSerializer
{
public CleaningContentSerializer(IContentApiSerializer defaultSerializer)
{
DefaultSerializer = defaultSerializer;
}
public string MediaType => DefaultSerializer.MediaType;
public Encoding Encoding => DefaultSerializer.Encoding;
private IContentApiSerializer DefaultSerializer { get; }
public string Serialize(object value)
@Hangsolow
Hangsolow / content.d.ts
Created April 22, 2020 14:41
common interfaces for content coming from Content Api
/** interface for ContentLanguage coming from episerver content delivery api */
export interface ContentLanguage {
/** the link to the this langauge version of the current content */
link: string;
/** the localized displayName for the language */
displayName: string;
/** the ISO name for the language */
name: string;
}
public class ContentTypeCodeGenerator
{
private static readonly IDictionary<Type, Func<Type, PropertyInfo, string>> TypeMappings = CreateTypeMapping();
private static readonly IEnumerable<string> KeyWords = new[] { "Url" };
public ContentTypeCodeGenerator(string filePath)
{
FilePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
}
private ILogger Logger { get; } = Log.ForContext<ContentTypeCodeGenerator>();
public string FilePath { get; }
@Hangsolow
Hangsolow / AbContentAreaPropertyModel.cs
Created April 14, 2020 09:48
ContentAreaPropertyModel
public class AbContentAreaPropertyModel : ContentAreaPropertyModel
{
private readonly Injected<IDisplayOptionService> _injectedDisplayOptionService;
public AbContentAreaPropertyModel(EPiServer.SpecializedProperties.PropertyContentArea propertyContentArea, bool excludePersonalizedContent) : base(propertyContentArea, excludePersonalizedContent)
{
}
public AbContentAreaPropertyModel(EPiServer.SpecializedProperties.PropertyContentArea propertyContentArea, bool excludePersonalizedContent, EPiServer.ContentApi.Core.ContentLoaderService contentLoaderService, EPiServer.ContentApi.Core.Serialization.IContentModelMapper contentModelMapper, EPiServer.Security.IContentAccessEvaluator accessEvaluator, EPiServer.ContentApi.Core.Security.ISecurityPrincipal principalAccessor) : base(propertyContentArea, excludePersonalizedContent, contentLoaderService, contentModelMapper, accessEvaluator, principalAccessor)
{
}
public AbContentAreaPropertyModel(EPiServer.SpecializedProperties.PropertyContentAr
@Hangsolow
Hangsolow / ContentDeliveryApiInitialization.cs
Created April 14, 2020 07:51
Content Delivery API config
[ModuleDependency(typeof(ContentApiCmsInitialization))]
public class ContentDeliveryApiInitialization : IConfigurableModule
{
public void ConfigureContainer(ServiceConfigurationContext context)
{
context.Services.Configure<ContentApiConfiguration>(config =>
{
config.Default()
.SetMinimumRoles(string.Empty)
.SetFlattenPropertyModel(true)