diff --git a/CHANGELOG.md b/CHANGELOG.md index ff9a776..01a5e75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,22 @@ All notable changes to this project will be documented in this file. +## [1.8.0] + +### Added + +- Import/Export functionality (#22). + +### Fixed + +- Potential issue in `DefaultLinkHtmlSerializer` where data would be lost if no link url was present. + +## [1.7.0] + +### Added + +- Default value handling for `LinkData` properties (#21). + ## [1.6.2] ### Fixed diff --git a/docs/example-link-data-collection.md b/docs/example-link-data-collection.md index 7ca0922..4e87991 100644 --- a/docs/example-link-data-collection.md +++ b/docs/example-link-data-collection.md @@ -50,3 +50,15 @@ public virtual LinkDataCollection Thumbnails { get; set; } ``` ![Property looks like this](./images/thumbnal-links.png) + +## Register type for import/export + +In Startup.cs + +``` +public void ConfigureServices(IServiceCollection services) +{ + ... + services.AddLinkDataExportTransform(); +} +``` diff --git a/src/Geta.Optimizely.GenericLinks/Extensions/DependencyInjectionExtensions.cs b/src/Geta.Optimizely.GenericLinks/Extensions/DependencyInjectionExtensions.cs new file mode 100644 index 0000000..e392619 --- /dev/null +++ b/src/Geta.Optimizely.GenericLinks/Extensions/DependencyInjectionExtensions.cs @@ -0,0 +1,16 @@ +using EPiServer.Core.Transfer; +using Geta.Optimizely.GenericLinks; +using Geta.Optimizely.GenericLinks.Transfer; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class DependencyInjectionExtensions + { + public static void AddLinkDataExportTransform(this IServiceCollection services) + where TLinkData : LinkData, new() + { + services.AddSingleton>().Forward, IPropertyTransform>(); + services.AddSingleton>().Forward, IPropertyTransform>(); + } + } +} diff --git a/src/Geta.Optimizely.GenericLinks/Html/DefaultLinkHtmlSerializer.cs b/src/Geta.Optimizely.GenericLinks/Html/DefaultLinkHtmlSerializer.cs index c74df72..bc6b893 100644 --- a/src/Geta.Optimizely.GenericLinks/Html/DefaultLinkHtmlSerializer.cs +++ b/src/Geta.Optimizely.GenericLinks/Html/DefaultLinkHtmlSerializer.cs @@ -41,11 +41,6 @@ public virtual string Serialize(LinkDataCollection? links, public virtual string? CreateLink(string? hrefValue, ILinkData linkData) { - if (string.IsNullOrEmpty(hrefValue)) - { - return WebUtility.HtmlEncode(linkData.Text); - } - var stringBuilder = new StringBuilder(); stringBuilder.Append(" : PropertyTransform> + where TLinkData : ILinkData, new() + { + private readonly IContentLoader _contentLoader; + private readonly ILinkHtmlSerializer _linkHtmlSerializer; + private readonly IImplicitContentExporter _implicitContentExporter; + + public PropertyLinkDataCollectionTransform( + IImplicitContentExporter implicitContentExporter, + ILinkHtmlSerializer linkHtmlSerializer, + IContentLoader contentLoader) + { + _implicitContentExporter = implicitContentExporter; + _linkHtmlSerializer = linkHtmlSerializer; + _contentLoader = contentLoader; + } + + protected override bool TransformForExport( + PropertyLinkDataCollection source, + RawProperty output, + PropertyExportContext context) + { + if (source.Value is null) + { + output.Value = null; + return true; + } + + var sourceLinks = source.Links!.Select(l => l.ReferencedPermanentLinkIds); + + foreach (var guids in sourceLinks) + { + foreach (var contentGuid in guids) + { + if (_contentLoader.TryGet(contentGuid, out var content)) + _implicitContentExporter.ExportDependentContent(content, context.TransferContext); + } + } + + output.Value = _linkHtmlSerializer.Serialize(source.Links, StringMode.InternalMode); + return true; + } + } +} diff --git a/src/Geta.Optimizely.GenericLinks/Transfer/PropertyLinkDataTransform.cs b/src/Geta.Optimizely.GenericLinks/Transfer/PropertyLinkDataTransform.cs new file mode 100644 index 0000000..08edadb --- /dev/null +++ b/src/Geta.Optimizely.GenericLinks/Transfer/PropertyLinkDataTransform.cs @@ -0,0 +1,47 @@ +// Copyright (c) Geta Digital. All rights reserved. +// Licensed under Apache-2.0. See the LICENSE file in the project root for more information + +using EPiServer.Core.Transfer; +using EPiServer; +using EPiServer.Core.Transfer.Internal; +using EPiServer.Core; +using Geta.Optimizely.GenericLinks.Html; + +namespace Geta.Optimizely.GenericLinks.Transfer +{ + public sealed class PropertyLinkDataTransform : PropertyTransform> + where TLinkData : LinkData, new() + { + private readonly IContentLoader _contentLoader; + private readonly IImplicitContentExporter _implicitContentExporter; + private readonly ILinkHtmlSerializer _linkHtmlSerializer; + + public PropertyLinkDataTransform( + IImplicitContentExporter implicitContentExporter, + ILinkHtmlSerializer linkHtmlSerializer, + IContentLoader contentLoader) + { + _implicitContentExporter = implicitContentExporter; + _linkHtmlSerializer = linkHtmlSerializer; + _contentLoader = contentLoader; + } + + protected override bool TransformForExport(PropertyLinkData source, RawProperty output, PropertyExportContext context) + { + if (source.Value is null) + { + output.Value = null; + return true; + } + + foreach (var referencedPermanentLinkId in source.Link!.ReferencedPermanentLinkIds) + { + if (_contentLoader.TryGet(referencedPermanentLinkId, out IContent content)) + _implicitContentExporter.ExportDependentContent(content, context.TransferContext); + } + + output.Value = _linkHtmlSerializer.Serialize(source.Link, StringMode.InternalMode); + return true; + } + } +}