diff --git a/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/Projects/CreateProjectDto.cs b/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/Projects/CreateProjectDto.cs index 2427d2d51f..eb603b334c 100644 --- a/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/Projects/CreateProjectDto.cs +++ b/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/Projects/CreateProjectDto.cs @@ -14,6 +14,8 @@ namespace Volo.Docs.Admin.Projects public string NavigationDocumentName { get; set; } + public string ParametersDocumentName { get; set; } + public string MinimumVersion { get; set; } public string MainWebsiteUrl { get; set; } diff --git a/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/Projects/UpdateProjectDto.cs b/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/Projects/UpdateProjectDto.cs index b2ac8472ae..9e94cec66e 100644 --- a/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/Projects/UpdateProjectDto.cs +++ b/modules/docs/src/Volo.Docs.Admin.Application.Contracts/Volo/Docs/Admin/Projects/UpdateProjectDto.cs @@ -12,6 +12,8 @@ namespace Volo.Docs.Admin.Projects public string NavigationDocumentName { get; set; } + public string ParametersDocumentName { get; set; } + public string MinimumVersion { get; set; } public string MainWebsiteUrl { get; set; } diff --git a/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Projects/ProjectAdminAppService.cs b/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Projects/ProjectAdminAppService.cs index d1812ec410..d5aef3e614 100644 --- a/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Projects/ProjectAdminAppService.cs +++ b/modules/docs/src/Volo.Docs.Admin.Application/Volo/Docs/Admin/Projects/ProjectAdminAppService.cs @@ -53,7 +53,8 @@ namespace Volo.Docs.Admin.Projects input.DocumentStoreType, input.Format, input.DefaultDocumentName, - input.NavigationDocumentName + input.NavigationDocumentName, + input.ParametersDocumentName ) { MinimumVersion = input.MinimumVersion, diff --git a/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Create.cshtml.cs b/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Create.cshtml.cs index 4e5cbd1fbc..dc4af5e94c 100644 --- a/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Create.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Create.cshtml.cs @@ -88,6 +88,9 @@ namespace Volo.Docs.Admin.Pages.Docs.Admin.Projects [StringLength(ProjectConsts.MaxNavigationDocumentNameLength)] public string NavigationDocumentName { get; set; } = "docs-nav.json"; + [StringLength(ProjectConsts.MaxParametersDocumentNameLength)] + public string ParametersDocumentName { get; set; } = "docs-params.json"; + [StringLength(ProjectConsts.MaxVersionNameLength)] public string MinimumVersion { get; set; } diff --git a/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Edit.cshtml.cs b/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Edit.cshtml.cs index e1b83b8177..49b569d18c 100644 --- a/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Edit.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Admin.Web/Pages/Docs/Admin/Projects/Edit.cshtml.cs @@ -96,6 +96,9 @@ namespace Volo.Docs.Admin.Pages.Docs.Admin.Projects [StringLength(ProjectConsts.MaxNavigationDocumentNameLength)] public string NavigationDocumentName { get; set; } + [StringLength(ProjectConsts.MaxParametersDocumentNameLength)] + public string ParametersDocumentName { get; set; } + [StringLength(ProjectConsts.MaxVersionNameLength)] public string MinimumVersion { get; set; } diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentParameterDto.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentParameterDto.cs new file mode 100644 index 0000000000..ea387b2794 --- /dev/null +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentParameterDto.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace Volo.Docs.Documents +{ + public class DocumentParameterDto + { + public string Name { get; set; } + + public string DisplayName { get; set; } + + public Dictionary Values { get; set; } + } +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentParametersDto.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentParametersDto.cs new file mode 100644 index 0000000000..0e1d1fdb9c --- /dev/null +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/DocumentParametersDto.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace Volo.Docs.Documents +{ + public class DocumentParametersDto + { + public List Parameters {get;set;} + } +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetParametersDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetParametersDocumentInput.cs new file mode 100644 index 0000000000..33bd25b394 --- /dev/null +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetParametersDocumentInput.cs @@ -0,0 +1,19 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Volo.Docs.Language; +using Volo.Docs.Projects; + +namespace Volo.Docs.Documents +{ + public class GetParametersDocumentInput + { + public Guid ProjectId { get; set; } + + [StringLength(ProjectConsts.MaxVersionNameLength)] + public string Version { get; set; } + + [Required] + [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/IDocumentAppService.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/IDocumentAppService.cs index fa8345a7c3..cfc4840824 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 @@ -11,6 +11,8 @@ namespace Volo.Docs.Documents Task GetNavigationAsync(GetNavigationDocumentInput input); + Task GetParametersAsync(GetParametersDocumentInput input); + Task GetResourceAsync(GetDocumentResourceInput input); } } \ 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 186f9827b0..b3b2a05b53 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,10 @@ using System; using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; -using Volo.Abp.Application.Services; +using Newtonsoft.Json; using Volo.Abp.Caching; using Volo.Docs.Projects; @@ -18,19 +17,21 @@ namespace Volo.Docs.Documents protected IDistributedCache DocumentCache { get; } protected IDistributedCache LanguageCache { get; } protected IDistributedCache ResourceCache { get; } - + protected IHostEnvironment HostEnvironment { get; } public DocumentAppService( IProjectRepository projectRepository, IDocumentStoreFactory documentStoreFactory, IDistributedCache documentCache, IDistributedCache languageCache, - IDistributedCache resourceCache) + IDistributedCache resourceCache, + IHostEnvironment hostEnvironment) { _projectRepository = projectRepository; _documentStoreFactory = documentStoreFactory; DocumentCache = documentCache; LanguageCache = languageCache; ResourceCache = resourceCache; + HostEnvironment = hostEnvironment; } public virtual async Task GetAsync(GetDocumentInput input) @@ -83,7 +84,7 @@ namespace Volo.Docs.Documents return ObjectMapper.Map(documentResource); } - if (Debugger.IsAttached) + if (HostEnvironment.IsDevelopment()) { return await GetResourceAsync(); } @@ -100,6 +101,28 @@ namespace Volo.Docs.Documents ); } + public async Task GetParametersAsync(GetParametersDocumentInput input) + { + var project = await _projectRepository.GetAsync(input.ProjectId); + + try + { + var document = await GetDocumentWithDetailsDtoAsync( + project, + project.ParametersDocumentName, + input.LanguageCode, + input.Version + ); + + return JsonConvert.DeserializeObject(document.Content); + } + catch (DocumentNotFoundException) + { + Logger.LogWarning($"Parameter file ({project.ParametersDocumentName}) not found."); + return new DocumentParametersDto(); + } + } + protected virtual async Task GetDocumentWithDetailsDtoAsync( Project project, string documentName, @@ -119,7 +142,7 @@ namespace Volo.Docs.Documents return CreateDocumentWithDetailsDto(project, document); } - if (Debugger.IsAttached) + if (HostEnvironment.IsDevelopment()) { return await GetDocumentAsync(); } @@ -143,5 +166,6 @@ namespace Volo.Docs.Documents documentDto.Contributors = ObjectMapper.Map, List>(document.Contributors); return documentDto; } + } } \ No newline at end of file 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..927a1a4c59 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 @@ -6,6 +6,7 @@ public const int MaxShortNameLength = 32; public const int MaxDefaultDocumentNameLength = 128; public const int MaxNavigationDocumentNameLength = 128; + public const int MaxParametersDocumentNameLength = 128; public const int MaxLatestVersionBranchNameLength = 128; public const int MaxVersionNameLength = 128; } 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 a5c81d46fc..bd399a4f83 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 @@ -36,6 +36,7 @@ namespace Volo.Docs.GitHub.Documents var rawDocumentUrl = rawRootUrl + documentName; var commitHistoryUrl = project.GetGitHubUrlForCommitHistory() + documentName; var isNavigationDocument = documentName == project.NavigationDocumentName; + var isParameterDocument = documentName == project.ParametersDocumentName; var editLink = rootUrl.ReplaceFirst("/tree/", "/blob/") + languageCode + "/" + documentName; var localDirectory = ""; var fileName = documentName; @@ -56,7 +57,7 @@ namespace Volo.Docs.GitHub.Documents LocalDirectory = localDirectory, FileName = fileName, Contributors = new List(), - //Contributors = !isNavigationDocument ? await GetContributors(commitHistoryUrl, token, userAgent): new List(), + //Contributors = !isNavigationDocument && !isParameterDocument ? await GetContributors(commitHistoryUrl, token, userAgent): new List(), Version = version, Content = await DownloadWebContentAsStringAsync(rawDocumentUrl, token, userAgent) }; diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Projects/Project.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Projects/Project.cs index 2a98ea6cff..f1cfd0d777 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Projects/Project.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Projects/Project.cs @@ -34,6 +34,11 @@ namespace Volo.Docs.Projects /// public virtual string NavigationDocumentName { get; protected set; } + /// + /// The document to be used for the parameters file (index). + /// + public virtual string ParametersDocumentName { get; protected set; } + public virtual string MinimumVersion { get; set; } /// @@ -56,8 +61,9 @@ namespace Volo.Docs.Projects [NotNull] string shortName, [NotNull] string documentStoreType, [NotNull] string format, - [NotNull] string defaultDocumentName, - [NotNull] string navigationDocumentName) + [NotNull] string defaultDocumentName = "Index", + [NotNull] string navigationDocumentName = "docs-nav.json", + [NotNull] string parametersDocumentName = "docs-params.json") { Id = id; @@ -67,6 +73,7 @@ namespace Volo.Docs.Projects Format = Check.NotNullOrWhiteSpace(format, nameof(format)); DefaultDocumentName = Check.NotNullOrWhiteSpace(defaultDocumentName, nameof(defaultDocumentName)); NavigationDocumentName = Check.NotNullOrWhiteSpace(navigationDocumentName, nameof(navigationDocumentName)); + ParametersDocumentName = Check.NotNullOrWhiteSpace(parametersDocumentName, nameof(parametersDocumentName)); ExtraProperties = new Dictionary(); } @@ -86,6 +93,11 @@ namespace Volo.Docs.Projects NavigationDocumentName = Check.NotNullOrWhiteSpace(navigationDocumentName, nameof(navigationDocumentName)); } + public void SetParametersDocumentName(string parametersDocumentName) + { + ParametersDocumentName = Check.NotNullOrWhiteSpace(parametersDocumentName, nameof(parametersDocumentName)); + } + public void SetDefaultDocumentName(string defaultDocumentName) { DefaultDocumentName = Check.NotNullOrWhiteSpace(defaultDocumentName, nameof(defaultDocumentName)); diff --git a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/EntityFrameworkCore/DocsDbContextModelBuilderExtensions.cs b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/EntityFrameworkCore/DocsDbContextModelBuilderExtensions.cs index b9cada3e4d..f6a17b68d7 100644 --- a/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/EntityFrameworkCore/DocsDbContextModelBuilderExtensions.cs +++ b/modules/docs/src/Volo.Docs.EntityFrameworkCore/Volo/Docs/EntityFrameworkCore/DocsDbContextModelBuilderExtensions.cs @@ -33,6 +33,7 @@ namespace Volo.Docs.EntityFrameworkCore b.Property(x => x.ShortName).IsRequired().HasMaxLength(ProjectConsts.MaxShortNameLength); b.Property(x => x.DefaultDocumentName).IsRequired().HasMaxLength(ProjectConsts.MaxDefaultDocumentNameLength); b.Property(x => x.NavigationDocumentName).IsRequired().HasMaxLength(ProjectConsts.MaxNavigationDocumentNameLength); + b.Property(x => x.ParametersDocumentName).IsRequired().HasMaxLength(ProjectConsts.MaxParametersDocumentNameLength); b.Property(x => x.LatestVersionBranchName).HasMaxLength(ProjectConsts.MaxLatestVersionBranchNameLength); }); } 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 54244bbe01..dd60b2de06 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 @@ -45,5 +45,10 @@ namespace Volo.Docs.Documents { return DocumentAppService.GetResourceAsync(input); } + + public Task GetParametersAsync(GetParametersDocumentInput input) + { + return DocumentAppService.GetParametersAsync(input); + } } } diff --git a/modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentParseParamaters.cs b/modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentParseParamaters.cs new file mode 100644 index 0000000000..7d9f26832f --- /dev/null +++ b/modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentParseParamaters.cs @@ -0,0 +1,8 @@ +using System.Collections.Generic; + +namespace Volo.Docs.HtmlConverting +{ + public class DocumentRenderParameters : Dictionary + { + } +} \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentSectionConsts.cs b/modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentSectionConsts.cs new file mode 100644 index 0000000000..ba8d566563 --- /dev/null +++ b/modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentSectionConsts.cs @@ -0,0 +1,12 @@ +namespace Volo.Docs.HtmlConverting +{ + public class DocumentSectionConsts + { + public const string SectionOpenerPrefix = "[doc-section:"; + public const string SectionOpenerKeyValueSeparator = "="; + public const string SectionOpenerAndCondition = "&"; + public const string SectionOpenerOrCondition = "|"; + public const string SectionOpenerPostfix = "]"; + public const string SectionCloser = "[/doc-section]"; + } +} diff --git a/modules/docs/src/Volo.Docs.Web/HtmlConverting/HtmlNormalizer.cs b/modules/docs/src/Volo.Docs.Web/HtmlConverting/HtmlNormalizer.cs index ed3f999614..6f0a680ce2 100644 --- a/modules/docs/src/Volo.Docs.Web/HtmlConverting/HtmlNormalizer.cs +++ b/modules/docs/src/Volo.Docs.Web/HtmlConverting/HtmlNormalizer.cs @@ -28,7 +28,6 @@ namespace Volo.Docs.HtmlConverting }, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline); - return content; } @@ -36,6 +35,5 @@ namespace Volo.Docs.HtmlConverting { return Regex.Replace(content, "", "", RegexOptions.IgnoreCase); } - } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/HtmlConverting/IDocumentSectionRenderer.cs b/modules/docs/src/Volo.Docs.Web/HtmlConverting/IDocumentSectionRenderer.cs new file mode 100644 index 0000000000..2d9511d05b --- /dev/null +++ b/modules/docs/src/Volo.Docs.Web/HtmlConverting/IDocumentSectionRenderer.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Docs.HtmlConverting +{ + public interface IDocumentSectionRenderer: ITransientDependency + { + Task RenderAsync(string doucment, DocumentRenderParameters parameters); + + Task>> GetAvailableParametersAsync(string document); + } +} diff --git a/modules/docs/src/Volo.Docs.Web/HtmlConverting/ScribanDocumentSectionParser.cs b/modules/docs/src/Volo.Docs.Web/HtmlConverting/ScribanDocumentSectionParser.cs new file mode 100644 index 0000000000..549a285d0c --- /dev/null +++ b/modules/docs/src/Volo.Docs.Web/HtmlConverting/ScribanDocumentSectionParser.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Newtonsoft.Json; +using Scriban; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Volo.Docs.HtmlConverting +{ + public class ScribanDocumentSectionRenderer : IDocumentSectionRenderer + { + private const string jsonOpener = "````json"; + private const string jsonCloser = "````"; + private const string docs_param = "//[doc-params]"; + + public ILogger Logger { get; set; } + + public ScribanDocumentSectionRenderer() + { + Logger = NullLogger.Instance; + } + + public async Task RenderAsync(string document, DocumentRenderParameters parameters = null) + { + Template scribanTemplate = Template.Parse(document); + + if (parameters == null) + { + return await scribanTemplate.RenderAsync(); + } + + var result = await scribanTemplate.RenderAsync(parameters); + return RemoveOptionsJson(result); + } + + public async Task>> GetAvailableParametersAsync(string document) + { + try + { + if (!document.Contains(jsonOpener) || !document.Contains(docs_param)) + { + return new Dictionary>(); + } + + var (jsonBeginningIndex, JsonEndingIndex, insideJsonSection) = GetJsonBeginEndIndexesAndPureJson(document); + + if (jsonBeginningIndex < 0 || JsonEndingIndex <= 0 || string.IsNullOrWhiteSpace(insideJsonSection)) + { + return new Dictionary>(); + } + + var pureJson = insideJsonSection.Replace(docs_param, "").Trim(); + + return JsonConvert.DeserializeObject>>(pureJson); + } + catch (Exception) + { + Logger.LogWarning("Unable to parse parameters of document."); + return new Dictionary>(); + } + } + + private string RemoveOptionsJson(string document) + { + var orgDocument = document; + try + { + if (!document.Contains(jsonOpener) || !document.Contains(docs_param)) + { + return orgDocument; + } + + var (jsonBeginningIndex, JsonEndingIndex, insideJsonSection) = GetJsonBeginEndIndexesAndPureJson(document); + + if (jsonBeginningIndex < 0 || JsonEndingIndex <= 0 || string.IsNullOrWhiteSpace(insideJsonSection)) + { + return orgDocument; + } + + return document.Remove( + jsonBeginningIndex - jsonOpener.Length, (JsonEndingIndex + jsonCloser.Length) - (jsonBeginningIndex - jsonOpener.Length) + ); + } + catch (Exception) + { + return orgDocument; + } + } + + private (int, int, string) GetJsonBeginEndIndexesAndPureJson(string document) + { + var searchedIndex = 0; + + while (searchedIndex < document.Length) + { + var jsonBeginningIndex = document.Substring(searchedIndex).IndexOf(jsonOpener) + jsonOpener.Length + searchedIndex; + + if (jsonBeginningIndex < 0) + { + return (-1,-1,""); + } + + var JsonEndingIndex = document.Substring(jsonBeginningIndex).IndexOf(jsonCloser) + jsonBeginningIndex; + var insideJsonSection = document[jsonBeginningIndex..JsonEndingIndex]; + + if (insideJsonSection.IndexOf(docs_param) < 0) + { + searchedIndex = JsonEndingIndex + jsonCloser.Length; + continue; + } + + return (jsonBeginningIndex, JsonEndingIndex, insideJsonSection); + } + + return (-1, -1, ""); + } + } +} 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 f74a47fb50..bb84b8a0ea 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 @@ -207,10 +207,36 @@ } } + + +
+ @if (Model.DocumentLanguageIsDifferent) { 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 d675251ac4..37b92ac547 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 @@ -56,22 +56,29 @@ namespace Volo.Docs.Pages.Documents.Project public bool DocumentLanguageIsDifferent { get; set; } + public DocumentParametersDto DocumentPreferences { get; set; } + + public DocumentRenderParameters UserPreferences { get; set; } + private readonly IDocumentAppService _documentAppService; private readonly IDocumentToHtmlConverterFactory _documentToHtmlConverterFactory; private readonly IProjectAppService _projectAppService; + private readonly IDocumentSectionRenderer _documentSectionRenderer; private readonly DocsUiOptions _uiOptions; public IndexModel( IDocumentAppService documentAppService, IDocumentToHtmlConverterFactory documentToHtmlConverterFactory, IProjectAppService projectAppService, - IOptions options) + IOptions options, + IDocumentSectionRenderer documentSectionRenderer) { ObjectMapperContext = typeof(DocsWebModule); _documentAppService = documentAppService; _documentToHtmlConverterFactory = documentToHtmlConverterFactory; _projectAppService = projectAppService; + _documentSectionRenderer = documentSectionRenderer; _uiOptions = options.Value; } @@ -353,7 +360,7 @@ namespace Volo.Docs.Pages.Documents.Project } } - ConvertDocumentContentToHtml(); + await ConvertDocumentContentToHtmlAsync(); } private void SetLanguageSelectListItems() @@ -372,8 +379,13 @@ namespace Volo.Docs.Pages.Documents.Project } } - private void ConvertDocumentContentToHtml() + private async Task ConvertDocumentContentToHtmlAsync() { + await SetDocumentPreferences(); + SetUserPreferences(); + + Document.Content = await _documentSectionRenderer.RenderAsync(Document.Content, UserPreferences); + var converter = _documentToHtmlConverterFactory.Create(Document.Format ?? Project.Format); var content = converter.Convert(Project, Document, GetSpecificVersionOrLatest(), LanguageCode); @@ -392,5 +404,105 @@ namespace Volo.Docs.Pages.Documents.Project Document.Content = content; } + + private void SetUserPreferences() + { + UserPreferences = new DocumentRenderParameters(); + + var cookie = Request.Cookies["AbpIoDocsPreferences"]; + + if (cookie != null) + { + var keyValues = cookie.Split("|"); + + foreach (var keyValue in keyValues) + { + if (keyValue.Split("=").Length < 2) + { + continue; + } + var key = keyValue.Split("=")[0]; + var value = keyValue.Split("=")[1]; + + UserPreferences.Add(key, value); + } + } + + var query = Request.Query; + + foreach (var keyValue in query) + { + if (UserPreferences.ContainsKey(keyValue.Key)) + { + UserPreferences.Remove(keyValue.Key); + } + UserPreferences.Add(keyValue.Key, keyValue.Value); + } + + if (DocumentPreferences?.Parameters == null) + { + return; + } + + foreach (var parameter in DocumentPreferences.Parameters) + { + if (!UserPreferences.ContainsKey(parameter.Name)) + { + UserPreferences.Add(parameter.Name, parameter.Values.FirstOrDefault().Key); + } + } + } + + public async Task SetDocumentPreferences() + { + var projectParameters = await _documentAppService.GetParametersAsync( + new GetParametersDocumentInput + { + ProjectId = Project.Id, + LanguageCode = LanguageCode, + Version = Version + }); + + if (projectParameters?.Parameters == null) + { + return; + } + + var availableparameters = await _documentSectionRenderer.GetAvailableParametersAsync(Document.Content); + + DocumentPreferences = new DocumentParametersDto + { + Parameters = new List() + }; + + if (availableparameters == null || !availableparameters.Any()) + { + return; + } + + foreach (var parameter in projectParameters.Parameters) + { + var availableParameter = availableparameters.GetOrDefault(parameter.Name); + if (availableParameter != null) + { + var newParameter = new DocumentParameterDto + { + Name = parameter.Name, + DisplayName = parameter.DisplayName, + Values = new Dictionary() + }; + + foreach (var value in parameter.Values) + { + if (availableParameter.Contains(value.Key)) + { + newParameter.Values.Add(value.Key, value.Value); + } + } + + DocumentPreferences.Parameters.Add(newParameter); + } + } + } } -} \ 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 8eb2083749..a2ef1803ac 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,7 +2,7 @@ $(function () { var initNavigationFilter = function (navigationContainerId) { - + var $navigation = $("#" + navigationContainerId); @@ -102,12 +102,96 @@ ); }; + var initSections = function () { + var clearQueryString = function () { + var uri = window.location.href.toString(); + + if (uri.indexOf("?") > 0) { + uri = uri.substring(0, uri.indexOf("?")); + } + + window.history.replaceState({}, document.title, uri); + }; + + var setQueryString = function () { + clearQueryString(); + + var uri = window.location.href.toString(); + + var comboboxes = $(".doc-section-combobox"); + + if (comboboxes.length < 1) { + return; + } + + var new_uri = uri + "?"; + + for (var i = 0; i < comboboxes.length; i++) { + var key = $(comboboxes[i]).data('key'); + var value = comboboxes[i].value; + + new_uri += key + "=" + value; + + if (i !== comboboxes.length - 1) { + new_uri += "&"; + } + } + + window.history.replaceState({}, document.title, new_uri); + }; + + var setCookies = function () { + var cookie = abp.utils.getCookieValue("AbpIoDocsPreferences"); + + if (!cookie || cookie == null || cookie === null) { + cookie = ""; + } + var keyValues = cookie.split("|"); + + var comboboxes = $(".doc-section-combobox"); + + for (var i = 0; i < comboboxes.length; i++) { + var key = $(comboboxes[i]).data('key'); + var value = comboboxes[i].value; + + var changed = false; + var keyValueslength = keyValues.length; + for (var k = 0; k < keyValueslength; k++) { + var splitted = keyValues[k].split("="); + + if (splitted.length > 0 && splitted[0] === key) { + keyValues[k] = key + "=" + value; + console.log(keyValues[k]); + changed = true; + } + } + + if (!changed) { + keyValues.push(key + "=" + value); + } + } + + abp.utils.setCookieValue("AbpIoDocsPreferences", keyValues.join('|')); + }; + + $(".doc-section-combobox").change(function () { + setCookies(); + clearQueryString(); + location.reload(); + }); + + setQueryString(); + }; + + initNavigationFilter("sidebar-scroll"); initAnchorTags(".docs-page .docs-body"); initSocialShareLinks(); + initSections(); + }); })(jQuery); diff --git a/modules/docs/src/Volo.Docs.Web/Volo.Docs.Web.csproj b/modules/docs/src/Volo.Docs.Web/Volo.Docs.Web.csproj index cced411e33..27c806762e 100644 --- a/modules/docs/src/Volo.Docs.Web/Volo.Docs.Web.csproj +++ b/modules/docs/src/Volo.Docs.Web/Volo.Docs.Web.csproj @@ -19,6 +19,7 @@ + diff --git a/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocsTestDataBuilder.cs b/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocsTestDataBuilder.cs index 39ffc9691e..15fa70eee7 100644 --- a/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocsTestDataBuilder.cs +++ b/modules/docs/test/Volo.Docs.TestBase/Volo/Docs/DocsTestDataBuilder.cs @@ -27,7 +27,8 @@ namespace Volo.Docs GithubDocumentStore.Type, "md", "index", - "docs-nav.json" + "docs-nav.json", + "docs-params.json" ); project