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 d1dd5ba6d7..06edbd7d88 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 @@ -27,6 +27,8 @@ namespace Volo.Docs.Documents public string FileName { get; set; } public ProjectDto Project { get; set; } + + public bool SuccessfullyRetrieved { get; set; } } public class NavigationNode diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/Document.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/Document.cs index c8ad716607..d52184f00f 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/Document.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/Document.cs @@ -19,5 +19,7 @@ namespace Volo.Docs.Documents public string LocalDirectory { get; set; } public string FileName { get; set; } + + public bool SuccessfullyRetrieved { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/GithubDocumentStore.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/GithubDocumentStore.cs index f4504ed8e1..b41d643f5d 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/GithubDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/GithubDocumentStore.cs @@ -8,6 +8,7 @@ using System.Net.Sockets; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Octokit; +using Volo.Abp; using Volo.Abp.Domain.Services; using ProductHeaderValue = Octokit.ProductHeaderValue; @@ -17,7 +18,7 @@ namespace Volo.Docs.Documents { public const string Type = "Github"; //TODO: Convert to "github" - private const bool IsOffline = true; //use it when you don't want to get from GitHub (eg: I have no internet) + public const int DocumentNotFoundExceptionCode = 20181001; public async Task FindDocumentByNameAsync(Dictionary projectExtraProperties, string projectFormat, string documentName, string version) { @@ -38,62 +39,37 @@ namespace Volo.Docs.Documents documentName.Length - documentName.LastIndexOf('/') - 1); } - var content = DownloadWebContent(documentName, rawUrl); - - return await Task.FromResult(new Document + var document = new Document { Title = documentName, - Content = content, EditLink = editLink, RootUrl = rootUrl, RawRootUrl = rawRootUrl, Format = projectFormat, LocalDirectory = localDirectory, FileName = fileName, - Version = version - }); + Version = version, + SuccessfullyRetrieved = TryDownloadWebContent(rawUrl, out var content), + Content = content + }; + + return await Task.FromResult(document); } - private string DownloadWebContent(string documentName, string rawUrl) + private bool TryDownloadWebContent(string rawUrl, out string content) { using (var webClient = new WebClient()) { try { - return webClient.DownloadString(rawUrl); - } - catch (WebException ex) - { - Logger.LogError(ex, ex.Message); - - if (ex.Status == WebExceptionStatus.ProtocolError) - { - if (ex.Response != null && ex.Response is HttpWebResponse response) - { - if (response.StatusCode == HttpStatusCode.NotFound) - { - return $"The document {documentName} not found in this version!"; - } - } - } - //todo: remove it when filedocumentstore is implemented - else if (ex.InnerException is HttpRequestException && - ex.InnerException.InnerException != null && - ex.InnerException.InnerException is SocketException exception && - exception.SocketErrorCode == SocketError.HostNotFound) - { - if (IsOffline) - { - return File.ReadAllText(Path.Combine(@"D:\Github\abp\docs\", documentName)); - } - } - - return "An error occured while getting the document " + documentName; + content = webClient.DownloadString(rawUrl); + return true; } catch (Exception ex) { + content = null; Logger.LogError(ex, ex.Message); - return "An error occured while getting the document " + documentName; + return false; } } } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Localization/Domain/en.json b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Localization/Domain/en.json index ac5e7b0873..5540146823 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Localization/Domain/en.json +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Localization/Domain/en.json @@ -8,6 +8,7 @@ "InThisDocument": "In this document", "GoToTop": "Go to top", "Projects": "Project(s)", - "NoProjectWarning": "There are no projects yet!" + "NoProjectWarning": "There are no projects yet!", + "DocumentNotFound": "Oops, the requested document was not found!" } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Localization/Domain/tr.json b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Localization/Domain/tr.json index 634fa9e26d..1dd00600bc 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Localization/Domain/tr.json +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Localization/Domain/tr.json @@ -8,6 +8,7 @@ "InThisDocument": "Bu dökümanda", "GoToTop": "En üste çık", "Projects": "Proje(ler)", - "NoProjectWarning": "Hiç proje yok!" + "NoProjectWarning": "Hiç proje yok!", + "DocumentNotFound": "Aradığınız döküman bulunamadı!" } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs b/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs index 9737d6d2d4..4102461359 100644 --- a/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs +++ b/modules/docs/src/Volo.Docs.Web/Formatting/MarkdownDocumentConverter.cs @@ -16,12 +16,17 @@ namespace Volo.Docs.Formatting public string Convert(string content) { - return CommonMarkConverter.Convert(Encoding.UTF8.GetString(Encoding.Default.GetBytes(content))); + return content == null ? null : CommonMarkConverter.Convert(Encoding.UTF8.GetString(Encoding.Default.GetBytes(content))); } public string NormalizeLinks(string content, string projectShortName, string version, string documentLocalDirectory) { + if (content == null) + { + return null; + } + return Regex.Replace(content, MarkdownLinkRegExp, delegate (Match match) { var displayText = match.Groups[1].Value; @@ -33,6 +38,11 @@ namespace Volo.Docs.Formatting private static string RemoveFileExtensionIfLocalUrl(string documentName) { + if (documentName == null) + { + return null; + } + if (string.IsNullOrWhiteSpace(documentName)) { return documentName; @@ -53,6 +63,11 @@ namespace Volo.Docs.Formatting private static bool IsRemoteUrl(string url) { + if (url == null) + { + return true; + } + try { return Regex.IsMatch(url, @"\A(https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?\z"); diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/HtmlNormalizer.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/HtmlNormalizer.cs index fcf9efe275..f484591c3b 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/HtmlNormalizer.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/HtmlNormalizer.cs @@ -7,6 +7,11 @@ namespace Volo.Docs.Pages.Documents.Project { public static string ReplaceImageSources(string content, string documentRawRootUrl, string localDirectory) { + if (content == null) + { + return null; + } + content = Regex.Replace(content, @"(]*)src=""([^""]*)""([^>]*>)", delegate (Match match) { var newImageSource = documentRawRootUrl.EnsureEndsWith('/') + @@ -22,7 +27,7 @@ namespace Volo.Docs.Pages.Documents.Project public static string ReplaceCodeBlocksLanguage(string content, string currentLanguage, string newLanguage) { - return content.Replace("", ""); + return content?.Replace("", ""); } } 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 6655141126..133a0e3d19 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 @@ -109,41 +109,54 @@ -
+ @if (Model.Document.SuccessfullyRetrieved) + { - +
+ + -
-
-
-
- @Html.Raw(Model.Document.Content) -
+
+
+
+
+ @Html.Raw(Model.Document.Content) +
+
-
-
+
-
+
-
- +
+ - -
+ } + else + { +
+

@L["DocumentNotFound"]

+ Go to home +
+ } +
\ No newline at end of file