Skip to content

Commit

Permalink
Merge pull request #14 from blendinteractive/feature/fix-remaining-link
Browse files Browse the repository at this point in the history
Adds support for including the "remaining URL" content from LinkItems.
  • Loading branch information
mrdrbob committed Mar 7, 2024
2 parents a0a1a8d + f904873 commit 1d9edec
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/Blend.Optimizely/Blend.Optimizely.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blend.Optimizely", "Blend.Optimizely.csproj", "{4E632FE6-D7B6-41A1-8877-4BA6DC874AAC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4E632FE6-D7B6-41A1-8877-4BA6DC874AAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4E632FE6-D7B6-41A1-8877-4BA6DC874AAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4E632FE6-D7B6-41A1-8877-4BA6DC874AAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4E632FE6-D7B6-41A1-8877-4BA6DC874AAC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7A6965B6-B410-4318-BF31-124F15289F38}
EndGlobalSection
EndGlobal
11 changes: 10 additions & 1 deletion src/Blend.Optimizely/IResolvable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@ public interface IResolvable
ResolvedLink Resolve(LinkOptions options);
}

public record ResolvedLink(string? Href, string? Target);
public record ResolvedLink(string? Href, string? Target)
{
public ResolvedLink AddAdditional(string? additional)
{
if (string.IsNullOrWhiteSpace(additional))
return this;

return new ResolvedLink((this.Href ?? "") + additional, Target);
}
}
}
45 changes: 45 additions & 0 deletions src/Blend.Optimizely/LinkResolverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using EPiServer.SpecializedProperties;
using EPiServer.Web;
using EPiServer.Web.Routing;
using Microsoft.AspNetCore.Http;
using System;
using System.Globalization;
using System.Web;

namespace Blend.Optimizely
{
Expand Down Expand Up @@ -58,13 +60,56 @@ public class LinkResolverService
var resolvedContent = ResolveIContent(content, options: options);
if (resolvedContent is not null)
{
string remaining = ExtractRemainingUrl(href);
resolvedContent = resolvedContent.AddAdditional(remaining);
return resolvedContent;
}
}

return new ResolvedLink(href, null);
}

private string ExtractRemainingUrl(string href)
{
var url = new UrlBuilder(href);
string remaining = "";
if (url.QueryCollection is not null)
{
var epsremainingpath = url.QueryCollection["epsremainingpath"];
if (!string.IsNullOrEmpty(epsremainingpath))
{
remaining = epsremainingpath;
}

bool anyParameters = false;
foreach (string key in url.QueryCollection)
{
if (key is null || string.Compare(key, "epsremainingpath", true) == 0)
continue;
var value = url.QueryCollection[key];
string keyValuePair = $"{Uri.EscapeDataString(key)}={Uri.EscapeDataString(value ?? "")}";

if (!anyParameters)
{
remaining += "?";
anyParameters = true;
}
else
{
remaining += "&";
}
remaining += keyValuePair;
}
}

if (!string.IsNullOrEmpty(url.Fragment))
{
remaining += url.Fragment;
}

return remaining;
}

public virtual ResolvedLink? ResolveUrl(Url url, LinkOptions options = LinkOptions.None, string languageBranchId = "")
{
var content = UrlResolver.Service.Route(new UrlBuilder(url));
Expand Down
5 changes: 4 additions & 1 deletion src/TestSite/Controllers/Pages/HomePageController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EPiServer.Web.Mvc;
using Blend.Optimizely;
using EPiServer.Web.Mvc;
using Microsoft.AspNetCore.Mvc;
using TestSite.Models.Pages;

Expand All @@ -8,6 +9,8 @@ public class HomePageController : PageController<HomePage>
{
public virtual IActionResult Index(HomePage currentContent)
{
var links = currentContent.Links.HasValue() ? currentContent.Links.Select(x => x.ResolveUrl()!).ToList() : Enumerable.Empty<string>();

return View("~/Views/Pages/Homepage.cshtml", currentContent);
}
}
6 changes: 5 additions & 1 deletion src/TestSite/Models/Pages/HomePage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace TestSite.Models.Pages;
using EPiServer.SpecializedProperties;

namespace TestSite.Models.Pages;


[ContentType(
Expand All @@ -8,4 +10,6 @@
public class HomePage : PageData
{
public virtual ContentArea? Body { get; set; }

public virtual LinkItemCollection? Links { get; set; }
}
3 changes: 3 additions & 0 deletions src/TestSite/Views/Pages/HomePage.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
@Html.XhtmlString(block.Body)
</div>
}


@Html.PropertyFor(x => x.Links)

0 comments on commit 1d9edec

Please sign in to comment.