public HtmlLinkTag Parse(HtmlLinkTag htmlLinkTag) { if (IsExternalLink(htmlLinkTag.OriginalHref)) { // Add the external-link class to all outward bound links, // except for anchors pointing to <a name=""> tags on the current page. // (# links shouldn't be treated as internal links) if (!htmlLinkTag.OriginalHref.StartsWith("#", StringComparison.Ordinal)) { htmlLinkTag.CssClass = "external-link"; } } else { string href = htmlLinkTag.OriginalHref; string upperHref = href.ToUpperInvariant(); if (upperHref.StartsWith("ATTACHMENT:", StringComparison.Ordinal) || upperHref.StartsWith("~/", StringComparison.Ordinal)) { ConvertAttachmentToFullPath(htmlLinkTag); } else if (upperHref.StartsWith("SPECIAL:", StringComparison.Ordinal)) { ConvertSpecialLinkToFullPath(htmlLinkTag); } else { ConvertInternalLinkToFullPath(htmlLinkTag); } } return(htmlLinkTag); }
private void ConvertSpecialLinkToFullPath(HtmlLinkTag htmlLinkTag) { // note: "~" is replaced by ASP.NET HttpContext string href = htmlLinkTag.OriginalHref; htmlLinkTag.Href = ConvertToAbsolutePath("~/wiki/" + href); }
private void ConvertAttachmentToFullPath(HtmlLinkTag htmlLinkTag) { string href = htmlLinkTag.OriginalHref; string upperHref = href.ToUpperInvariant(); if (upperHref.StartsWith("ATTACHMENT:", StringComparison.Ordinal)) { // Remove the attachment: part href = href.Remove(0, 11); if (!href.StartsWith("/", StringComparison.Ordinal)) { href = "/" + href; } } else if (upperHref.StartsWith("~/", StringComparison.Ordinal)) { // Remove the ~ href = href.Remove(0, 1); } // Get the full path to the attachment string attachmentsPath = _textSettings.AttachmentsUrlPath; if (attachmentsPath.EndsWith("/", StringComparison.Ordinal)) { attachmentsPath = attachmentsPath.Remove(attachmentsPath.Length - 1); } htmlLinkTag.Href = ConvertToAbsolutePath(attachmentsPath) + href; }
private void ConvertInternalLinkToFullPath(HtmlLinkTag htmlLinkTag) { string href = htmlLinkTag.OriginalHref; // Parse internal links string title = href; string querystringAndAnchor = ""; // querystrings, #anchors // Parse querystrings and # if (_querystringRegex.IsMatch(href)) { // Grab the querystring contents Match match = _querystringRegex.Match(href); querystringAndAnchor = match.Groups["querystring"].Value; // Grab the url title = href.Replace(querystringAndAnchor, ""); } else if (_anchorRegex.IsMatch(href)) { // Grab the hash contents System.Text.RegularExpressions.Match match = _anchorRegex.Match(href); querystringAndAnchor = match.Groups["hash"].Value; // Grab the url title = href.Replace(querystringAndAnchor, ""); } // For markdown, only urls with "-" in them are valid, spaces are ignored. // Remove these, so a match is made. No page title should have a "-" in?, so replacing them is ok. title = title.Replace("-", " "); // Find the page, or if it doesn't exist point to the new page url Page page = _pageRepository.GetPageByTitleAsync(title).GetAwaiter().GetResult(); if (page != null) { string encodedTitle = EncodePageTitle(page.Title); UrlActionContext actionContext = new UrlActionContext() { Action = "Index", Controller = "Wiki", Values = new { id = page.Id, title = encodedTitle } }; href = _urlHelper.Action(actionContext); href += querystringAndAnchor; } else { // e.g. /pages/new?title=xyz UrlActionContext actionContext = new UrlActionContext() { Action = "New", Controller = "Pages", Values = new { title = title } }; href = _urlHelper.Action(actionContext); htmlLinkTag.CssClass = "missing-page-link"; } htmlLinkTag.Href = href; htmlLinkTag.Target = ""; }