From d88e8126e6b032ad9b805a6dc29fcac2a90b3487 Mon Sep 17 00:00:00 2001 From: Salih Date: Mon, 12 Feb 2024 14:52:18 +0300 Subject: [PATCH] Refactor #17775 --- .../ScribanDocumentSectionRenderer.cs | 54 +++++++------------ 1 file changed, 20 insertions(+), 34 deletions(-) diff --git a/modules/docs/src/Volo.Docs.Web/HtmlConverting/ScribanDocumentSectionRenderer.cs b/modules/docs/src/Volo.Docs.Web/HtmlConverting/ScribanDocumentSectionRenderer.cs index b28e42d611..b95966ea28 100644 --- a/modules/docs/src/Volo.Docs.Web/HtmlConverting/ScribanDocumentSectionRenderer.cs +++ b/modules/docs/src/Volo.Docs.Web/HtmlConverting/ScribanDocumentSectionRenderer.cs @@ -45,35 +45,45 @@ namespace Volo.Docs.HtmlConverting return RemoveOptionsJson(result, DocsParam, DocsNav); } - public async Task>> GetAvailableParametersAsync(string document) + public Task>> GetAvailableParametersAsync(string document) + { + return GetSectionAsync>>(document, DocsParam); + } + + public Task GetDocumentNavigationsAsync(string documentContent) + { + return GetSectionAsync(documentContent, DocsNav); + } + + protected virtual async Task GetSectionAsync(string document, string sectionName) where T : new() { try { - if (!document.Contains(JsonOpener) || !document.Contains(DocsParam)) + if (!document.Contains(JsonOpener) || !document.Contains(sectionName)) { - return new Dictionary>(); + return new T(); } - var (jsonBeginningIndex, jsonEndingIndex, insideJsonSection) = GetJsonBeginEndIndexesAndPureJson(document, DocsParam); + var (jsonBeginningIndex, jsonEndingIndex, insideJsonSection) = GetJsonBeginEndIndexesAndPureJson(document, sectionName); if (jsonBeginningIndex < 0 || jsonEndingIndex <= 0 || string.IsNullOrWhiteSpace(insideJsonSection)) { - return new Dictionary>(); + return new T(); } - var pureJson = insideJsonSection.Replace(DocsParam, "").Trim(); + var pureJson = insideJsonSection.Replace(sectionName, "").Trim(); - if (!DocsJsonSerializerHelper.TryDeserialize>>(pureJson, out var availableParameters)) + if (!DocsJsonSerializerHelper.TryDeserialize(pureJson, out var section)) { - throw new UserFriendlyException("ERROR-20200327: Cannot validate JSON content for `AvailableParameters`!"); + throw new UserFriendlyException($"ERROR-20200327: Cannot validate JSON content for `{sectionName}`!"); } - return await Task.FromResult(availableParameters); + return await Task.FromResult(section); } catch (Exception) { Logger.LogWarning("Unable to parse parameters of document."); - return new Dictionary>(); + return new T(); } } @@ -176,30 +186,6 @@ namespace Volo.Docs.HtmlConverting return await Task.FromResult(templates); } - public Task GetDocumentNavigationsAsync(string documentContent) - { - if (!documentContent.Contains(JsonOpener) || !documentContent.Contains(DocsNav)) - { - return Task.FromResult(new DocumentNavigationsDto()); - } - - var (jsonBeginningIndex, jsonEndingIndex, insideJsonSection) = GetJsonBeginEndIndexesAndPureJson(documentContent, DocsNav); - - if (jsonBeginningIndex < 0 || jsonEndingIndex <= 0 || string.IsNullOrWhiteSpace(insideJsonSection)) - { - return Task.FromResult(new DocumentNavigationsDto()); - } - - var pureJson = insideJsonSection.Replace(DocsNav, "").Trim(); - - if (!DocsJsonSerializerHelper.TryDeserialize(pureJson, out var navigationDocumentNames)) - { - throw new UserFriendlyException("ERROR-20200327: Cannot validate JSON content for `NavigationDocumentNames`!"); - } - - return Task.FromResult(navigationDocumentNames); - } - private static string SetPartialTemplates(string document, IReadOnlyCollection templates) { var newDocument = new StringBuilder();