Skip to content

Commit

Permalink
Fixed block creation and link generation. Fixes #1311 & fixes #1312
Browse files Browse the repository at this point in the history
  • Loading branch information
tidyui committed Aug 20, 2020
1 parent b93d7dd commit bc1e515
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 93 deletions.
4 changes: 2 additions & 2 deletions core/Piranha.AspNetCore/HtmlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public static HtmlString MetaTags(this IApplicationService app, IMeta content, b
sb.AppendLine($"<meta name=\"og:title\" value=\"{ OgTitle(content) }\">");
if (content.OgImage != null && content.OgImage.HasValue)
{
sb.AppendLine($"<meta name=\"og:image\" value=\"{ app.AbsoluteUrl(content.OgImage) }\">");
sb.AppendLine($"<meta name=\"og:image\" value=\"{ app.AbsoluteContentUrl(content.OgImage) }\">");
}
else if (content is RoutedContentBase contentBase && contentBase.PrimaryImage != null && contentBase.PrimaryImage.HasValue)
{
// If there's no OG image specified but we have a primary image,
// default to the primary image.
sb.AppendLine($"<meta name=\"og:image\" value=\"{ app.AbsoluteUrl(contentBase.PrimaryImage) }\">");
sb.AppendLine($"<meta name=\"og:image\" value=\"{ app.AbsoluteContentUrl(contentBase.PrimaryImage) }\">");
}
if (!string.IsNullOrWhiteSpace(OgDescription(content)))
{
Expand Down
4 changes: 2 additions & 2 deletions core/Piranha.AspNetCore/Piranha.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>8.4.0</Version>
<Version>8.4.1</Version>
<Company>Piranha CMS</Company>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
Expand All @@ -12,7 +12,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

<PackageId>Piranha.AspNetCore</PackageId>
<PackageVersion>8.4.0</PackageVersion>
<PackageVersion>8.4.1</PackageVersion>
<Authors>Piranha CMS</Authors>
<Description>Piranha CMS middleware components for AspNetCore</Description>
<Copyright>Copyright (c) .NET Foundation and Contributors</Copyright>
Expand Down
140 changes: 61 additions & 79 deletions core/Piranha.AspNetCore/UrlGenerationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,6 @@ public static string AbsoluteUrl(this IApplicationService app, PostField field)
return $"{ AbsoluteUrlStart(app) }{ Url(app, field) }";
}

/// <summary>
/// Generates an absolute url for the given image block.
/// </summary>
/// <param name="app">The application service</param>
/// <param name="block">The block</param>
/// <returns>The url</returns>
public static string AbsoluteUrl(this IApplicationService app, ImageBlock block)
{
return $"{ AbsoluteUrlStart(app) }{ Url(app, block) }";
}

/// <summary>
/// Generates an absolute url for the given media field.
/// </summary>
/// <param name="app">The application service</param>
/// <param name="field">The field</param>
/// <returns>The url</returns>
public static string AbsoluteUrl(this IApplicationService app, MediaField field)
{
return $"{ AbsoluteUrlStart(app) }{ Url(app, field) }";
}

/// <summary>
/// Generates an absolute url for the given taxonomy in the
/// current archive.
Expand Down Expand Up @@ -225,36 +203,6 @@ public static string Url(this IApplicationService app, SitemapItem item)
return "";
}

/// <summary>
/// Generates a local url for the given image block.
/// </summary>
/// <param name="app">The application service</param>
/// <param name="block">The block</param>
/// <returns>The url</returns>
public static string Url(this IApplicationService app, ImageBlock block)
{
if (block != null)
{
return Url(app, block.Body);
}
return "";
}

/// <summary>
/// Generates a local url for the given media field.
/// </summary>
/// <param name="app">The application service</param>
/// <param name="field">The field</param>
/// <returns>The url</returns>
public static string Url(this IApplicationService app, MediaField field)
{
if (field != null && field.Media != null)
{
return Url(app, field.Media.PublicUrl);
}
return "";
}

/// <summary>
/// Generates a local url for the given taxonomy in the
/// current archive.
Expand Down Expand Up @@ -283,34 +231,18 @@ public static string Url(this IApplicationService app, Taxonomy taxonomy)
/// <returns>The url</returns>
public static string Url(this IApplicationService app, string slug)
{
// Make sure we got a slug
if (slug == null)
return null;

// If this is an external url, return it
if (slug.ToLower().StartsWith("http"))
{
return slug;
}

// Fix internal urls
if (slug.StartsWith("~/"))
{
slug = slug.Replace("~/", "/");
}

// Fix slug that doesn't start with /
if (!slug.StartsWith("/"))
{
slug = $"/{ slug }";
}
return GenerateUrl(app, slug, true);
}

// Append site prefix, if available
if (!string.IsNullOrEmpty(app.Site.SitePrefix))
{
slug = $"/{ app.Site.SitePrefix }{ slug }";
}
return slug;
/// <summary>
/// Generates a local url for the given content.
/// </summary>
/// <param name="app">The current application service</param>
/// <param name="url">The content url</param>
/// <returns>The url</returns>
public static string ContentUrl(this IApplicationService app, string url)
{
return GenerateUrl(app, url, false);
}

/// <summary>
Expand All @@ -324,6 +256,17 @@ public static string AbsoluteUrl(this IApplicationService app, string slug)
return $"{ AbsoluteUrlStart(app) }{ Url(app, slug) }";
}

/// <summary>
/// Generates an absolute url for the given content.
/// </summary>
/// <param name="app">The current application service</param>
/// <param name="url">The content url</param>
/// <returns>The url</returns>
public static string AbsoluteContentUrl(this IApplicationService app, string url)
{
return $"{ AbsoluteUrlStart(app) }{ ContentUrl(app, url) }";
}

/// <summary>
/// Generates the scheme://host:port segment of the url from
/// the current application request.
Expand All @@ -344,4 +287,43 @@ private static string AbsoluteUrlStart(IApplicationService app)
}
return sb.ToString();
}

/// <summary>
/// Generates a local url for the given slug.
/// </summary>
/// <param name="app">The current application service</param>
/// <param name="slug">The slug</param>
/// <param name="prefix">If site prefix should be appended</param>
/// <returns>The url</returns>
private static string GenerateUrl(this IApplicationService app, string slug, bool prefix)
{
// Make sure we got a slug
if (slug == null)
return null;

// If this is an external url, return it
if (slug.ToLower().StartsWith("http"))
{
return slug;
}

// Fix internal urls
if (slug.StartsWith("~/"))
{
slug = slug.Replace("~/", "/");
}

// Fix slug that doesn't start with /
if (!slug.StartsWith("/"))
{
slug = $"/{ slug }";
}

// Append site prefix, if available
if (prefix && !string.IsNullOrEmpty(app.Site.SitePrefix))
{
slug = $"/{ app.Site.SitePrefix }{ slug }";
}
return slug;
}
}
4 changes: 2 additions & 2 deletions core/Piranha/Piranha.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>8.4.1</Version>
<Version>8.4.2</Version>
<Company>Piranha CMS</Company>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
Expand All @@ -12,7 +12,7 @@
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

<PackageId>Piranha</PackageId>
<PackageVersion>8.4.1</PackageVersion>
<PackageVersion>8.4.2</PackageVersion>
<Authors>Piranha CMS</Authors>
<Description>Core package for Piranha CMS for .NET Core</Description>
<Copyright>Copyright (c) .NET Foundation and Contributors</Copyright>
Expand Down
16 changes: 9 additions & 7 deletions core/Piranha/Runtime/AppDataList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ public virtual TItem GetByType(Type type)
/// <returns>The item, null if not found</returns>
public virtual TItem GetByType(string typeName)
{
// Temporary hotfix for poorly formatted CLR types
// for generic types
var versionIndex = typeName.IndexOf(",");
if (versionIndex != -1)
if (typeName != null)
{
var fixedName = typeName.Substring(0, versionIndex).Replace("[[", "[");
// Temporary hotfix for poorly formatted CLR types
// for generic types
var versionIndex = typeName.IndexOf(",");
if (versionIndex != -1)
{
var fixedName = typeName.Substring(0, versionIndex).Replace("[[", "[");

return _items.SingleOrDefault(i => i.TypeName.StartsWith(fixedName));
return _items.SingleOrDefault(i => i.TypeName.StartsWith(fixedName));
}
}

return _items.SingleOrDefault(i => i.TypeName == typeName);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/RazorWeb/Pages/Partial/Teasers.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<h3>@teaser.Title.Value</h3>
@if (teaser.Image.HasValue)
{
<img src="@WebApp.Url(WebApp.Media.ResizeImage(teaser.Image, 132))">
<img src="@Url.Content(WebApp.Media.ResizeImage(teaser.Image, 132))">
}
@Html.Raw(teaser.Body)
</div>
Expand Down

0 comments on commit bc1e515

Please sign in to comment.