using Microsoft.Extensions.Logging; using System.Reflection; using System.Text; namespace zero.Metadata; public class MetadataService : IMetadataService { protected ILogger Logger { get; private set; } protected ILocalizer Localizer { get; private set; } public MetadataService(ILogger logger, ILocalizer localizer) { Logger = logger; Localizer = localizer; } /// public virtual Metadata Generate(string pageName, MetadataOptions options) { Metadata model = new() { Description = options.Description, Icon = options.Icon, Image = options.Image, Author = options.Author, PageName = pageName, AppVersion = Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion }; // generate robots bool noIndex = options.NoIndex ?? false; bool noFollow = options.NoFollow ?? false; model.Robots = String.Format("{0},{1}", noIndex ? "noindex" : "index", noFollow ? "nofollow" : "follow"); // title HashSet fragments = options.TitleFragments.Where(x => !x.IsNullOrWhiteSpace()).Reverse().ToHashSet(); model.Title = GenerateTitle(pageName, fragments, options); // add schema //if (options.Schema != null) //{ // model.Schema = options.Schema.ToJson(); //} return model; } /// /// Generates the page title based on passed fragments /// protected virtual string GenerateTitle(string pageName, HashSet fragments, MetadataOptions options) { StringBuilder sb = new(); if (fragments.Count != 0) { sb.Append(string.Join(options.TitleFragmentsSeparator, fragments.Select(fragment => Localizer.Maybe(fragment)))); if (!options.HidePageName) { sb.Append(options.TitlePageNameToFragmentSeparator); sb.Append(pageName); } } else { sb.Append(pageName); } return sb.ToString(); } } public interface IMetadataService { /// /// Generates metadata. /// Metadata Generate(string pageName, MetadataOptions options); }