From 273caa2c25fe0eb6d32d2b526bd43b57a8b33d5f Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Thu, 27 Sep 2018 11:15:08 +0300 Subject: [PATCH] Removed file extension from URL. And refactored. --- .../Docs/Documents/IDocumentAppService.cs | 7 +++- .../Volo/Docs/Projects/IProjectAppService.cs | 2 ++ .../Volo/Docs/Projects/ProjectDto.cs | 4 +++ .../Volo/Docs/Documents/DocumentAppService.cs | 36 ++++++++----------- .../Docs/Documents/DocumentStoreFactory.cs | 7 ++-- .../Docs/Documents/GithubDocumentStore.cs | 15 ++++---- .../Volo/Docs/Documents/IDocumentStore.cs | 6 ++-- .../Docs/Documents/IDocumentStoreFactory.cs | 2 +- .../Volo/Docs/Projects/ProjectAppService.cs | 12 +++++++ .../Helpers/TagHelpers/TreeTagHelper.cs | 13 +++++-- .../Pages/Documents/Index.cshtml | 13 ++++--- .../Pages/Documents/Project/Index.cshtml | 3 +- .../Pages/Documents/Project/Index.cshtml.cs | 28 ++++++++++----- 13 files changed, 92 insertions(+), 56 deletions(-) diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs index cac27efac6..25a1debd00 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Application.Services; +using Volo.Docs.Projects; namespace Volo.Docs.Documents { @@ -12,6 +13,10 @@ namespace Volo.Docs.Documents Task GetNavigationDocumentAsync(string projectShortName, string version, bool normalize); - Task> GetVersions(string projectShortName, string documentName); + Task> GetVersions(string projectShortName, string defaultDocumentName, + Dictionary projectExtraProperties, + string documentStoreType, string documentName); + + Task GetDocument(ProjectDto project, string documentName, string version, bool normalize); } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs index 53524e17b9..92c2a49aeb 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/IProjectAppService.cs @@ -7,5 +7,7 @@ namespace Volo.Docs.Projects public interface IProjectAppService : IApplicationService { Task> GetListAsync(); + + Task FindByShortNameAsync(string shortName); } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs index c497bfb5e7..e6710bef3c 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs @@ -19,5 +19,9 @@ namespace Volo.Docs.Projects public Dictionary ExtraProperties { get; protected set; } public string MainWebsiteUrl { get; set; } + + public virtual string DocumentStoreType { get; protected set; } + + public virtual string Format { get; protected set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs index 4892c0015e..6174f617dc 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs @@ -1,11 +1,9 @@ using System; using System.Collections.Generic; -using System.Text.RegularExpressions; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.Application.Services; using Volo.Abp.Caching; -using Volo.Abp.Domain.Entities; using Volo.Docs.Projects; namespace Volo.Docs.Documents @@ -30,7 +28,7 @@ namespace Volo.Docs.Documents { var project = await _projectRepository.FindByShortNameAsync(projectShortName); - return await GetDocument(project, documentName, version, normalize); + return await GetDocument(ObjectMapper.Map(project), documentName, version, normalize); } public async Task GetNavigationDocumentAsync(string projectShortName, string version, bool normalize) @@ -38,14 +36,15 @@ namespace Volo.Docs.Documents var project = await _projectRepository.FindByShortNameAsync(projectShortName); return ObjectMapper.Map( - await GetDocument(project, project.NavigationDocumentName, version, normalize)); + await GetDocument(ObjectMapper.Map(project), project.NavigationDocumentName, + version, normalize)); } - private async Task GetDocument(Project project, string documentName, string version, bool normalize) + public async Task GetDocument(ProjectDto project, string documentName, string version, bool normalize) { if (project == null) { - throw new EntityNotFoundException("Project Not Found!"); + throw new ArgumentNullException(nameof(project)); } if (string.IsNullOrWhiteSpace(documentName)) @@ -53,37 +52,32 @@ namespace Volo.Docs.Documents documentName = project.DefaultDocumentName; } - IDocumentStore documentStore = _documentStoreFactory.Create(project); - Document document = await documentStore.FindDocumentByNameAsync(project, documentName, version); + IDocumentStore documentStore = _documentStoreFactory.Create(project.DocumentStoreType); + + Document document = await documentStore.FindDocumentByNameAsync(project.ExtraProperties, project.Format, documentName, version); var dto = ObjectMapper.Map(document); - dto.Project = ObjectMapper.Map(project); + dto.Project = project; return dto; } - public async Task> GetVersions(string projectShortName, string documentName) + public async Task> GetVersions(string projectShortName, string defaultDocumentName, Dictionary projectExtraProperties, + string documentStoreType, string documentName) { - var project = await _projectRepository.FindByShortNameAsync(projectShortName); - - if (project == null) - { - throw new EntityNotFoundException($"Project Not Found!"); - } - if (string.IsNullOrWhiteSpace(documentName)) { - documentName = project.DefaultDocumentName; + documentName = defaultDocumentName; } - var documentStore = _documentStoreFactory.Create(project); + var documentStore = _documentStoreFactory.Create(documentStoreType); var versions = await GetVersionsFromCache(projectShortName); if (versions == null) { - versions = await documentStore.GetVersions(project, documentName); + versions = await documentStore.GetVersions(projectExtraProperties, documentName); await SetVersionsToCache(projectShortName, versions); } @@ -100,7 +94,5 @@ namespace Volo.Docs.Documents var options = new DistributedCacheEntryOptions() { SlidingExpiration = TimeSpan.FromDays(1) }; await _distributedCache.SetAsync(projectShortName, versions, options); } - - } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentStoreFactory.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentStoreFactory.cs index 0f3ca94d6a..cad104b561 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentStoreFactory.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentStoreFactory.cs @@ -1,7 +1,6 @@ using System; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; -using Volo.Docs.Projects; namespace Volo.Docs.Documents { @@ -14,14 +13,14 @@ namespace Volo.Docs.Documents _serviceProvider = serviceProvider; } - public IDocumentStore Create(Project project) + public IDocumentStore Create(string documentStoreType) { - switch (project.DocumentStoreType) + switch (documentStoreType) { case GithubDocumentStore.Type: return _serviceProvider.GetRequiredService(); default: - throw new ApplicationException($"Undefined document store: {project.DocumentStoreType}"); + throw new ApplicationException($"Undefined document store: {documentStoreType}"); } } } 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 c97762146d..cbfb150527 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 @@ -10,7 +10,6 @@ using Microsoft.Extensions.Logging; using Octokit; using Volo.Abp.Domain.Services; using ProductHeaderValue = Octokit.ProductHeaderValue; -using Project = Volo.Docs.Projects.Project; namespace Volo.Docs.Documents { @@ -20,11 +19,11 @@ namespace Volo.Docs.Documents private const bool IsOffline = true; //use it when you don't want to get from GitHub (eg: I have no internet) - public async Task FindDocumentByNameAsync(Project project, string documentName, string version) + public async Task FindDocumentByNameAsync(Dictionary projectExtraProperties, string projectFormat, string documentName, string version) { - var rootUrl = project.ExtraProperties["GithubRootUrl"].ToString().Replace("_version_/", version + "/") - .Replace("www.", ""); - var token = project.ExtraProperties["GithubAccessToken"]?.ToString(); + var rootUrl = projectExtraProperties["GithubRootUrl"].ToString().Replace("_version_/", version + "/").Replace("www.", ""); + + var token = projectExtraProperties["GithubAccessToken"]?.ToString(); var rawRootUrl = rootUrl.Replace("github.com", token + "raw.githubusercontent.com").Replace("/tree/", "/"); var rawUrl = rawRootUrl + documentName; @@ -48,7 +47,7 @@ namespace Volo.Docs.Documents EditLink = editLink, RootUrl = rootUrl, RawRootUrl = rawRootUrl, - Format = project.Format, + Format = projectFormat, LocalDirectory = localDirectory, FileName = fileName, Version = version @@ -99,12 +98,12 @@ namespace Volo.Docs.Documents } } - public async Task> GetVersions(Project project, string documentName) + public async Task> GetVersions(Dictionary projectExtraProperties, string documentName) { try { var gitHubClient = new GitHubClient(new ProductHeaderValue("AbpWebSite")); - var url = project.ExtraProperties["GithubRootUrl"].ToString(); + var url = projectExtraProperties["GithubRootUrl"].ToString(); var releases = await gitHubClient.Repository.Release.GetAll(GetGithubOrganizationNameFromUrl(url), GetGithubRepositoryNameFromUrl(url)); diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/IDocumentStore.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/IDocumentStore.cs index 4bc3993ac5..3523774b0d 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/IDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/IDocumentStore.cs @@ -1,14 +1,14 @@ using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Domain.Services; -using Volo.Docs.Projects; namespace Volo.Docs.Documents { public interface IDocumentStore : IDomainService { - Task FindDocumentByNameAsync(Project project, string documentName, string version); + Task FindDocumentByNameAsync(Dictionary projectExtraProperties, string projectFormat, + string documentName, string version); - Task> GetVersions(Project project, string documentName); + Task> GetVersions(Dictionary projectExtraProperties, string documentName); } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/IDocumentStoreFactory.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/IDocumentStoreFactory.cs index 5336bf86b9..aaada0e7fe 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/IDocumentStoreFactory.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/IDocumentStoreFactory.cs @@ -4,6 +4,6 @@ namespace Volo.Docs.Documents { public interface IDocumentStoreFactory { - IDocumentStore Create(Project project); + IDocumentStore Create(string documentStoreType); } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs index fa0be93049..1af6da31c6 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; +using Volo.Abp.Domain.Entities; namespace Volo.Docs.Projects { @@ -22,5 +23,16 @@ namespace Volo.Docs.Projects ObjectMapper.Map, List>(projects) ); } + + public async Task FindByShortNameAsync(string shortName) + { + var project = await _projectRepository.FindByShortNameAsync(shortName); + if (project == null) + { + throw new EntityNotFoundException($"Project with the name {shortName} not found!"); + } + + return ObjectMapper.Map(project); + } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs b/modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs index 43b672dcd6..511e8539c6 100644 --- a/modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs +++ b/modules/docs/src/Volo.Docs.Web/Areas/Documents/Helpers/TagHelpers/TreeTagHelper.cs @@ -35,6 +35,9 @@ namespace Volo.Docs.Areas.Documents.Helpers.TagHelpers [HtmlAttributeName("selected-document-name")] public string SelectedDocumentName { get; set; } + [HtmlAttributeName("project-format")] + public string ProjectFormat { get; set; } + public override void Process(TagHelperContext context, TagHelperOutput output) { var content = new StringBuilder(); @@ -80,14 +83,18 @@ namespace Volo.Docs.Areas.Documents.Helpers.TagHelpers anchorCss += " opened"; } + var normalizedPath = node.Path != null && node.Path.EndsWith("." + ProjectFormat) + ? node.Path.Left(node.Path.Length - ProjectFormat.Length - 1) + : node.Path; + return string.Format(LiItemTemplate, - node.Path.IsNullOrEmpty() ? "#" : "/documents/" + ProjectName + "/" + Version + "/" + node.Path, + node.Path.IsNullOrEmpty() ? "#" : "/documents/" + ProjectName + "/" + Version + "/" + normalizedPath, node.Text.IsNullOrEmpty() ? "?" : node.Text, content, node.HasChildItems ? "nav-header" : "last-link", node.HasChildItems ? "chevron-down" : "long-arrow-right", - anchorCss , - isNodeSelected? "selected-tree" : ""); + anchorCss, + isNodeSelected ? "selected-tree" : ""); } } diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml index 32e813d544..c03a97bcea 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml @@ -1,4 +1,5 @@ @page +@using Castle.Core.Internal @using Volo.Docs @using Volo.Docs.Pages.Documents @model IndexModel @@ -10,9 +11,14 @@ } -
+ @if (Model.Projects.IsNullOrEmpty()) + { + + } @if (Model.Projects.Count > 0) {

Project(s)

@@ -33,8 +39,5 @@

} - else - { -

There is no project defined yet!

- } +
diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml index a162b4f63c..8daf579254 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml @@ -92,7 +92,8 @@ 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 ce69915a94..8267999723 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 @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using Volo.Docs.Documents; using Volo.Docs.Formatting; +using Volo.Docs.Projects; namespace Volo.Docs.Pages.Documents.Project { @@ -21,6 +22,10 @@ namespace Volo.Docs.Pages.Documents.Project [BindProperty(SupportsGet = true)] public string DocumentName { get; set; } + public string ProjectFormat { get; set; } + + public string DocumentNameWithExtension { get; private set; } + public DocumentWithDetailsDto Document { get; private set; } public List Versions { get; private set; } @@ -36,24 +41,31 @@ namespace Volo.Docs.Pages.Documents.Project private readonly IDocumentAppService _documentAppService; private readonly IDocumentConverterFactory _documentConverterFactory; + private readonly IProjectAppService _projectAppService; - public IndexModel(IDocumentAppService documentAppService, IDocumentConverterFactory documentConverterFactory) + public IndexModel(IDocumentAppService documentAppService, IDocumentConverterFactory documentConverterFactory, IProjectAppService projectAppService) { _documentAppService = documentAppService; _documentConverterFactory = documentConverterFactory; + _projectAppService = projectAppService; } public async Task OnGet() { - Versions = (await _documentAppService.GetVersions(ProjectName, DocumentName)) - .Select(v => new VersionInfo(v, v)) - .ToList(); + var projectDto = await _projectAppService.FindByShortNameAsync(ProjectName); + + ProjectFormat = projectDto.Format; + + DocumentNameWithExtension = DocumentName + "." + projectDto.Format; + + var versions = await _documentAppService.GetVersions(projectDto.ShortName, projectDto.DefaultDocumentName, + projectDto.ExtraProperties, projectDto.DocumentStoreType, DocumentNameWithExtension); - var hasAnyVersion = Versions.Any(); + Versions = versions.Select(v => new VersionInfo(v, v)).ToList(); AddDefaultVersionIfNotContains(); - var latestVersion = hasAnyVersion ? Versions[1] : Versions[0]; + var latestVersion = Versions.Count == 1 ? Versions[0] : Versions[1]; latestVersion.DisplayText = $"{latestVersion.Version} - latest"; latestVersion.Version = latestVersion.Version; @@ -77,8 +89,8 @@ namespace Volo.Docs.Pages.Documents.Project Version = Versions.Single(x => x.IsSelected).Version; } - Document = await _documentAppService.GetByNameAsync(ProjectName, DocumentName, Version, true); - var converter = _documentConverterFactory.Create(Document.Format ?? "md"); + Document = await _documentAppService.GetByNameAsync(ProjectName, DocumentNameWithExtension, Version, true); + var converter = _documentConverterFactory.Create(Document.Format ?? projectDto.Format); var content = converter.NormalizeLinks(Document.Content, Document.Project.ShortName, Document.Version, Document.LocalDirectory); content = converter.Convert(content);