using TightWiki.Engine.Library; using TightWiki.Engine.Library.Interfaces; using TightWiki.Library; using TightWiki.Models; using TightWiki.Repository; using static TightWiki.Engine.Library.Constants; namespace TightWiki.Engine.Implementation { /// /// Handles links from one wiki page to another. /// public class InternalLinkHandler : IInternalLinkHandler { /// /// Handles an internal wiki link. /// /// Reference to the wiki state object /// The navigation for the linked page. /// The name of the page being linked to. /// The text which should be show in the absence of an image. /// The image that should be shown. /// The 0-100 image scale factor for the given image. public HandlerResult Handle(ITightEngineState state, NamespaceNavigation pageNavigation, string pageName, string linkText, string? image, int imageScale) { var page = PageRepository.GetPageRevisionByNavigation(pageNavigation); if (page == null) { if (state.Session?.CanCreate == true) { if (image != null) { string href; if (image.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) || image.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)) { //The image is external. href = $""; } else if (image.Contains('/')) { //The image is located on another page. href = $""; } else { //The image is located on this page, but this page does not exist. href = $"{linkText}"; } return new HandlerResult(href) { Instructions = [HandlerResultInstruction.DisallowNestedProcessing] }; } else if (linkText != null) { var href = $"{linkText}" + "?"; return new HandlerResult(href) { Instructions = [HandlerResultInstruction.DisallowNestedProcessing] }; } else { throw new Exception("No link or image was specified."); } } else { //The page does not exist and the user does not have permission to create it. if (image != null) { string mockHref; if (image.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) || image.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)) { //The image is external. mockHref = $""; } else if (image.Contains('/')) { //The image is located on another page. mockHref = $""; } else { //The image is located on this page, but this page does not exist. mockHref = $"linkText"; } return new HandlerResult(mockHref) { Instructions = [HandlerResultInstruction.DisallowNestedProcessing] }; } else if (linkText != null) { return new HandlerResult(linkText) { Instructions = [HandlerResultInstruction.DisallowNestedProcessing] }; } else { throw new Exception("No link or image was specified."); } } } else { string href; if (image != null) { if (image.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) || image.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase)) { //The image is external. href = $""; } else if (image.Contains('/')) { //The image is located on another page. href = $""; } else { //The image is located on this page. href = $""; } } else { //Just a plain ol' internal page link. href = $"{linkText}"; } return new HandlerResult(href) { Instructions = [HandlerResultInstruction.DisallowNestedProcessing] }; } } } }