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 94c2f316e6..c97762146d 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 @@ -50,7 +50,8 @@ namespace Volo.Docs.Documents RawRootUrl = rawRootUrl, Format = project.Format, LocalDirectory = localDirectory, - FileName = fileName + FileName = fileName, + Version = version }); } diff --git a/modules/docs/src/Volo.Docs.Web/Formatting/DocumentFormattingFactory.cs b/modules/docs/src/Volo.Docs.Web/Formatting/DocumentConverterFactory.cs similarity index 64% rename from modules/docs/src/Volo.Docs.Web/Formatting/DocumentFormattingFactory.cs rename to modules/docs/src/Volo.Docs.Web/Formatting/DocumentConverterFactory.cs index 0897bf786e..74c73590c2 100644 --- a/modules/docs/src/Volo.Docs.Web/Formatting/DocumentFormattingFactory.cs +++ b/modules/docs/src/Volo.Docs.Web/Formatting/DocumentConverterFactory.cs @@ -4,21 +4,21 @@ using Volo.Abp.DependencyInjection; namespace Volo.Docs.Formatting { - public class DocumentFormattingFactory : IDocumentFormattingFactory, ITransientDependency + public class DocumentConverterFactory : IDocumentConverterFactory, ITransientDependency { private readonly IServiceProvider _serviceProvider; - public DocumentFormattingFactory(IServiceProvider serviceProvider) + public DocumentConverterFactory(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } - public IDocumentFormatting Create(string format) + public IDocumentConverter Create(string format) { switch (format.ToLowerInvariant()) { - case MarkdownDocumentFormatting.Type: - return _serviceProvider.GetRequiredService(); + case MarkdownDocumentConverter.Type: + return _serviceProvider.GetRequiredService(); default: throw new ApplicationException($"Undefined document formatting: {format}"); } diff --git a/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentConverter.cs b/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentConverter.cs new file mode 100644 index 0000000000..ef11fc6851 --- /dev/null +++ b/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentConverter.cs @@ -0,0 +1,9 @@ +namespace Volo.Docs.Formatting +{ + public interface IDocumentConverter + { + string Convert(string content); + + string NormalizeLinks(string content, string projectShortName, string version, string documentLocalDirectory); + } +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentConverterFactory.cs b/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentConverterFactory.cs new file mode 100644 index 0000000000..20fe67f142 --- /dev/null +++ b/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentConverterFactory.cs @@ -0,0 +1,7 @@ +namespace Volo.Docs.Formatting +{ + public interface IDocumentConverterFactory + { + IDocumentConverter Create(string format); + } +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentFormatting.cs b/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentFormatting.cs deleted file mode 100644 index 5abf8fe819..0000000000 --- a/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentFormatting.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Volo.Docs.Formatting -{ - public interface IDocumentFormatting - { - string Format(string content); - } -} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentFormattingFactory.cs b/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentFormattingFactory.cs deleted file mode 100644 index 4e2eca1117..0000000000 --- a/modules/docs/src/Volo.Docs.Web/Formatting/IDocumentFormattingFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Volo.Docs.Formatting -{ - public interface IDocumentFormattingFactory - { - IDocumentFormatting Create(string format); - } -} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs b/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs new file mode 100644 index 0000000000..cbdb13f82d --- /dev/null +++ b/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs @@ -0,0 +1,32 @@ +using System.Text; +using System.Text.RegularExpressions; +using CommonMark; +using Volo.Abp.DependencyInjection; + +namespace Volo.Docs.Formatting +{ + public class MarkdownDocumentConverter : IDocumentConverter, ITransientDependency + { + public const string Type = "md"; + + private const string NewLinkFormat = "[{0}](/documents/{1}/{2}/{3}/{4})"; + + public string Convert(string content) + { + return CommonMarkConverter.Convert(Encoding.UTF8.GetString(Encoding.Default.GetBytes(content))); + } + + 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; + }); + } + + } +} diff --git a/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentFormatting.cs b/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentFormatting.cs deleted file mode 100644 index 42686ef5c2..0000000000 --- a/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentFormatting.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Text; -using CommonMark; -using Volo.Abp.DependencyInjection; - -namespace Volo.Docs.Formatting -{ - public class MarkdownDocumentFormatting : IDocumentFormatting, ITransientDependency - { - public const string Type = "md"; - - public string Format(string content) - { - byte[] bytes = Encoding.Default.GetBytes(content); - var utf8Content = Encoding.UTF8.GetString(bytes); - - return CommonMarkConverter.Convert(utf8Content); - } - } -} diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/HtmlNormalizer.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/HtmlNormalizer.cs index 023381f048..b5c05ec199 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/HtmlNormalizer.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/HtmlNormalizer.cs @@ -5,23 +5,7 @@ namespace Volo.Docs.Pages.Documents.Project { public static class HtmlNormalizer { - //todo: fix this for html content. - public static string NormalizeLinks(string content, string projectShortName, string version) - { - //var linkRegex = new Regex(@"\(([^)]+.md)\)", RegexOptions.Multiline); - //var matches = linkRegex.Matches(content); - //foreach (Match match in matches) - //{ - // var mdFile = match.Value; - // content = content.Replace(mdFile, "(/documents/" + projectShortName + "/" + version + "/" + mdFile.Replace("(", "").Replace(")", "").Replace(".md", "") + ")"); - //} - - //return content; - - return content; - } - - public static string NormalizeImages(string content, string documentRawRootUrl, string localDirectory) + public static string ReplaceImageSources(string content, string documentRawRootUrl, string localDirectory) { content = Regex.Replace(content, @"(]*)src=""([^""]*)""([^>]*>)", delegate (Match match) { 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 17b8272b18..bc8ce8e966 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 @@ -27,12 +27,12 @@ namespace Volo.Docs.Pages.Documents.Project public NavigationWithDetailsDto Navigation { get; private set; } private readonly IDocumentAppService _documentAppService; - private readonly IDocumentFormattingFactory _documentFormattingFactory; + private readonly IDocumentConverterFactory _documentConverterFactory; - public IndexModel(IDocumentAppService documentAppService, IDocumentFormattingFactory documentFormattingFactory) + public IndexModel(IDocumentAppService documentAppService, IDocumentConverterFactory documentConverterFactory) { _documentAppService = documentAppService; - _documentFormattingFactory = documentFormattingFactory; + _documentConverterFactory = documentConverterFactory; } public async Task OnGet() @@ -41,12 +41,14 @@ namespace Volo.Docs.Pages.Documents.Project .Select(v => new VersionInfo(v, v)) .ToList(); + var hasAnyVersion = Versions.Any(); - var latestVersion = Versions.First(); + AddDefaultVersionIfNotContains(); + + var latestVersion = hasAnyVersion ? Versions[1] : Versions[0]; latestVersion.DisplayText = $"{latestVersion.Version} - latest"; latestVersion.Version = latestVersion.Version; - AddDefaultVersionIfNotContains(); var versionFromUrl = Versions.FirstOrDefault(v => v.Version == Version); if (versionFromUrl != null) @@ -68,11 +70,11 @@ namespace Volo.Docs.Pages.Documents.Project } Document = await _documentAppService.GetByNameAsync(ProjectName, DocumentName, Version, true); - var documentFormatting = _documentFormattingFactory.Create(Document.Format ?? "md"); - var content = documentFormatting.Format(Document.Content); - - content = HtmlNormalizer.NormalizeImages(content, Document.RawRootUrl, Document.LocalDirectory); - content = HtmlNormalizer.NormalizeLinks(content, Document.Project.ShortName, Document.Version); + var converter = _documentConverterFactory.Create(Document.Format ?? "md"); + var content = converter.NormalizeLinks(Document.Content, Document.Project.ShortName, Document.Version, Document.LocalDirectory); + content = converter.Convert(content); + + content = HtmlNormalizer.ReplaceImageSources(content, Document.RawRootUrl, Document.LocalDirectory); Document.Content = content; Navigation = await _documentAppService.GetNavigationDocumentAsync(ProjectName, Version, false);