예제 #1
0
    /// <summary>
    /// Generates meta tags for the given content.
    /// </summary>
    /// <param name="app">The application service</param>
    /// <param name="content">The content</param>
    /// <param name="meta">If meta tags should be generated</param>
    /// <param name="opengraph">If open graph tags should be generated</param>
    /// <param name="generator">If generator tag should be generated</param>
    /// <returns>The meta tags</returns>
    public static HtmlString MetaTags(this IApplicationService app, IMeta content, bool meta = true, bool opengraph = true, bool generator = true)
    {
        var sb = new StringBuilder();

        if (meta)
        {
            // Generate meta tags
            sb.AppendLine($"<meta name=\"robots\" content=\"{ MetaRobots(content) }\">");

            if (!string.IsNullOrWhiteSpace(content.MetaKeywords))
            {
                sb.AppendLine($"<meta name=\"keywords\" content=\"{ content.MetaKeywords }\">");
            }
            if (!string.IsNullOrWhiteSpace(content.MetaDescription))
            {
                sb.AppendLine($"<meta name=\"description\" content=\"{ content.MetaDescription }\">");
            }
        }

        if (generator)
        {
            // Generate generator tag
            sb.AppendLine($"<meta name=\"generator\" content=\"Piranha CMS { Piranha.Utils.GetAssemblyVersion(typeof(Piranha.App).Assembly) }\">");
        }

        if (opengraph)
        {
            // Generate open graph tags
            if (content is PageBase page && page.IsStartPage)
            {
                sb.AppendLine($"<meta property=\"og:type\" content=\"website\">");
            }
            else
            {
                sb.AppendLine($"<meta property=\"og:type\" content=\"article\">");
            }
            sb.AppendLine($"<meta property=\"og:title\" content=\"{ OgTitle(content) }\">");
            if (content.OgImage != null && content.OgImage.HasValue)
            {
                sb.AppendLine($"<meta property=\"og:image\" content=\"{ 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 property=\"og:image\" content=\"{ app.AbsoluteContentUrl(contentBase.PrimaryImage) }\">");
            }
            if (!string.IsNullOrWhiteSpace(OgDescription(content)))
            {
                sb.AppendLine($"<meta property=\"og:description\" content=\"{ OgDescription(content) }\">");
            }
        }