Skip to content

Commit ec9702c

Browse files
committed
Basic wiki, needs automation
1 parent 8e6b7f9 commit ec9702c

7 files changed

Lines changed: 189 additions & 0 deletions

File tree

Web/Pages/WikiPage.razor

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
@page "/wiki/{project}/{route?}"
2+
@using Markdig
3+
@using Markdig.SyntaxHighlighting
4+
@using Web.Services
5+
@inject HttpClient Http
6+
@inject ProjectsService Projects
7+
8+
<PageTitle>Wiki</PageTitle>
9+
10+
@if (IsErrored)
11+
{
12+
<p>There was an error loading this page. Please reload the page to try again, or <a href="/wiki">go back to the wiki root</a>.</p>
13+
@if (!string.IsNullOrEmpty(CustomError))
14+
{
15+
<p><b>Error:</b> @CustomError</p>
16+
}
17+
}
18+
else
19+
{
20+
@if (IsLoading)
21+
{
22+
<p>Loading page, please wait...</p>
23+
}
24+
else
25+
{
26+
@if (mdSource != null)
27+
{
28+
@((MarkupString) mdSource)
29+
}
30+
else
31+
{
32+
<p>Route not found; please check the URL and try again, or <a href="/wiki">go back to the wiki root</a>.</p>
33+
}
34+
}
35+
}
36+
37+
@code {
38+
[Parameter]
39+
public string? Project { get; set; }
40+
41+
[Parameter]
42+
public string? Route { get; set; }
43+
44+
public string? mdSource;
45+
46+
public bool IsErrored = false;
47+
public bool IsLoading = true;
48+
public string? CustomError;
49+
50+
protected override async Task OnParametersSetAsync()
51+
{
52+
if (string.IsNullOrWhiteSpace(Route))
53+
Route = "home";
54+
55+
if(string.IsNullOrEmpty(Project))
56+
{
57+
IsErrored = true;
58+
IsLoading = false;
59+
CustomError = "No project found.";
60+
return;
61+
}
62+
63+
var projList = await Projects.GetProjectSlugs();
64+
if(!projList.Contains(Project, StringComparer.InvariantCultureIgnoreCase))
65+
{
66+
IsErrored = true;
67+
IsLoading = false;
68+
CustomError = $"No project with the slug '{Project}' was found. Check the URL and try again.";
69+
return;
70+
}
71+
72+
if (!string.IsNullOrEmpty(Project) && !string.IsNullOrEmpty(Route))
73+
{
74+
var source = $"wiki-pages/{Project}/{Route}.md";
75+
var sourceStr = await Http.GetAsync(source);
76+
if (sourceStr.IsSuccessStatusCode)
77+
{
78+
var pl = new Markdig.MarkdownPipelineBuilder()
79+
.UseAdvancedExtensions()
80+
.UseSyntaxHighlighting()
81+
.Build();
82+
83+
var mdSourceStr = await sourceStr.Content.ReadAsStringAsync();
84+
mdSource = Markdown.ToHtml(mdSourceStr, pl);
85+
}
86+
87+
IsLoading = false;
88+
}
89+
else
90+
{
91+
IsErrored = true;
92+
IsLoading = false;
93+
}
94+
}
95+
}

Web/Pages/WikiRoot.razor

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@page "/wiki";
2+
@using Web.Services
3+
@inject ProjectsService Projects
4+
5+
<h1>Wiki Home</h1>
6+
7+
@if (IsLoaded)
8+
{
9+
@if (ProjectList.Count > 0)
10+
{
11+
<p>Please choose a mod to view its wiki pages:</p>
12+
<ul>
13+
@foreach (var proj in ProjectList)
14+
{
15+
<li><a href="/wiki/@proj.Slug">@proj.Name</a></li>
16+
}
17+
</ul>
18+
} else
19+
{
20+
<p>There are no projects to list. Please try again later.</p>
21+
}
22+
} else
23+
{
24+
<p>Loading projects, please wait...</p>
25+
}
26+
27+
@code {
28+
public bool IsLoaded = false;
29+
public ICollection<ProjectsService.Project> ProjectList = Array.Empty<ProjectsService.Project>();
30+
31+
protected override async Task OnInitializedAsync()
32+
{
33+
ProjectList = await Projects.GetProjects();
34+
IsLoaded = true;
35+
}
36+
}

Web/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using Microsoft.AspNetCore.Components.Web;
22
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
33
using Web;
4+
using Web.Services;
45

56
var builder = WebAssemblyHostBuilder.CreateDefault(args);
67
builder.RootComponents.Add<App>("#app");
78
builder.RootComponents.Add<HeadOutlet>("head::after");
89

910
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
1011

12+
builder.Services.AddScoped<ProjectsService>();
13+
1114
await builder.Build().RunAsync();

Web/Services/ProjectsService.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Text.Json.Serialization;
2+
using System.Net.Http.Json;
3+
using System.Collections.Immutable;
4+
5+
namespace Web.Services
6+
{
7+
public class ProjectsService
8+
{
9+
public HttpClient Client { get; }
10+
public ProjectsService(HttpClient client)
11+
{
12+
Client = client;
13+
}
14+
15+
public async Task<IReadOnlyCollection<string>> GetProjectSlugs()
16+
{
17+
var projects = await GetProjects();
18+
return projects.Select(x => x.Slug).ToImmutableArray();
19+
}
20+
21+
public async Task<ICollection<Project>> GetProjects()
22+
{
23+
var root = await Client.GetAsync("projects.json");
24+
if(root.IsSuccessStatusCode && root.Content is not null)
25+
return await root.Content.ReadFromJsonAsync<ICollection<Project>>();
26+
27+
return Array.Empty<Project>();
28+
}
29+
30+
public struct Project
31+
{
32+
public string Name { get; set; }
33+
public string Slug { get; set; }
34+
}
35+
}
36+
}

Web/Shared/NavMenu.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
<i class="fa-solid fa-compass"></i>
1717
<span>CM4 Coords Tool</span>
1818
</NavLink>
19+
20+
<NavLink class="nav-link" href="wiki" Match="NavLinkMatch.Prefix">
21+
<i class="fa-solid fa-book"></i>
22+
<span>Wiki</span>
23+
</NavLink>
1924
</nav>
2025

2126
<div class="d-flex" id="socials">

Web/Web.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
<ItemGroup>
3030
<PackageReference Include="BuildWebCompiler2022" Version="1.14.6" />
31+
<PackageReference Include="Markdig" Version="0.30.2" />
32+
<PackageReference Include="Markdig.SyntaxHighlighting" Version="1.1.7" />
3133
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.3" />
3234
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.3" PrivateAssets="all" />
3335
</ItemGroup>
@@ -44,6 +46,7 @@
4446

4547
<ItemGroup>
4648
<Folder Include="wwwroot\boot\" />
49+
<Folder Include="wwwroot\wiki-pages\" />
4750
</ItemGroup>
4851

4952
</Project>

Web/wwwroot/projects.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"name": "Compact Machines",
4+
"slug": "machines"
5+
},
6+
7+
{
8+
"name": "Compact Crafting",
9+
"slug": "crafting"
10+
}
11+
]

0 commit comments

Comments
 (0)