internal static IEnumerable <(String, String)> GetLinkReplacementsForFeed(this String template, ILinkProvider linkProvider, String regexPattern, String pathToRoot, String relativePath, string fileName, String defaultLinkText) { var matches = Regex.Matches(template, regexPattern); var linkReplacements = new List <(String, String)>(); foreach (Match match in matches) { var sourceText = match.Groups[0].Value; var linkText = match.Groups[1].Value ?? defaultLinkText; var pageLink = new InternalLink(linkProvider, linkText, pathToRoot, relativePath, fileName); linkReplacements.Add((sourceText, pageLink.AsUrl(false))); } return(linkReplacements); }
internal static IEnumerable <(String, String)> GetLinkReplacementsForContentItem(this String template, IServiceProvider serviceProvider, String regexPattern, String pathToRoot, String relativePath, Enumerations.TemplateType contentItemType) { var linkProvider = serviceProvider.GetService <ILinkProvider>(); var contentRepo = serviceProvider.GetService <IContentRepository>(); MatchCollection matches = Regex.Matches(template, regexPattern); var linkReplacements = new List <(String, String)>(); foreach (Match match in matches) { var sourceText = match.Groups[0].Value; var itemSlug = match.Groups[1].Value; var linkText = match.Groups[2].Value; ContentItem item = null; if (contentItemType == Enumerations.TemplateType.ContentPage) { item = contentRepo.GetPageBySlug(itemSlug); } else if (contentItemType == Enumerations.TemplateType.PostPage) { item = contentRepo.GetPostBySlug(itemSlug); } else { throw new ArgumentException($"Invalid template type for link replacement '{contentItemType.ToString()}'. For anything other than a Page or Post, the link text must be supplied.", nameof(contentItemType)); } if (item is null) { throw new Exceptions.ContentItemNotFoundException(itemSlug); } else { // LinkText defaults to the item title if none supplied linkText = String.IsNullOrWhiteSpace(linkText) ? item.Title : linkText; var pageLink = new InternalLink(linkProvider, linkText, pathToRoot, relativePath, item.Slug); linkReplacements.Add((sourceText, pageLink.AsLink(true))); } } return(linkReplacements); }
internal static IEnumerable <(String, String)> GetLinkReplacementsForImageEmbeddings(this String template, ILinkProvider linkProvider, String regexPattern, String pathToRoot, String relativePath) { var matches = Regex.Matches(template, regexPattern); var linkReplacements = new List <(String, String)>(); foreach (Match match in matches) { var sourceText = match.Groups[0].Value; var imagePath = match.Groups[1].Value; var group2Value = match.Groups[2].Value; string linkText = String.IsNullOrWhiteSpace(group2Value) ? string.Empty : group2Value; var pageLink = new InternalLink(linkProvider, linkText, pathToRoot, relativePath, imagePath); linkReplacements.Add((sourceText, pageLink.AsImageEmbedding())); } return(linkReplacements); }
internal static IEnumerable <(String, String)> GetLinkReplacementsForFileLinks(this String template, ILinkProvider linkProvider, String regexPattern, String pathToRoot, String relativePath) { var matches = Regex.Matches(template, regexPattern); var linkReplacements = new List <(String, String)>(); foreach (Match match in matches) { var sourceText = match.Groups[0].Value; var fileName = match.Groups[1].Value; var group2Value = match.Groups[2].Value; string linkText = String.IsNullOrWhiteSpace(group2Value) ? fileName : group2Value; var targetLink = new InternalLink(linkProvider, linkText, pathToRoot, relativePath, fileName); linkReplacements.Add((sourceText, targetLink.AsLink(false))); } return(linkReplacements); }