Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
salihozkara committed Mar 21, 2024
1 parent 7abad2b commit 7928755
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 109 deletions.
16 changes: 10 additions & 6 deletions modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -49,11 +48,16 @@ private async Task<IActionResult> RedirectToProjectAsync(ProjectDto project, str
//Eg: "/en/abp/latest"
public string GetUrlForProject(ProjectDto project, string language = "en", string version = null)
{
return "." +
_urlUiOptions.RoutePrefix.EnsureStartsWith('/').EnsureEndsWith('/') +
language.EnsureEndsWith('/') +
project.ShortName.EnsureEndsWith('/') +
(version ?? DocsAppConsts.Latest);
var routeValues = new Dictionary<string, object> {
{ nameof(Volo.Docs.Pages.Documents.Project.IndexModel.LanguageCode), language },
{ nameof(Volo.Docs.Pages.Documents.Project.IndexModel.Version), version ?? DocsAppConsts.Latest },
};

if (!_urlUiOptions.SingleProjectMode.Enable)
{
routeValues.Add(nameof(Volo.Docs.Pages.Documents.Project.IndexModel.ProjectName), project.ShortName);
}
return Url.Page("/Documents/Project/Index", routeValues);
}
}
}
2 changes: 2 additions & 0 deletions modules/docs/app/VoloDocs.Web/VoloDocsWebModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public override void ConfigureServices(ServiceConfigurationContext context)
Configure<DocsUiOptions>(options =>
{
options.RoutePrefix = null;
options.SingleProjectMode.Enable = true;
options.SingleProjectMode.ProjectName = "abp";
});

Configure<DocsElasticSearchOptions>(options =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using Volo.Docs.Documents;
using Volo.Docs.Localization;
using Volo.Docs.Pages.Documents.Project;
using Volo.Docs.Utils;

namespace Volo.Docs.Areas.Documents.TagHelpers
Expand All @@ -18,6 +22,8 @@ public class TreeTagHelper : TagHelper
private readonly DocsUiOptions _uiOptions;

private readonly IStringLocalizer<DocsResource> _localizer;

private readonly LinkGenerator _linkGenerator;

private const string LiItemTemplateWithLink = @"<li class='{0}'><span class='plus-icon'><i class='fa fa-{1}'></i></span>{2}{3}</li>";

Expand Down Expand Up @@ -45,10 +51,11 @@ public class TreeTagHelper : TagHelper
[HtmlAttributeName("language")]
public string LanguageCode { get; set; }

public TreeTagHelper(IOptions<DocsUiOptions> urlOptions, IStringLocalizer<DocsResource> localizer)
public TreeTagHelper(IOptions<DocsUiOptions> urlOptions, IStringLocalizer<DocsResource> localizer, LinkGenerator linkGenerator)
{
_localizer = localizer;
_uiOptions = urlOptions.Value;
_linkGenerator = linkGenerator;
}

public override void Process(TagHelperContext context, TagHelperOutput output)
Expand Down Expand Up @@ -172,11 +179,19 @@ private string NormalizePath(string path)
{
return "javascript:;";
}

var prefix = _uiOptions.RoutePrefix;

var sb = new StringBuilder();
return sb.Append(prefix).Append(LanguageCode).Append("/").Append(ProjectName).Append("/").Append(Version).Append("/").Append(pathWithoutFileExtension).ToString();
var routeValues = new Dictionary<string, object> {
{ nameof(IndexModel.LanguageCode), LanguageCode },
{ nameof(IndexModel.Version), Version },
{ nameof(IndexModel.DocumentName), pathWithoutFileExtension }
};

if (!_uiOptions.SingleProjectMode.Enable)
{
routeValues.Add(nameof(IndexModel.ProjectName), ProjectName);
}

return _linkGenerator.GetPathByPage("/Documents/Project/Index", values: routeValues);
}

private string RemoveFileExtensionFromPath(string path)
Expand Down
13 changes: 13 additions & 0 deletions modules/docs/src/Volo.Docs.Web/DocsUiOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public string RoutePrefix
/// Default value: True;
/// </summary>
public bool SectionRendering = true;

public SingleProjectModeOptions SingleProjectMode { get; } = new ();

private string GetFormattedRoutePrefix()
{
Expand All @@ -43,4 +45,15 @@ private string GetFormattedRoutePrefix()
return _routePrefix.EnsureEndsWith('/').EnsureStartsWith('/');
}
}

public class SingleProjectModeOptions
{
/// <summary>
/// Determines whether to enable single project mode by removing the project name from the routing.
/// When enabled, only a single project is allowed within the module.
/// </summary>
public bool Enable { get; set; }

public string ProjectName { get; set; }
}
}
21 changes: 17 additions & 4 deletions modules/docs/src/Volo.Docs.Web/DocsWebModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,23 @@ public override void ConfigureServices(ServiceConfigurationContext context)
var routePrefix = docsOptions.RoutePrefix;
options.Conventions.AddPageRoute("/Documents/Project/Index", routePrefix + "{projectName}");
options.Conventions.AddPageRoute("/Documents/Project/Index", routePrefix + "{languageCode}/{projectName}");
options.Conventions.AddPageRoute("/Documents/Project/Index", routePrefix + "{languageCode}/{projectName}/{version}/{*documentName}");
options.Conventions.AddPageRoute("/Documents/Search", routePrefix + "search/{languageCode}/{projectName}/{version}");
if (docsOptions.SingleProjectMode.Enable)
{
if (routePrefix != "/")
{
options.Conventions.AddPageRoute("/Documents/Project/Index", routePrefix);
}
options.Conventions.AddPageRoute("/Documents/Project/Index", routePrefix + "{languageCode}");
options.Conventions.AddPageRoute("/Documents/Project/Index", routePrefix + "{languageCode}/{version}/{*documentName}");
options.Conventions.AddPageRoute("/Documents/Search", routePrefix + "search/{languageCode}/{version}");
}
else
{
options.Conventions.AddPageRoute("/Documents/Project/Index", routePrefix + "{projectName}");
options.Conventions.AddPageRoute("/Documents/Project/Index", routePrefix + "{languageCode}/{projectName}");
options.Conventions.AddPageRoute("/Documents/Project/Index", routePrefix + "{languageCode}/{projectName}/{version}/{*documentName}");
options.Conventions.AddPageRoute("/Documents/Search", routePrefix + "search/{languageCode}/{projectName}/{version}");
}
});

context.Services.AddAutoMapperObjectMapper<DocsWebModule>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection;
using Volo.Docs.Documents;
using Volo.Docs.HtmlConverting;
using Volo.Docs.Pages.Documents.Project;
using Volo.Docs.Projects;
using Volo.Docs.Utils;

Expand All @@ -16,15 +18,17 @@ public class MarkdownDocumentToHtmlConverter : IDocumentToHtmlConverter, ITransi

private readonly IMarkdownConverter _markdownConverter;
private readonly DocsUiOptions _uiOptions;
private readonly LinkGenerator _linkGenerator;

public MarkdownDocumentToHtmlConverter(IMarkdownConverter markdownConverter,
IOptions<DocsUiOptions> urlOptions)
IOptions<DocsUiOptions> urlOptions, LinkGenerator linkGenerator)
{
_markdownConverter = markdownConverter;
_linkGenerator = linkGenerator;
_uiOptions = urlOptions.Value;
}

private const string MdLinkFormat = "[{0}]({1}{2}/{3}/{4}{5}/{6})";
private const string MdLinkFormat = "[{0}]({1})";
private const string MarkdownLinkRegExp = @"\[(.*?)\]\(((.*?)(\?(.*?))*?)\)";
private const string AnchorLinkRegExp = @"<a[^>]+href=\""(.*?)\""[^>]*>(.*)?</a>";

Expand Down Expand Up @@ -117,12 +121,7 @@ public class MarkdownDocumentToHtmlConverter : IDocumentToHtmlConverter, ITransi
return string.Format(
MdLinkFormat,
displayText,
_uiOptions.RoutePrefix,
languageCode,
projectShortName,
version,
documentLocalDirectoryNormalized,
documentName
GenerateUrl(languageCode, $"{version}{documentLocalDirectoryNormalized}", documentName, projectShortName)
);
});
}
Expand All @@ -149,12 +148,7 @@ public class MarkdownDocumentToHtmlConverter : IDocumentToHtmlConverter, ITransi
return string.Format(
MdLinkFormat,
displayText,
_uiOptions.RoutePrefix,
languageCode,
projectShortName,
version,
documentLocalDirectoryNormalized,
documentName
GenerateUrl(languageCode, $"{version}{documentLocalDirectoryNormalized}", documentName, projectShortName)
);
});
}
Expand All @@ -178,5 +172,21 @@ private static string RemoveFileExtension(string documentName)

return documentName.Left(documentName.Length - Type.Length - 1);
}

private string GenerateUrl(string languageCode, string version, string documentName, string projectShortName)
{
var routeValues = new Dictionary<string, object> {
{ nameof(IndexModel.LanguageCode), languageCode },
{ nameof(IndexModel.Version), version },
{ nameof(IndexModel.DocumentName), documentName }
};

if (!_uiOptions.SingleProjectMode.Enable)
{
routeValues.Add(nameof(IndexModel.ProjectName), projectShortName);
}

return _linkGenerator.GetPathByPage("/Documents/Project/Index", values: routeValues);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ public class IndexModel : PageModel

public virtual async Task<IActionResult> OnGetAsync()
{
DocumentsUrlPrefix = _uiOptions.RoutePrefix;
if (_uiOptions.SingleProjectMode.Enable)
{
return RedirectToPage("/Documents/Project/Index");
}

var listResult = await _projectAppService.GetListAsync();

if (listResult.Items.Count == 1)
{
return Redirect(DocumentsUrlPrefix + listResult.Items[0].ShortName);
return RedirectToPage("/Documents/Project/Index", new { shortName = listResult.Items[0].ShortName });
}

Projects = listResult.Items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ else
{
model = new ErrorPageModel
{
RedirectUrl = Model.DocumentsUrlPrefix,
RedirectUrl = Url.Page("/Projects/Index"),
ErrorCode = "404",
ErrorMessage = L.GetString("ProjectNotFound")
}
Expand All @@ -493,8 +493,7 @@ else
{
model = new ErrorPageModel
{
RedirectUrl = Model.DocumentsUrlPrefix + Model.LanguageCode + "/" + Model.ProjectName + "/"
+ (Model.LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Model.Version),
RedirectUrl = Model.BuildDocumentUrl(Model.ProjectName, Model.LanguageCode, Model.LatestVersionInfo.IsSelected ? DocsAppConsts.Latest : Model.Version, null),
ErrorCode = "404",
ErrorMessage = L.GetString("DocumentNotFound"),
AutoRedirect = !Model.DocumentName.IsNullOrWhiteSpace()
Expand Down

0 comments on commit 7928755

Please sign in to comment.