From de96c447bdc5d5a4f6bc8f07c3f926820c039c7d Mon Sep 17 00:00:00 2001 From: EngincanV Date: Wed, 31 Mar 2021 18:12:24 +0300 Subject: [PATCH] Docs: Create get all document links endpoint --- .../Docs/Documents/IDocumentAppService.cs | 2 + .../Volo/Docs/Documents/DocumentAppService.cs | 85 ++++++++++++++++++- .../Docs/Documents/DocsDocumentController.cs | 7 ++ 3 files changed, 92 insertions(+), 2 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 259d9edf0b..355c2319f8 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 @@ -19,5 +19,7 @@ namespace Volo.Docs.Documents Task> SearchAsync(DocumentSearchInput input); Task FullSearchEnabledAsync(); + + Task> GetLinksAsync(); } } \ 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 9d5015d942..2c9521209c 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,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Configuration; @@ -193,6 +194,87 @@ namespace Volo.Docs.Documents return await Task.FromResult(_docsElasticSearchOptions.Enable); } + public async Task> GetLinksAsync() + { + var documentLinks = new List(); + var projects = await _projectRepository.GetListAsync(); + + foreach (var project in projects) + { + var documents = await _documentRepository.GetListByProjectId(project.Id); + + foreach (var document in documents) + { + var navigationNode = await GetNavigationAsync(new GetNavigationDocumentInput + { + ProjectId = project.Id, + LanguageCode = document.LanguageCode, + Version = document.Version + }); + + navigationNode.Items?.ForEach(node => + { + documentLinks.AddIfNotContains( + GetDocumentLinks(node, documentLinks, document.LanguageCode, + project.ShortName, document.Version, document.Format) + ); + }); + } + } + + return documentLinks; + } + + private List GetDocumentLinks(NavigationNode node, List documentLinks, string languageName, + string shortName, string version, string format) + { + if (!IsExternalLink(node.Path)) + { + documentLinks.AddIfNotContains(NormalizePath(node.Path, languageName, shortName, version, format)); + } + + node.Items?.ForEach(childNode => + { + GetDocumentLinks(childNode, documentLinks, languageName, shortName, version, format); + }); + + return documentLinks; + } + + private string NormalizePath(string path, string languageCode, string projectName, string version, + string projectFormat) + { + var pathWithoutFileExtension = RemoveFileExtensionFromPath(path, projectFormat); + + //TODO: get prefix from DocsUiOptions? + var prefix = "/"; + + return prefix + languageCode + "/" + projectName + "/" + version + "/" + pathWithoutFileExtension; + } + + private string RemoveFileExtensionFromPath(string path, string projectFormat) + { + if (path == null) + { + return null; + } + + return path.EndsWith("." + projectFormat) + ? path.Left(path.Length - projectFormat.Length - 1) + : path; + } + + private static bool IsExternalLink(string path) + { + if (path.IsNullOrEmpty()) + { + return false; + } + + return path.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || + path.StartsWith("https://", StringComparison.OrdinalIgnoreCase); + } + public async Task GetParametersAsync(GetParametersDocumentInput input) { var project = await _projectRepository.GetAsync(input.ProjectId); @@ -353,7 +435,6 @@ namespace Volo.Docs.Documents } return project.ExtraProperties["VersionBranchPrefix"].ToString(); - } private GithubVersionProviderSource GetGithubVersionProviderSource(Project project) @@ -363,4 +444,4 @@ namespace Volo.Docs.Documents : GithubVersionProviderSource.Releases; } } -} +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Documents/DocsDocumentController.cs b/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Documents/DocsDocumentController.cs index d8d68ec0e8..bec1be922e 100644 --- a/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Documents/DocsDocumentController.cs +++ b/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Documents/DocsDocumentController.cs @@ -61,6 +61,13 @@ namespace Volo.Docs.Documents return DocumentAppService.FullSearchEnabledAsync(); } + [HttpGet] + [Route("links")] + public Task> GetLinksAsync() + { + return DocumentAppService.GetLinksAsync(); + } + [HttpGet] [Route("parameters")] public Task GetParametersAsync(GetParametersDocumentInput input)