Removed file extension from URL. And refactored.

pull/457/head
Alper Ebicoglu 6 years ago
parent e5bb814b62
commit 273caa2c25

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Docs.Projects;
namespace Volo.Docs.Documents
{
@ -12,6 +13,10 @@ namespace Volo.Docs.Documents
Task<NavigationWithDetailsDto> GetNavigationDocumentAsync(string projectShortName, string version,
bool normalize);
Task<List<string>> GetVersions(string projectShortName, string documentName);
Task<List<string>> GetVersions(string projectShortName, string defaultDocumentName,
Dictionary<string, object> projectExtraProperties,
string documentStoreType, string documentName);
Task<DocumentWithDetailsDto> GetDocument(ProjectDto project, string documentName, string version, bool normalize);
}
}

@ -7,5 +7,7 @@ namespace Volo.Docs.Projects
public interface IProjectAppService : IApplicationService
{
Task<ListResultDto<ProjectDto>> GetListAsync();
Task<ProjectDto> FindByShortNameAsync(string shortName);
}
}

@ -19,5 +19,9 @@ namespace Volo.Docs.Projects
public Dictionary<string, object> ExtraProperties { get; protected set; }
public string MainWebsiteUrl { get; set; }
public virtual string DocumentStoreType { get; protected set; }
public virtual string Format { get; protected set; }
}
}

@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Entities;
using Volo.Docs.Projects;
namespace Volo.Docs.Documents
@ -30,7 +28,7 @@ namespace Volo.Docs.Documents
{
var project = await _projectRepository.FindByShortNameAsync(projectShortName);
return await GetDocument(project, documentName, version, normalize);
return await GetDocument(ObjectMapper.Map<Project, ProjectDto>(project), documentName, version, normalize);
}
public async Task<NavigationWithDetailsDto> GetNavigationDocumentAsync(string projectShortName, string version, bool normalize)
@ -38,14 +36,15 @@ namespace Volo.Docs.Documents
var project = await _projectRepository.FindByShortNameAsync(projectShortName);
return ObjectMapper.Map<DocumentWithDetailsDto, NavigationWithDetailsDto>(
await GetDocument(project, project.NavigationDocumentName, version, normalize));
await GetDocument(ObjectMapper.Map<Project, ProjectDto>(project), project.NavigationDocumentName,
version, normalize));
}
private async Task<DocumentWithDetailsDto> GetDocument(Project project, string documentName, string version, bool normalize)
public async Task<DocumentWithDetailsDto> GetDocument(ProjectDto project, string documentName, string version, bool normalize)
{
if (project == null)
{
throw new EntityNotFoundException("Project Not Found!");
throw new ArgumentNullException(nameof(project));
}
if (string.IsNullOrWhiteSpace(documentName))
@ -53,37 +52,32 @@ namespace Volo.Docs.Documents
documentName = project.DefaultDocumentName;
}
IDocumentStore documentStore = _documentStoreFactory.Create(project);
Document document = await documentStore.FindDocumentByNameAsync(project, documentName, version);
IDocumentStore documentStore = _documentStoreFactory.Create(project.DocumentStoreType);
Document document = await documentStore.FindDocumentByNameAsync(project.ExtraProperties, project.Format, documentName, version);
var dto = ObjectMapper.Map<Document, DocumentWithDetailsDto>(document);
dto.Project = ObjectMapper.Map<Project, ProjectDto>(project);
dto.Project = project;
return dto;
}
public async Task<List<string>> GetVersions(string projectShortName, string documentName)
public async Task<List<string>> GetVersions(string projectShortName, string defaultDocumentName, Dictionary<string, object> projectExtraProperties,
string documentStoreType, string documentName)
{
var project = await _projectRepository.FindByShortNameAsync(projectShortName);
if (project == null)
{
throw new EntityNotFoundException($"Project Not Found!");
}
if (string.IsNullOrWhiteSpace(documentName))
{
documentName = project.DefaultDocumentName;
documentName = defaultDocumentName;
}
var documentStore = _documentStoreFactory.Create(project);
var documentStore = _documentStoreFactory.Create(documentStoreType);
var versions = await GetVersionsFromCache(projectShortName);
if (versions == null)
{
versions = await documentStore.GetVersions(project, documentName);
versions = await documentStore.GetVersions(projectExtraProperties, documentName);
await SetVersionsToCache(projectShortName, versions);
}
@ -100,7 +94,5 @@ namespace Volo.Docs.Documents
var options = new DistributedCacheEntryOptions() { SlidingExpiration = TimeSpan.FromDays(1) };
await _distributedCache.SetAsync(projectShortName, versions, options);
}
}
}

@ -1,7 +1,6 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Docs.Projects;
namespace Volo.Docs.Documents
{
@ -14,14 +13,14 @@ namespace Volo.Docs.Documents
_serviceProvider = serviceProvider;
}
public IDocumentStore Create(Project project)
public IDocumentStore Create(string documentStoreType)
{
switch (project.DocumentStoreType)
switch (documentStoreType)
{
case GithubDocumentStore.Type:
return _serviceProvider.GetRequiredService<GithubDocumentStore>();
default:
throw new ApplicationException($"Undefined document store: {project.DocumentStoreType}");
throw new ApplicationException($"Undefined document store: {documentStoreType}");
}
}
}

@ -10,7 +10,6 @@ using Microsoft.Extensions.Logging;
using Octokit;
using Volo.Abp.Domain.Services;
using ProductHeaderValue = Octokit.ProductHeaderValue;
using Project = Volo.Docs.Projects.Project;
namespace Volo.Docs.Documents
{
@ -20,11 +19,11 @@ namespace Volo.Docs.Documents
private const bool IsOffline = true; //use it when you don't want to get from GitHub (eg: I have no internet)
public async Task<Document> FindDocumentByNameAsync(Project project, string documentName, string version)
public async Task<Document> FindDocumentByNameAsync(Dictionary<string, object> projectExtraProperties, string projectFormat, string documentName, string version)
{
var rootUrl = project.ExtraProperties["GithubRootUrl"].ToString().Replace("_version_/", version + "/")
.Replace("www.", "");
var token = project.ExtraProperties["GithubAccessToken"]?.ToString();
var rootUrl = projectExtraProperties["GithubRootUrl"].ToString().Replace("_version_/", version + "/").Replace("www.", "");
var token = projectExtraProperties["GithubAccessToken"]?.ToString();
var rawRootUrl = rootUrl.Replace("github.com", token + "raw.githubusercontent.com").Replace("/tree/", "/");
var rawUrl = rawRootUrl + documentName;
@ -48,7 +47,7 @@ namespace Volo.Docs.Documents
EditLink = editLink,
RootUrl = rootUrl,
RawRootUrl = rawRootUrl,
Format = project.Format,
Format = projectFormat,
LocalDirectory = localDirectory,
FileName = fileName,
Version = version
@ -99,12 +98,12 @@ namespace Volo.Docs.Documents
}
}
public async Task<List<string>> GetVersions(Project project, string documentName)
public async Task<List<string>> GetVersions(Dictionary<string, object> projectExtraProperties, string documentName)
{
try
{
var gitHubClient = new GitHubClient(new ProductHeaderValue("AbpWebSite"));
var url = project.ExtraProperties["GithubRootUrl"].ToString();
var url = projectExtraProperties["GithubRootUrl"].ToString();
var releases = await gitHubClient.Repository.Release.GetAll(GetGithubOrganizationNameFromUrl(url),
GetGithubRepositoryNameFromUrl(url));

@ -1,14 +1,14 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;
using Volo.Docs.Projects;
namespace Volo.Docs.Documents
{
public interface IDocumentStore : IDomainService
{
Task<Document> FindDocumentByNameAsync(Project project, string documentName, string version);
Task<Document> FindDocumentByNameAsync(Dictionary<string, object> projectExtraProperties, string projectFormat,
string documentName, string version);
Task<List<string>> GetVersions(Project project, string documentName);
Task<List<string>> GetVersions(Dictionary<string, object> projectExtraProperties, string documentName);
}
}

@ -4,6 +4,6 @@ namespace Volo.Docs.Documents
{
public interface IDocumentStoreFactory
{
IDocumentStore Create(Project project);
IDocumentStore Create(string documentStoreType);
}
}

@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
namespace Volo.Docs.Projects
{
@ -22,5 +23,16 @@ namespace Volo.Docs.Projects
ObjectMapper.Map<List<Project>, List<ProjectDto>>(projects)
);
}
public async Task<ProjectDto> FindByShortNameAsync(string shortName)
{
var project = await _projectRepository.FindByShortNameAsync(shortName);
if (project == null)
{
throw new EntityNotFoundException($"Project with the name {shortName} not found!");
}
return ObjectMapper.Map<Project, ProjectDto>(project);
}
}
}

@ -35,6 +35,9 @@ namespace Volo.Docs.Areas.Documents.Helpers.TagHelpers
[HtmlAttributeName("selected-document-name")]
public string SelectedDocumentName { get; set; }
[HtmlAttributeName("project-format")]
public string ProjectFormat { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var content = new StringBuilder();
@ -80,14 +83,18 @@ namespace Volo.Docs.Areas.Documents.Helpers.TagHelpers
anchorCss += " opened";
}
var normalizedPath = node.Path != null && node.Path.EndsWith("." + ProjectFormat)
? node.Path.Left(node.Path.Length - ProjectFormat.Length - 1)
: node.Path;
return string.Format(LiItemTemplate,
node.Path.IsNullOrEmpty() ? "#" : "/documents/" + ProjectName + "/" + Version + "/" + node.Path,
node.Path.IsNullOrEmpty() ? "#" : "/documents/" + ProjectName + "/" + Version + "/" + normalizedPath,
node.Text.IsNullOrEmpty() ? "?" : node.Text,
content,
node.HasChildItems ? "nav-header" : "last-link",
node.HasChildItems ? "chevron-down" : "long-arrow-right",
anchorCss ,
isNodeSelected? "selected-tree" : "");
anchorCss,
isNodeSelected ? "selected-tree" : "");
}
}

@ -1,4 +1,5 @@
@page
@using Castle.Core.Internal
@using Volo.Docs
@using Volo.Docs.Pages.Documents
@model IndexModel
@ -10,9 +11,14 @@
<abp-style src="/assets/fa/css/font-awesome.min.css" />
</abp-style-bundle>
}
<div class="p-5">
@if (Model.Projects.IsNullOrEmpty())
{
<div class="alert alert-secondary text-center" role="alert">
There are no projects yet!
</div>
}
@if (Model.Projects.Count > 0)
{
<h1>Project(s)</h1>
@ -33,8 +39,5 @@
</ul>
</p>
}
else
{
<p>There is no project defined yet!</p>
}
</div>

@ -92,7 +92,8 @@
<ul root-node="@Model.Navigation.RootNode"
version="@(Model.Version.IsNullOrEmpty() ? "latest" : Model.Version)"
project-name="@Model.ProjectName"
selected-document-name="@Model.DocumentName"
project-format="@Model.ProjectFormat"
selected-document-name="@Model.DocumentNameWithExtension"
id="sidebar-scroll"
class="nav nav-list"></ul>
</div>

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Volo.Docs.Documents;
using Volo.Docs.Formatting;
using Volo.Docs.Projects;
namespace Volo.Docs.Pages.Documents.Project
{
@ -21,6 +22,10 @@ namespace Volo.Docs.Pages.Documents.Project
[BindProperty(SupportsGet = true)]
public string DocumentName { get; set; }
public string ProjectFormat { get; set; }
public string DocumentNameWithExtension { get; private set; }
public DocumentWithDetailsDto Document { get; private set; }
public List<VersionInfo> Versions { get; private set; }
@ -36,24 +41,31 @@ namespace Volo.Docs.Pages.Documents.Project
private readonly IDocumentAppService _documentAppService;
private readonly IDocumentConverterFactory _documentConverterFactory;
private readonly IProjectAppService _projectAppService;
public IndexModel(IDocumentAppService documentAppService, IDocumentConverterFactory documentConverterFactory)
public IndexModel(IDocumentAppService documentAppService, IDocumentConverterFactory documentConverterFactory, IProjectAppService projectAppService)
{
_documentAppService = documentAppService;
_documentConverterFactory = documentConverterFactory;
_projectAppService = projectAppService;
}
public async Task OnGet()
{
Versions = (await _documentAppService.GetVersions(ProjectName, DocumentName))
.Select(v => new VersionInfo(v, v))
.ToList();
var projectDto = await _projectAppService.FindByShortNameAsync(ProjectName);
ProjectFormat = projectDto.Format;
DocumentNameWithExtension = DocumentName + "." + projectDto.Format;
var versions = await _documentAppService.GetVersions(projectDto.ShortName, projectDto.DefaultDocumentName,
projectDto.ExtraProperties, projectDto.DocumentStoreType, DocumentNameWithExtension);
var hasAnyVersion = Versions.Any();
Versions = versions.Select(v => new VersionInfo(v, v)).ToList();
AddDefaultVersionIfNotContains();
var latestVersion = hasAnyVersion ? Versions[1] : Versions[0];
var latestVersion = Versions.Count == 1 ? Versions[0] : Versions[1];
latestVersion.DisplayText = $"{latestVersion.Version} - latest";
latestVersion.Version = latestVersion.Version;
@ -77,8 +89,8 @@ namespace Volo.Docs.Pages.Documents.Project
Version = Versions.Single(x => x.IsSelected).Version;
}
Document = await _documentAppService.GetByNameAsync(ProjectName, DocumentName, Version, true);
var converter = _documentConverterFactory.Create(Document.Format ?? "md");
Document = await _documentAppService.GetByNameAsync(ProjectName, DocumentNameWithExtension, Version, true);
var converter = _documentConverterFactory.Create(Document.Format ?? projectDto.Format);
var content = converter.NormalizeLinks(Document.Content, Document.Project.ShortName, Document.Version, Document.LocalDirectory);
content = converter.Convert(content);

Loading…
Cancel
Save