From 159c8e650cecc3660b1f429255b0845124afd480 Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Fri, 28 Sep 2018 11:31:13 +0300 Subject: [PATCH] Removed file extension for local MD files. --- .../Docs/Documents/ContentWithDetailsDto.cs | 5 +- .../Docs/Documents/GithubDocumentStore.cs | 4 +- .../Formatting/MarkdownDocumentConverter.cs | 50 ++++++++++++++++--- .../Pages/Documents/Project/Index.cshtml.cs | 8 +-- 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs index 02ccc06316..d1dd5ba6d7 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/ContentWithDetailsDto.cs @@ -43,7 +43,7 @@ namespace Volo.Docs.Documents public bool HasChildItems => Items != null && Items.Any(); public bool IsEmpty => Text == null && Path == null; - + public bool IsSelected(string documentName) { if (documentName == null) @@ -51,7 +51,7 @@ namespace Volo.Docs.Documents return false; } - if (string.Equals(documentName, Path, StringComparison.InvariantCultureIgnoreCase)) + if (string.Equals(documentName, Path, StringComparison.OrdinalIgnoreCase)) { return true; } @@ -71,7 +71,6 @@ namespace Volo.Docs.Documents return false; } - } public class NavigationWithDetailsDto : DocumentWithDetailsDto diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/GithubDocumentStore.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/GithubDocumentStore.cs index cbfb150527..f4504ed8e1 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/GithubDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/GithubDocumentStore.cs @@ -121,7 +121,7 @@ namespace Volo.Docs.Documents try { var urlStartingAfterFirstSlash = - url.Substring(url.IndexOf("github.com/", StringComparison.Ordinal) + "github.com/".Length); + url.Substring(url.IndexOf("github.com/", StringComparison.OrdinalIgnoreCase) + "github.com/".Length); return urlStartingAfterFirstSlash.Substring(0, urlStartingAfterFirstSlash.IndexOf('/')); } catch (Exception) @@ -135,7 +135,7 @@ namespace Volo.Docs.Documents try { var urlStartingAfterFirstSlash = - url.Substring(url.IndexOf("github.com/", StringComparison.Ordinal) + "github.com/".Length); + url.Substring(url.IndexOf("github.com/", StringComparison.OrdinalIgnoreCase) + "github.com/".Length); var urlStartingAfterSecondSlash = urlStartingAfterFirstSlash.Substring(urlStartingAfterFirstSlash.IndexOf('/') + 1); return urlStartingAfterSecondSlash.Substring(0, urlStartingAfterSecondSlash.IndexOf('/')); diff --git a/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs b/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs index cbdb13f82d..9737d6d2d4 100644 --- a/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs +++ b/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs @@ -1,4 +1,5 @@ -using System.Text; +using System; +using System.Text; using System.Text.RegularExpressions; using CommonMark; using Volo.Abp.DependencyInjection; @@ -11,6 +12,8 @@ namespace Volo.Docs.Formatting private const string NewLinkFormat = "[{0}](/documents/{1}/{2}/{3}/{4})"; + private const string MarkdownLinkRegExp = @"\[([^)]+)\]\(([^)]+." + Type + @")\)"; + public string Convert(string content) { return CommonMarkConverter.Convert(Encoding.UTF8.GetString(Encoding.Default.GetBytes(content))); @@ -19,14 +22,45 @@ namespace Volo.Docs.Formatting public string NormalizeLinks(string content, string projectShortName, string version, string documentLocalDirectory) { - return Regex.Replace(content, @"\[([^)]+)\]\(([^)]+.md)\)", delegate (Match match) - { - var displayText = match.Groups[1].Value; - var documentName = match.Groups[2].Value; - var newLink = string.Format(NewLinkFormat, displayText, projectShortName, version, documentLocalDirectory.TrimStart('/').TrimEnd('/'), documentName); - return newLink; - }); + return Regex.Replace(content, MarkdownLinkRegExp, delegate (Match match) + { + var displayText = match.Groups[1].Value; + var documentName = RemoveFileExtensionIfLocalUrl(match.Groups[2].Value); + return string.Format(NewLinkFormat, displayText, projectShortName, version, + documentLocalDirectory.TrimStart('/').TrimEnd('/'), documentName); + }); } + private static string RemoveFileExtensionIfLocalUrl(string documentName) + { + if (string.IsNullOrWhiteSpace(documentName)) + { + return documentName; + } + + if (!documentName.EndsWith(Type, StringComparison.OrdinalIgnoreCase)) + { + return documentName; + } + + if (IsRemoteUrl(documentName)) + { + return documentName; + } + + return documentName.Left(documentName.Length - Type.Length - 1); + } + + private static bool IsRemoteUrl(string url) + { + try + { + return Regex.IsMatch(url, @"\A(https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?\z"); + } + catch (Exception) + { + return true; + } + } } } diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs index 291b658676..0cd4f76566 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs @@ -75,13 +75,8 @@ namespace Volo.Docs.Pages.Documents.Project projectDto.ExtraProperties, projectDto.DocumentStoreType, DocumentNameWithExtension); Versions = versions.Select(v => new VersionInfo(v, v)).ToList(); - - //VersionInfo latestVersion = Versions.First(); - //latestVersion.DisplayText = $"{latestVersion.Version} - " + DocsAppConsts.LatestVersion; - //latestVersion.Version = latestVersion.Version; - - if (string.Equals(Version, DocsAppConsts.LatestVersion, StringComparison.InvariantCultureIgnoreCase)) + if (string.Equals(Version, DocsAppConsts.LatestVersion, StringComparison.OrdinalIgnoreCase)) { LatestVersionInfo.IsSelected = true; Version = LatestVersionInfo.Version; @@ -112,6 +107,7 @@ namespace Volo.Docs.Pages.Documents.Project Navigation = await _documentAppService.GetNavigationDocumentAsync(ProjectName, Version, false); Navigation.ConvertItems(); + } }