From 83679fb8ba7f13dff5837bab2919ea7d2893bf45 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 29 May 2019 16:58:26 +0300 Subject: [PATCH 1/9] implemented multi language on docs module basicly --- .../app/VoloDocs.Web/Pages/Index.cshtml.cs | 10 ---- .../Docs/Documents/GetDefaultDocumentInput.cs | 3 + .../Volo/Docs/Documents/GetDocumentInput.cs | 3 + .../Documents/GetDocumentResourceInput.cs | 3 + .../Documents/GetNavigationDocumentInput.cs | 3 + .../Volo/Docs/Projects/ProjectDto.cs | 2 + .../Docs/DocsApplicationAutoMapperProfile.cs | 2 +- .../Volo/Docs/Documents/DocumentAppService.cs | 55 ++++++++++++++++--- .../Volo/Docs/Projects/ProjectAppService.cs | 4 +- .../Volo/Docs/Projects/ProjectConsts.cs | 1 + .../Volo/Docs/Documents/IDocumentStore.cs | 5 +- .../Volo/Docs/Documents/LanguageConfig.cs | 11 ++++ .../Docs/Documents/LanguageConfigElement.cs | 11 ++++ .../Documents/FileSystemDocumentStore.cs | 15 ++++- .../GitHub/Documents/GithubDocumentStore.cs | 30 +++++++++- .../docs/src/Volo.Docs.Web/DocsWebModule.cs | 4 +- .../Pages/Documents/Index.cshtml.cs | 10 ---- .../Pages/Documents/Project/Index.cshtml | 6 ++ .../Pages/Documents/Project/Index.cshtml.cs | 46 ++++++++++++++-- .../Pages/Documents/Project/index.js | 13 +++++ .../Volo/Docs/GithubDocumentStore_Tests.cs | 2 +- 21 files changed, 193 insertions(+), 46 deletions(-) create mode 100644 modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs create mode 100644 modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs diff --git a/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs b/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs index 8b6ffd2cdf..018e5fac4c 100644 --- a/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs +++ b/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs @@ -22,16 +22,6 @@ namespace VoloDocs.Web.Pages { Projects = (await _projectAppService.GetListAsync()).Items; - if (Projects.Count == 1) - { - return RedirectToPage("./Documents/Project/Index", new - { - projectName = Projects[0].ShortName, - version = DocsAppConsts.Latest, - documentName = Projects[0].DefaultDocumentName - }); - } - return Page(); } } diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs index 770f43b13e..4cfab98225 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs @@ -10,5 +10,8 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } + + [StringLength(ProjectConsts.MaxLanguageCodeLength)] + public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs index 2c887fb829..825c1a7942 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs @@ -14,5 +14,8 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } + + [StringLength(ProjectConsts.MaxLanguageCodeLength)] + public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs index e34dfa5512..eb0d853e06 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs @@ -14,5 +14,8 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } + + [StringLength(ProjectConsts.MaxLanguageCodeLength)] + public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs index c9ea504272..473014eb25 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs @@ -10,5 +10,8 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } + + [StringLength(ProjectConsts.MaxLanguageCodeLength)] + public string LanguageCode { get; set; } } } \ 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 91f5c63a22..a9ec815a6c 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 @@ -26,5 +26,7 @@ namespace Volo.Docs.Projects public string DocumentStoreType { get; set; } public Dictionary ExtraProperties { get; set; } + + public Dictionary Languages { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs index d29828ad05..320dbe49c3 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs @@ -9,7 +9,7 @@ namespace Volo.Docs { public DocsApplicationAutoMapperProfile() { - CreateMap(); + CreateMap().Ignore(x=>x.Languages); CreateMap(); CreateMap() .Ignore(x => x.Project).Ignore(x => x.Contributors); 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 3a4474e877..4b214547f5 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.Diagnostics; +using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Logging; @@ -15,17 +16,20 @@ namespace Volo.Docs.Documents private readonly IProjectRepository _projectRepository; private readonly IDocumentStoreFactory _documentStoreFactory; protected IDistributedCache DocumentCache { get; } + protected IDistributedCache LanguaCache { get; } protected IDistributedCache ResourceCache { get; } public DocumentAppService( IProjectRepository projectRepository, IDocumentStoreFactory documentStoreFactory, IDistributedCache documentCache, + IDistributedCache languaCache, IDistributedCache resourceCache) { _projectRepository = projectRepository; _documentStoreFactory = documentStoreFactory; DocumentCache = documentCache; + LanguaCache = languaCache; ResourceCache = resourceCache; } @@ -33,9 +37,10 @@ namespace Volo.Docs.Documents { var project = await _projectRepository.GetAsync(input.ProjectId); - return await GetDocumentWithDetailsDto( + return await GetDocument( project, input.Name, + input.LanguageCode, input.Version ); } @@ -44,9 +49,10 @@ namespace Volo.Docs.Documents { var project = await _projectRepository.GetAsync(input.ProjectId); - return await GetDocumentWithDetailsDto( + return await GetDocument( project, project.DefaultDocumentName, + input.LanguageCode, input.Version ); } @@ -55,9 +61,10 @@ namespace Volo.Docs.Documents { var project = await _projectRepository.GetAsync(input.ProjectId); - return await GetDocumentWithDetailsDto( + return await GetDocument( project, project.NavigationDocumentName, + input.LanguageCode, input.Version ); } @@ -92,20 +99,23 @@ namespace Volo.Docs.Documents ); } - protected virtual async Task GetDocumentWithDetailsDto( + protected virtual async Task GetDocument( Project project, string documentName, + string languageCode, string version) { - var cacheKey = $"Document@{project.ShortName}#{documentName}#{version}"; + var cacheKey = $"Document@{project.ShortName}#{languageCode}#{documentName}#{version}"; async Task GetDocumentAsync() { Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the store..."); var store = _documentStoreFactory.Create(project.DocumentStoreType); - var document = await store.GetDocumentAsync(project, documentName, version); + var languages = await GetLanguageListAsync(store, project, version); + var language = GetLanguageByCode(languages, languageCode); + var document = await store.GetDocumentAsync(project, documentName, language.Code, version); Logger.LogInformation($"Document retrieved: {documentName}"); - return CreateDocumentWithDetailsDto(project, document); + return CreateDocumentWithDetailsDto(project, document, languages); } if (Debugger.IsAttached) @@ -125,11 +135,40 @@ namespace Volo.Docs.Documents ); } - protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document) + protected virtual LanguageConfigElement GetLanguageByCode(LanguageConfig languageCodes, string languageCode) + { + var language = languageCodes.Languages.FirstOrDefault(l => l.Code == languageCode); + + return language ?? languageCodes.Languages.Single(l => l.IsDefault); + } + + protected virtual async Task GetLanguageListAsync(IDocumentStore store, Project project, string version) + { + async Task GetLanguagesAsync() + { + return await store.GetLanguageListAsync(project, version); + } + + return await LanguaCache.GetOrAddAsync( + project.ShortName, + GetLanguagesAsync, + () => new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24) + } + ); + } + + protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document, LanguageConfig languages) { var documentDto = ObjectMapper.Map(document); documentDto.Project = ObjectMapper.Map(project); documentDto.Contributors = ObjectMapper.Map, List>(document.Contributors); + documentDto.Project.Languages = new Dictionary(); + foreach (var language in languages.Languages) + { + documentDto.Project.Languages.Add(language.Code,language.DisplayName); + } return documentDto; } } 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 02ffac9ee9..63fc5557ce 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 @@ -16,17 +16,15 @@ namespace Volo.Docs.Projects private readonly IProjectRepository _projectRepository; private readonly IDistributedCache> _versionCache; private readonly IDocumentStoreFactory _documentStoreFactory; - private readonly IGuidGenerator _guidGenerator; public ProjectAppService( IProjectRepository projectRepository, IDistributedCache> versionCache, - IDocumentStoreFactory documentStoreFactory, IGuidGenerator guidGenerator) + IDocumentStoreFactory documentStoreFactory) { _projectRepository = projectRepository; _versionCache = versionCache; _documentStoreFactory = documentStoreFactory; - _guidGenerator = guidGenerator; } public async Task> GetListAsync() diff --git a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs index 57f3c17112..ea5506151f 100644 --- a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs +++ b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs @@ -8,5 +8,6 @@ public const int MaxNavigationDocumentNameLength = 128; public const int MaxLatestVersionBranchNameLength = 128; public const int MaxVersionNameLength = 128; + public const int MaxLanguageCodeLength = 10; } } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs index 1b001303b5..6f6d90de49 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs @@ -1,16 +1,19 @@ using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Domain.Services; +using Volo.Docs.GitHub.Documents; using Volo.Docs.Projects; namespace Volo.Docs.Documents { public interface IDocumentStore : IDomainService { - Task GetDocumentAsync(Project project, string documentName, string version); + Task GetDocumentAsync(Project project, string documentName, string languageCode, string version); Task> GetVersionsAsync(Project project); Task GetResource(Project project, string resourceName, string version); + + Task GetLanguageListAsync(Project project, string version); } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs new file mode 100644 index 0000000000..b53497a948 --- /dev/null +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Volo.Docs.Documents +{ + public class LanguageConfig + { + public List Languages { get; set; } + } +} diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs new file mode 100644 index 0000000000..25525394ca --- /dev/null +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs @@ -0,0 +1,11 @@ +namespace Volo.Docs.Documents +{ + public class LanguageConfigElement + { + public string DisplayName { get; set; } + + public string Code { get; set; } + + public bool IsDefault { get; set; } + } +} diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs index 87a74d1702..e221dba23e 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs @@ -1,7 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Security; using System.Threading.Tasks; +using Newtonsoft.Json; using Volo.Abp.Domain.Services; using Volo.Abp.IO; using Volo.Docs.Documents; @@ -14,10 +16,10 @@ namespace Volo.Docs.FileSystem.Documents { public const string Type = "FileSystem"; - public async Task GetDocumentAsync(Project project, string documentName, string version) + public async Task GetDocumentAsync(Project project, string documentName, string languageCode, string version) { var projectFolder = project.GetFileSystemPath(); - var path = Path.Combine(projectFolder, documentName); + var path = Path.Combine(projectFolder, languageCode, documentName); CheckDirectorySecurity(projectFolder, path); @@ -46,6 +48,13 @@ namespace Volo.Docs.FileSystem.Documents return Task.FromResult(new List()); } + public async Task GetLanguageListAsync(Project project, string version) + { + var configAsJson = project.GetFileSystemPath() + "languageConfig.json"; + + return JsonConvert.DeserializeObject(configAsJson); + } + public async Task GetResource(Project project, string resourceName, string version) { var projectFolder = project.GetFileSystemPath(); diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs index 2d65a17c68..5ca5f310bf 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using Volo.Abp.Domain.Services; using Volo.Docs.Documents; using Volo.Docs.GitHub.Projects; @@ -27,14 +28,14 @@ namespace Volo.Docs.GitHub.Documents _githubRepositoryManager = githubRepositoryManager; } - public virtual async Task GetDocumentAsync(Project project, string documentName, string version) + public virtual async Task GetDocumentAsync(Project project, string documentName, string languageCode, string version) { var token = project.GetGitHubAccessTokenOrNull(); var rootUrl = project.GetGitHubUrl(version); - var rawRootUrl = CalculateRawRootUrl(rootUrl); + var userAgent = project.GetGithubUserAgentOrNull(); + var rawRootUrl = CalculateRawRootUrlWithLanguageCode(rootUrl, languageCode); var rawDocumentUrl = rawRootUrl + documentName; var commitHistoryUrl = project.GetGitHubUrlForCommitHistory() + documentName; - var userAgent = project.GetGithubUserAgentOrNull(); var isNavigationDocument = documentName == project.NavigationDocumentName; var editLink = rootUrl.ReplaceFirst("/tree/", "/blob/") + documentName; var localDirectory = ""; @@ -102,6 +103,19 @@ namespace Volo.Docs.GitHub.Documents return new DocumentResource(content); } + public async Task GetLanguageListAsync(Project project, string version) + { + var token = project.GetGitHubAccessTokenOrNull(); + var rootUrl = project.GetGitHubUrl(version); + var userAgent = project.GetGithubUserAgentOrNull(); + + var url = CalculateRawRootUrl(rootUrl) + "languageConfig.json"; + + var configAsJson = await DownloadWebContentAsStringAsync(url, token, userAgent); + + return JsonConvert.DeserializeObject(configAsJson); + } + private async Task> GetReleasesAsync(Project project) { var url = project.GetGitHubUrl(); @@ -202,6 +216,16 @@ namespace Volo.Docs.GitHub.Documents return contributors; } + private static string CalculateRawRootUrlWithLanguageCode(string rootUrl, string languageCode) + { + return (rootUrl + .Replace("github.com", "raw.githubusercontent.com") + .ReplaceFirst("/tree/", "/") + .EnsureEndsWith('/') + + languageCode + ).EnsureEndsWith('/'); + } + private static string CalculateRawRootUrl(string rootUrl) { return rootUrl diff --git a/modules/docs/src/Volo.Docs.Web/DocsWebModule.cs b/modules/docs/src/Volo.Docs.Web/DocsWebModule.cs index 89301265ea..e92a8d21a6 100644 --- a/modules/docs/src/Volo.Docs.Web/DocsWebModule.cs +++ b/modules/docs/src/Volo.Docs.Web/DocsWebModule.cs @@ -39,8 +39,8 @@ namespace Volo.Docs { //TODO: Make configurable! options.Conventions.AddPageRoute("/Documents/Project/Index", "documents/{projectName}"); - - options.Conventions.AddPageRoute("/Documents/Project/Index", "documents/{projectName}/{version}/{*documentName}"); + options.Conventions.AddPageRoute("/Documents/Project/Index", "documents/{languageCode}/{projectName}"); + options.Conventions.AddPageRoute("/Documents/Project/Index", "documents/{languageCode}/{projectName}/{version}/{*documentName}"); }); Configure(options => diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs index 552aa0c21d..32675ded2c 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs @@ -21,16 +21,6 @@ namespace Volo.Docs.Pages.Documents { var listResult = await _projectAppService.GetListAsync(); - if (listResult.Items.Count == 1) - { - return RedirectToPage("./Project/Index", new - { - projectName = listResult.Items[0].ShortName, - version = DocsAppConsts.Latest, - documentName = listResult.Items[0].DefaultDocumentName - }); - } - Projects = listResult.Items; return Page(); 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 9b98ebe4b4..3e6a8dda35 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 @@ -158,6 +158,11 @@ + @if (Model.LanguageSelectListItems.Count > 1) + { + + } + @if (!string.IsNullOrEmpty(Model.Document.EditLink)) { @L["Edit"] @@ -222,3 +227,4 @@ + \ No newline at end of file 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 378903d121..744ad1d7b9 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 @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; +using Volo.Abp.Localization; using Volo.Docs.Documents; using Volo.Docs.HtmlConverting; using Volo.Docs.Models; @@ -23,8 +24,13 @@ namespace Volo.Docs.Pages.Documents.Project [BindProperty(SupportsGet = true)] public string DocumentName { get; set; } + [BindProperty(SupportsGet = true)] + public string LanguageCode { get; set; } + public ProjectDto Project { get; set; } + public List LanguageSelectListItems { get; set; } + public string DocumentNameWithExtension { get; private set; } public DocumentWithDetailsDto Document { get; private set; } @@ -58,6 +64,8 @@ namespace Volo.Docs.Pages.Documents.Project await SetVersionAsync(); await SetDocumentAsync(); await SetNavigationAsync(); + SetLanguageSelectListItems(); + AddLanguageCodePrefixToLinks(); } private async Task SetProjectAsync() @@ -72,7 +80,7 @@ namespace Volo.Docs.Pages.Documents.Project ProjectSelectItems = projects.Items.Select(p => new SelectListItem { Text = p.Name, - Value = p.Id != Project.Id ? "/documents/" + p.ShortName + "/" + DocsAppConsts.Latest : null, + Value = p.Id != Project.Id ? "/documents/" + LanguageCode + "/" + p.ShortName + "/" + DocsAppConsts.Latest : null, Selected = p.Id == Project.Id }).ToList(); } @@ -133,6 +141,7 @@ namespace Volo.Docs.Pages.Documents.Project new GetNavigationDocumentInput { ProjectId = Project.Id, + LanguageCode = LanguageCode, Version = Version } ); @@ -154,7 +163,7 @@ namespace Volo.Docs.Pages.Documents.Project version = DocsAppConsts.Latest; } - var link = "/documents/" + ProjectName + "/" + version; + var link = "/documents/" + LanguageCode + "/" + ProjectName + "/" + version; if (documentName != null) { @@ -193,6 +202,7 @@ namespace Volo.Docs.Pages.Documents.Project new GetDefaultDocumentInput { ProjectId = Project.Id, + LanguageCode = LanguageCode, Version = Version } ); @@ -204,6 +214,7 @@ namespace Volo.Docs.Pages.Documents.Project { ProjectId = Project.Id, Name = DocumentNameWithExtension, + LanguageCode = LanguageCode, Version = Version } ); @@ -211,13 +222,35 @@ namespace Volo.Docs.Pages.Documents.Project } catch (DocumentNotFoundException) { - //TODO: Handle it! - throw; + Document = await _documentAppService.GetDefaultAsync( + new GetDefaultDocumentInput + { + ProjectId = Project.Id, + LanguageCode = LanguageCode, + Version = Version + } + ); } ConvertDocumentContentToHtml(); } + private void SetLanguageSelectListItems() + { + LanguageSelectListItems = new List(); + + foreach (var language in Document.Project.Languages) + { + LanguageSelectListItems.Add( + new SelectListItem( + language.Value, + "/documents/" + language.Key + "/" + Project.ShortName + "/" + Version + "/" + DocumentName, + language.Key == LanguageCode + ) + ); + } + } + private void ConvertDocumentContentToHtml() { var converter = _documentToHtmlConverterFactory.Create(Document.Format ?? Project.Format); @@ -238,5 +271,10 @@ namespace Volo.Docs.Pages.Documents.Project Document.Content = content; } + + private void AddLanguageCodePrefixToLinks() + { + Document.Content = Document.Content.Replace("href=\"/documents", "href=\"/documents/" + LanguageCode); + } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js index 32ce1933a7..5eeb579f43 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js @@ -2,10 +2,21 @@ $(function () { + var addLanguageCodePrefixToLinks = function () { + var anchors = $('#sidebar-scroll a'); + + for (var i = 0; i < anchors.length; i++) { + var anchor = $(anchors[i]); + var newhref = anchor.attr('href').replace("/documents", "/documents/" + $('#LanguageCode').val()); + anchor.attr('href', newhref); + } + }; + var initNavigationFilter = function (navigationContainerId) { var $navigation = $("#" + navigationContainerId); + var getShownDocumentLinks = function () { return $navigation.find(".mCSB_container > li a:visible").not(".tree-toggle"); }; @@ -108,6 +119,8 @@ initSocialShareLinks(); + addLanguageCodePrefixToLinks(); + }); })(jQuery); diff --git a/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs b/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs index 1f8bfeca77..5e60d5f96f 100644 --- a/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs +++ b/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs @@ -28,7 +28,7 @@ namespace Volo.Docs var project = await _projectRepository.FindAsync(_testData.PorjectId); project.ShouldNotBeNull(); - var document = await store.GetDocumentAsync(project, "index2", "0.123.0"); + var document = await store.GetDocumentAsync(project, "index2", "en", "0.123.0"); document.ShouldNotBeNull(); document.Title.ShouldBe("index2"); From 77fef984b88373a6cb650d3cf8ac36ef17cac76f Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 30 May 2019 08:33:20 +0300 Subject: [PATCH 2/9] multi language on docs refactor --- .../Docs/Documents/ContentWithDetailsDto.cs | 2 ++ .../Docs/DocsApplicationAutoMapperProfile.cs | 2 +- .../Volo/Docs/Documents/DocumentAppService.cs | 9 ++++--- .../Volo/Docs/Documents/LanguageConfig.cs | 6 ++--- ...nguageConfigElement.cs => LanguageInfo.cs} | 2 +- .../Documents/TagHelpers/TreeTagHelper.cs | 9 ++++--- .../Pages/Documents/Project/Index.cshtml | 25 +++++++++++-------- .../Pages/Documents/Project/Index.cshtml.cs | 5 ++++ .../Pages/Documents/Project/index.js | 13 ---------- 9 files changed, 37 insertions(+), 36 deletions(-) rename modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/{LanguageConfigElement.cs => LanguageInfo.cs} (82%) 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 44b2ef6861..43fab40c0c 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 @@ -25,6 +25,8 @@ namespace Volo.Docs.Documents public string FileName { get; set; } + public string CurrentLanguageCode { get; set; } + public ProjectDto Project { get; set; } public List Contributors { get; set; } diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs index 320dbe49c3..2f029f3a39 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs @@ -12,7 +12,7 @@ namespace Volo.Docs CreateMap().Ignore(x=>x.Languages); CreateMap(); CreateMap() - .Ignore(x => x.Project).Ignore(x => x.Contributors); + .Ignore(x => x.Project).Ignore(x => x.Contributors).Ignore(x => x.CurrentLanguageCode); CreateMap(); CreateMap(); } 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 4b214547f5..6bfe9b40ee 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 @@ -115,7 +115,7 @@ namespace Volo.Docs.Documents var language = GetLanguageByCode(languages, languageCode); var document = await store.GetDocumentAsync(project, documentName, language.Code, version); Logger.LogInformation($"Document retrieved: {documentName}"); - return CreateDocumentWithDetailsDto(project, document, languages); + return CreateDocumentWithDetailsDto(project, document, languages, language.Code); } if (Debugger.IsAttached) @@ -135,7 +135,7 @@ namespace Volo.Docs.Documents ); } - protected virtual LanguageConfigElement GetLanguageByCode(LanguageConfig languageCodes, string languageCode) + protected virtual LanguageInfo GetLanguageByCode(LanguageConfig languageCodes, string languageCode) { var language = languageCodes.Languages.FirstOrDefault(l => l.Code == languageCode); @@ -159,16 +159,19 @@ namespace Volo.Docs.Documents ); } - protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document, LanguageConfig languages) + protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document, LanguageConfig languages, string languageCode) { var documentDto = ObjectMapper.Map(document); documentDto.Project = ObjectMapper.Map(project); documentDto.Contributors = ObjectMapper.Map, List>(document.Contributors); + documentDto.Project.Languages = new Dictionary(); foreach (var language in languages.Languages) { documentDto.Project.Languages.Add(language.Code,language.DisplayName); } + + documentDto.CurrentLanguageCode = languageCode; return documentDto; } } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs index b53497a948..d98444386f 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs @@ -1,11 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace Volo.Docs.Documents { public class LanguageConfig { - public List Languages { get; set; } + public List Languages { get; set; } } } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageInfo.cs similarity index 82% rename from modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs rename to modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageInfo.cs index 25525394ca..f384dcaa76 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageInfo.cs @@ -1,6 +1,6 @@ namespace Volo.Docs.Documents { - public class LanguageConfigElement + public class LanguageInfo { public string DisplayName { get; set; } diff --git a/modules/docs/src/Volo.Docs.Web/Areas/Documents/TagHelpers/TreeTagHelper.cs b/modules/docs/src/Volo.Docs.Web/Areas/Documents/TagHelpers/TreeTagHelper.cs index 38e95a6e93..2e8ec8bde0 100644 --- a/modules/docs/src/Volo.Docs.Web/Areas/Documents/TagHelpers/TreeTagHelper.cs +++ b/modules/docs/src/Volo.Docs.Web/Areas/Documents/TagHelpers/TreeTagHelper.cs @@ -35,6 +35,9 @@ namespace Volo.Docs.Areas.Documents.TagHelpers [HtmlAttributeName("project-format")] public string ProjectFormat { get; set; } + [HtmlAttributeName("language")] + public string LanguageCode { get; set; } + public override void Process(TagHelperContext context, TagHelperOutput output) { var content = new StringBuilder(); @@ -107,7 +110,7 @@ namespace Volo.Docs.Areas.Documents.TagHelpers } else { - listInnerItem = string.Format(ListItemAnchor, NormalizePath(node.Path, node.HasChildItems), textCss, node.Text.IsNullOrEmpty() ? "?" : node.Text); + listInnerItem = string.Format(ListItemAnchor, NormalizePath(node.Path), textCss, node.Text.IsNullOrEmpty() ? "?" : node.Text); } return string.Format(LiItemTemplateWithLink, @@ -117,7 +120,7 @@ namespace Volo.Docs.Areas.Documents.TagHelpers content); } - private string NormalizePath(string path, bool hasChildItems) + private string NormalizePath(string path) { if (UrlHelper.IsExternalLink(path)) { @@ -131,7 +134,7 @@ namespace Volo.Docs.Areas.Documents.TagHelpers return "javascript:;"; } - return "/documents/" + ProjectName + "/" + Version + "/" + pathWithoutFileExtension; + return "/documents/" + LanguageCode + "/" + ProjectName + "/" + Version + "/" + pathWithoutFileExtension; } private string RemoveFileExtensionFromPath(string path) 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 3e6a8dda35..9dd8b484d7 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 @@ -107,7 +107,7 @@ } -
+
@@ -116,7 +116,7 @@
-
+
@if (Model.Navigation == null || Model.Navigation.Content.IsNullOrEmpty()) @@ -132,6 +132,7 @@ project-name="@Model.ProjectName" project-format="@Model.Project.Format" selected-document-name="@Model.DocumentNameWithExtension" + language="@Model.LanguageCode" id="sidebar-scroll" class="nav nav-list"> } @@ -158,15 +159,17 @@
- @if (Model.LanguageSelectListItems.Count > 1) - { - - } +
+ @if (Model.LanguageSelectListItems.Count > 1) + { + + } - @if (!string.IsNullOrEmpty(Model.Document.EditLink)) - { - @L["Edit"] - } + @if (!string.IsNullOrEmpty(Model.Document.EditLink)) + { + @L["Edit"] + } +
@@ -227,4 +230,4 @@
- \ No newline at end of file + \ No newline at end of file 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 744ad1d7b9..a50340294b 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 @@ -153,6 +153,8 @@ namespace Volo.Docs.Pages.Documents.Project return; } + LanguageCode = Document.CurrentLanguageCode; + Navigation.ConvertItems(); } @@ -219,6 +221,7 @@ namespace Volo.Docs.Pages.Documents.Project } ); } + } catch (DocumentNotFoundException) { @@ -232,6 +235,8 @@ namespace Volo.Docs.Pages.Documents.Project ); } + LanguageCode = Document.CurrentLanguageCode; + ConvertDocumentContentToHtml(); } diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js index 5eeb579f43..8eb2083749 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js @@ -1,17 +1,6 @@ (function ($) { $(function () { - - var addLanguageCodePrefixToLinks = function () { - var anchors = $('#sidebar-scroll a'); - - for (var i = 0; i < anchors.length; i++) { - var anchor = $(anchors[i]); - var newhref = anchor.attr('href').replace("/documents", "/documents/" + $('#LanguageCode').val()); - anchor.attr('href', newhref); - } - }; - var initNavigationFilter = function (navigationContainerId) { var $navigation = $("#" + navigationContainerId); @@ -119,8 +108,6 @@ initSocialShareLinks(); - addLanguageCodePrefixToLinks(); - }); })(jQuery); From 256e6ebce6f102b43c90872eb2e5dcf5b14e0678 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 30 May 2019 08:39:58 +0300 Subject: [PATCH 3/9] LanguageCode parameter added for DocumentAppService.GetResourceAsync --- .../Volo/Docs/Documents/DocumentAppService.cs | 4 ++-- .../Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs | 2 +- .../Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs | 2 +- .../Volo/Docs/GitHub/Documents/GithubDocumentStore.cs | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) 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 6bfe9b40ee..c7680baf1b 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 @@ -72,12 +72,12 @@ namespace Volo.Docs.Documents public async Task GetResourceAsync(GetDocumentResourceInput input) { var project = await _projectRepository.GetAsync(input.ProjectId); - var cacheKey = $"Resource@{project.ShortName}#{input.Name}#{input.Version}"; + var cacheKey = $"Resource@{project.ShortName}#{input.LanguageCode}#{input.Name}#{input.Version}"; async Task GetResourceAsync() { var store = _documentStoreFactory.Create(project.DocumentStoreType); - var documentResource = await store.GetResource(project, input.Name, input.Version); + var documentResource = await store.GetResource(project, input.Name, input.LanguageCode, input.Version); return ObjectMapper.Map(documentResource); } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs index 6f6d90de49..3cbf2c40f2 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs @@ -12,7 +12,7 @@ namespace Volo.Docs.Documents Task> GetVersionsAsync(Project project); - Task GetResource(Project project, string resourceName, string version); + Task GetResource(Project project, string resourceName, string languageCode, string version); Task GetLanguageListAsync(Project project, string version); } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs index e221dba23e..e74a7c1d83 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs @@ -55,7 +55,7 @@ namespace Volo.Docs.FileSystem.Documents return JsonConvert.DeserializeObject(configAsJson); } - public async Task GetResource(Project project, string resourceName, string version) + public async Task GetResource(Project project, string resourceName, string languageCode, string version) { var projectFolder = project.GetFileSystemPath(); var path = Path.Combine(projectFolder, resourceName); diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs index 5ca5f310bf..91c03379ab 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs @@ -91,9 +91,9 @@ namespace Volo.Docs.GitHub.Documents return versions; } - public async Task GetResource(Project project, string resourceName, string version) + public async Task GetResource(Project project, string resourceName, string languageCode, string version) { - var rawRootUrl = CalculateRawRootUrl(project.GetGitHubUrl(version)); + var rawRootUrl = CalculateRawRootUrlWithLanguageCode(project.GetGitHubUrl(version), languageCode); var content = await DownloadWebContentAsByteArrayAsync( rawRootUrl + resourceName, project.GetGitHubAccessTokenOrNull(), From 2153d4f16b362a17d0ec9cb42ced8d1c50a87e01 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 30 May 2019 08:43:30 +0300 Subject: [PATCH 4/9] Update FileSystemDocumentStore.cs --- .../Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs index e74a7c1d83..ff06abdd27 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs @@ -58,7 +58,7 @@ namespace Volo.Docs.FileSystem.Documents public async Task GetResource(Project project, string resourceName, string languageCode, string version) { var projectFolder = project.GetFileSystemPath(); - var path = Path.Combine(projectFolder, resourceName); + var path = Path.Combine(projectFolder, languageCode, resourceName); if (!DirectoryHelper.IsSubDirectoryOf(projectFolder, path)) { From 3f5d12dd7041dd26d3808b73c234448fc43d5b8c Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 30 May 2019 08:43:33 +0300 Subject: [PATCH 5/9] Update GithubDocumentStore_Tests.cs --- .../Volo/Docs/GithubDocumentStore_Tests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs b/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs index 5e60d5f96f..76d1f9f68f 100644 --- a/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs +++ b/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs @@ -60,7 +60,7 @@ namespace Volo.Docs var project = await _projectRepository.FindAsync(_testData.PorjectId); project.ShouldNotBeNull(); - var documentResource = await store.GetResource(project, "index.md", "0.123.0"); + var documentResource = await store.GetResource(project, "index.md", "en", "0.123.0"); documentResource.ShouldNotBeNull(); documentResource.Content.ShouldBe(new byte[] From 6f5eb0f63065b7712d1132b0baca5b0224689f30 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 30 May 2019 09:09:17 +0300 Subject: [PATCH 6/9] docs configure redirects --- modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs | 13 ++++++++++++- .../Volo/Docs/Projects/IProjectAppService.cs | 2 ++ .../Volo/Docs/Projects/ProjectAppService.cs | 9 +++++++++ .../Volo/Docs/Projects/DocsProjectController.cs | 7 +++++++ .../Volo.Docs.Web/Pages/Documents/Index.cshtml.cs | 13 ++++++++++++- .../Pages/Documents/Project/Index.cshtml | 1 - 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs b/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs index 018e5fac4c..25d42e3ac2 100644 --- a/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs +++ b/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs @@ -18,10 +18,21 @@ namespace VoloDocs.Web.Pages _projectAppService = projectAppService; } - public async Task OnGet() + public async Task OnGetAsync() { Projects = (await _projectAppService.GetListAsync()).Items; + if (Projects.Count == 1) + { + return RedirectToPage("./Documents/Project/Index", new + { + projectName = Projects[0].ShortName, + version = DocsAppConsts.Latest, + languageCode = await _projectAppService.GetDefaultLanguageCode(Projects[0].ShortName), + documentName = Projects[0].DefaultDocumentName + }); + } + return Page(); } } 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 5bfc221e4d..d7fdb6759d 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 @@ -9,6 +9,8 @@ namespace Volo.Docs.Projects Task> GetListAsync(); Task GetAsync(string shortName); + + Task GetDefaultLanguageCode(string shortName); Task> GetVersionsAsync(string shortName); } 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 63fc5557ce..19c4e66b7b 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 @@ -43,6 +43,15 @@ namespace Volo.Docs.Projects return ObjectMapper.Map(project); } + public async Task GetDefaultLanguageCode(string shortName) + { + var project = await _projectRepository.GetByShortNameAsync(shortName); + var store = _documentStoreFactory.Create(project.DocumentStoreType); + var languageList = await store.GetLanguageListAsync(project, project.LatestVersionBranchName); + + return languageList.Languages.Single(l=>l.IsDefault).Code; + } + public async Task> GetVersionsAsync(string shortName) { var project = await _projectRepository.GetByShortNameAsync(shortName); diff --git a/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Projects/DocsProjectController.cs b/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Projects/DocsProjectController.cs index e22df620f9..9b9df7b481 100644 --- a/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Projects/DocsProjectController.cs +++ b/modules/docs/src/Volo.Docs.HttpApi/Volo/Docs/Projects/DocsProjectController.cs @@ -34,6 +34,13 @@ namespace Volo.Docs.Projects return ProjectAppService.GetAsync(shortName); } + [HttpGet] + [Route("{shortName}/defaultLanguage")] + public Task GetDefaultLanguageCode(string shortName) + { + return ProjectAppService.GetDefaultLanguageCode(shortName); + } + [HttpGet] [Route("{shortName}/versions")] public virtual Task> GetVersionsAsync(string shortName) diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs index 32675ded2c..436b0fe8e3 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs @@ -17,10 +17,21 @@ namespace Volo.Docs.Pages.Documents _projectAppService = projectAppService; } - public async Task OnGet() + public async Task OnGetAsync() { var listResult = await _projectAppService.GetListAsync(); + if (listResult.Items.Count == 1) + { + return RedirectToPage("./Project/Index", new + { + projectName = listResult.Items[0].ShortName, + version = DocsAppConsts.Latest, + languageCode = await _projectAppService.GetDefaultLanguageCode(listResult.Items[0].ShortName), + documentName = listResult.Items[0].DefaultDocumentName + }); + } + Projects = listResult.Items; return Page(); 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 9dd8b484d7..e6991812b9 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 @@ -230,4 +230,3 @@ - \ No newline at end of file From d7f375ed84e10bc47e8ed30a7ffb7a7f295a2041 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 30 May 2019 09:13:58 +0300 Subject: [PATCH 7/9] typo fix --- .../Volo/Docs/Documents/DocumentAppService.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 c7680baf1b..95a869f296 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 @@ -16,20 +16,20 @@ namespace Volo.Docs.Documents private readonly IProjectRepository _projectRepository; private readonly IDocumentStoreFactory _documentStoreFactory; protected IDistributedCache DocumentCache { get; } - protected IDistributedCache LanguaCache { get; } + protected IDistributedCache LanguageCache { get; } protected IDistributedCache ResourceCache { get; } public DocumentAppService( IProjectRepository projectRepository, IDocumentStoreFactory documentStoreFactory, IDistributedCache documentCache, - IDistributedCache languaCache, + IDistributedCache languageCache, IDistributedCache resourceCache) { _projectRepository = projectRepository; _documentStoreFactory = documentStoreFactory; DocumentCache = documentCache; - LanguaCache = languaCache; + LanguageCache = languageCache; ResourceCache = resourceCache; } @@ -149,7 +149,7 @@ namespace Volo.Docs.Documents return await store.GetLanguageListAsync(project, version); } - return await LanguaCache.GetOrAddAsync( + return await LanguageCache.GetOrAddAsync( project.ShortName, GetLanguagesAsync, () => new DistributedCacheEntryOptions From 9305b933b7ee0751b335dc13eb0615562b832de9 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 30 May 2019 09:20:54 +0300 Subject: [PATCH 8/9] refactor --- .../Volo/Docs/Documents/DocumentAppService.cs | 4 ++-- .../Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs | 2 +- .../Documents/{LanguageInfo.cs => LanguageConfigElement.cs} | 2 +- .../Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs | 3 ++- 4 files changed, 6 insertions(+), 5 deletions(-) rename modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/{LanguageInfo.cs => LanguageConfigElement.cs} (82%) 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 95a869f296..013bcefe84 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 @@ -135,7 +135,7 @@ namespace Volo.Docs.Documents ); } - protected virtual LanguageInfo GetLanguageByCode(LanguageConfig languageCodes, string languageCode) + protected virtual LanguageConfigElement GetLanguageByCode(LanguageConfig languageCodes, string languageCode) { var language = languageCodes.Languages.FirstOrDefault(l => l.Code == languageCode); @@ -168,7 +168,7 @@ namespace Volo.Docs.Documents documentDto.Project.Languages = new Dictionary(); foreach (var language in languages.Languages) { - documentDto.Project.Languages.Add(language.Code,language.DisplayName); + documentDto.Project.Languages.Add(language.Code, language.DisplayName); } documentDto.CurrentLanguageCode = languageCode; diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs index d98444386f..038ec00cee 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs @@ -4,6 +4,6 @@ namespace Volo.Docs.Documents { public class LanguageConfig { - public List Languages { get; set; } + public List Languages { get; set; } } } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageInfo.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs similarity index 82% rename from modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageInfo.cs rename to modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs index f384dcaa76..25525394ca 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageInfo.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs @@ -1,6 +1,6 @@ namespace Volo.Docs.Documents { - public class LanguageInfo + public class LanguageConfigElement { public string DisplayName { get; set; } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs index ff06abdd27..74e2e8469e 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs @@ -50,7 +50,8 @@ namespace Volo.Docs.FileSystem.Documents public async Task GetLanguageListAsync(Project project, string version) { - var configAsJson = project.GetFileSystemPath() + "languageConfig.json"; + var path = Path.Combine(project.GetFileSystemPath(), "languageConfig.json"); + var configAsJson = await FileHelper.ReadAllTextAsync(path); return JsonConvert.DeserializeObject(configAsJson); } From d99efbce862275718cf18ee922a7893ed6d060ed Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 30 May 2019 09:23:51 +0300 Subject: [PATCH 9/9] MaxLanguageCodeLength moved to LanguageConsts --- .../Volo/Docs/Documents/GetDefaultDocumentInput.cs | 3 ++- .../Volo/Docs/Documents/GetDocumentInput.cs | 3 ++- .../Volo/Docs/Documents/GetDocumentResourceInput.cs | 3 ++- .../Volo/Docs/Documents/GetNavigationDocumentInput.cs | 3 ++- .../Volo/Docs/Language/LanguageConsts.cs | 8 ++++++++ .../Volo/Docs/Projects/ProjectConsts.cs | 1 - 6 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Language/LanguageConsts.cs diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs index 4cfab98225..c606c3be5d 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using Volo.Docs.Language; using Volo.Docs.Projects; namespace Volo.Docs.Documents @@ -11,7 +12,7 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } - [StringLength(ProjectConsts.MaxLanguageCodeLength)] + [StringLength(LanguageConsts.MaxLanguageCodeLength)] public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs index 825c1a7942..e28237c352 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using Volo.Docs.Language; using Volo.Docs.Projects; namespace Volo.Docs.Documents @@ -15,7 +16,7 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } - [StringLength(ProjectConsts.MaxLanguageCodeLength)] + [StringLength(LanguageConsts.MaxLanguageCodeLength)] public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs index eb0d853e06..1fff5a1e12 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using Volo.Docs.Language; using Volo.Docs.Projects; namespace Volo.Docs.Documents @@ -15,7 +16,7 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } - [StringLength(ProjectConsts.MaxLanguageCodeLength)] + [StringLength(LanguageConsts.MaxLanguageCodeLength)] public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs index 473014eb25..c2a614ce58 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using Volo.Docs.Language; using Volo.Docs.Projects; namespace Volo.Docs.Documents @@ -11,7 +12,7 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } - [StringLength(ProjectConsts.MaxLanguageCodeLength)] + [StringLength(LanguageConsts.MaxLanguageCodeLength)] public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Language/LanguageConsts.cs b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Language/LanguageConsts.cs new file mode 100644 index 0000000000..867cc2dd4e --- /dev/null +++ b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Language/LanguageConsts.cs @@ -0,0 +1,8 @@ + +namespace Volo.Docs.Language +{ + public static class LanguageConsts + { + public const int MaxLanguageCodeLength = 10; + } +} diff --git a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs index ea5506151f..57f3c17112 100644 --- a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs +++ b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs @@ -8,6 +8,5 @@ public const int MaxNavigationDocumentNameLength = 128; public const int MaxLatestVersionBranchNameLength = 128; public const int MaxVersionNameLength = 128; - public const int MaxLanguageCodeLength = 10; } }