Skip to content

Commit

Permalink
update for rich text content converter
Browse files Browse the repository at this point in the history
  • Loading branch information
xantari committed Dec 16, 2024
1 parent 3a1eef7 commit 6b13c44
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Kontent.Ai.Delivery/ContentItems/ModelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private IPropertyValueConverter GetValueConverter(PropertyInfo property)
// Specific type converters
if (typeof(IRichTextContent).IsAssignableFrom(property.PropertyType))
{
return new RichTextContentConverter(HtmlParser);
return new RichTextContentConverter(HtmlParser, DeliveryOptions);
}

if (typeof(IDateTimeContent).IsAssignableFrom(property.PropertyType))
Expand All @@ -389,7 +389,7 @@ private IPropertyValueConverter GetValueConverter(PropertyInfo property)
{
return new AssetElementValueConverter(DeliveryOptions);
}

return null;
}

Expand Down
37 changes: 35 additions & 2 deletions Kontent.Ai.Delivery/ContentItems/RichTextContentConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
using Kontent.Ai.Delivery.ContentItems.ContentLinks;
using Kontent.Ai.Delivery.ContentItems.RichText;
using Kontent.Ai.Delivery.ContentItems.RichText.Blocks;
using Microsoft.Extensions.Options;

namespace Kontent.Ai.Delivery.ContentItems
{
internal class RichTextContentConverter : IPropertyValueConverter<string>
{
public IHtmlParser Parser { get; }
public IOptionsMonitor<DeliveryOptions> Options { get; }

public RichTextContentConverter(IHtmlParser parser)
public RichTextContentConverter(IHtmlParser parser, IOptionsMonitor<DeliveryOptions> options)
{
Parser = parser;
Options = options;
}

public async Task<object> GetPropertyValueAsync<TElement>(PropertyInfo property, TElement contentElement, ResolvingContext context) where TElement : IContentElementValue<string>
Expand Down Expand Up @@ -56,7 +59,21 @@ public async Task<object> GetPropertyValueAsync<TElement>(PropertyInfo property,
if (img != null)
{
var assetId = Guid.Parse(img.GetAttribute("data-asset-id"));
blocks.Add(element.Images[assetId]);
if (!string.IsNullOrEmpty(Options.CurrentValue.AssetUrlReplacement))
{
var assetToReplace = element.Images[assetId];
var replacedAsset = new InlineImage()
{
Url = ReplaceAssetUrlWIthCustomAssetUrl(assetToReplace.Url),
Description = assetToReplace.Description,
Height = assetToReplace.Height,
Width = assetToReplace.Width,
ImageId = assetToReplace.ImageId
};
blocks.Add(replacedAsset);
}
else
blocks.Add(element.Images[assetId]);
}
}
else
Expand All @@ -67,5 +84,21 @@ public async Task<object> GetPropertyValueAsync<TElement>(PropertyInfo property,

return blocks;
}

/// <summary>
/// Replace the beginning part of the asset URL with the AssetUrlReplacement value.
/// </summary>
/// <param name="url">Original Asset Url</param>
/// <returns>New URL with the CDN URL replaces with AssetUrlReplacement</returns>
private string ReplaceAssetUrlWIthCustomAssetUrl(string url)
{
// Replace the beginning part of the asset URL with the AssetUrlReplacement value by taking the third forward slash as the ending point for the string replacement
var endOfUrlIndex = url.IndexOf("/", url.IndexOf("/", url.IndexOf("/", 0) + 1) + 1);
if (endOfUrlIndex > 0)
{
return Options.CurrentValue.AssetUrlReplacement + url.Substring(endOfUrlIndex);
}
return url;
}
}
}

0 comments on commit 6b13c44

Please sign in to comment.