Load sidebar from documentation.

This commit is contained in:
2024-05-03 14:19:43 +01:00
parent 69a665c20b
commit af1dc77756
8 changed files with 61 additions and 133 deletions

View File

@@ -12,6 +12,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Markdig" Version="0.37.0"/>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.4"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.4"/>

View File

@@ -1,6 +1,8 @@
@implements IDisposable
@inject NavigationManager NavigationManager
@inject NavigationManager NavigationManager
@inject IWebHostEnvironment HostEnvironment
@inject IConfiguration Configuration
<div class="navbar navbar-dark ps-3 top-row">
<div class="container-fluid">
@@ -18,17 +20,7 @@
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="counter">
<span aria-hidden="true" class="bi bi-plus-square-fill-nav-menu"></span> Counter
</NavLink>
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="weather">
<span aria-hidden="true" class="bi bi-list-nested-nav-menu"></span> Weather
</NavLink>
</div>
@((MarkupString)_sidebarMarkup)
<div class="nav-item px-3">
<NavLink class="nav-link" href="auth">
@@ -46,7 +38,7 @@
<div class="nav-item px-3">
<form action="Account/Logout" method="post">
<AntiforgeryToken/>
<input name="ReturnUrl" type="hidden" value="@currentUrl"/>
<input name="ReturnUrl" type="hidden" value="@_currentUrl"/>
<button class="nav-link" type="submit">
<span aria-hidden="true" class="bi bi-arrow-bar-left-nav-menu"></span> Logout
</button>
@@ -67,26 +59,4 @@
</NotAuthorized>
</AuthorizeView>
</nav>
</div>
@code {
private string? currentUrl;
protected override void OnInitialized()
{
currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
NavigationManager.LocationChanged += OnLocationChanged;
}
private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
{
currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
StateHasChanged();
}
public void Dispose()
{
NavigationManager.LocationChanged -= OnLocationChanged;
}
}
</div>

View File

@@ -0,0 +1,43 @@
using Markdig;
using Microsoft.AspNetCore.Components.Routing;
namespace Aaru.Server.New.Components.Layout;
public partial class NavMenu
{
string? _currentUrl;
string _sidebarMarkup = "";
public void Dispose()
{
NavigationManager.LocationChanged -= OnLocationChanged;
}
protected override void OnInitialized()
{
_currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
NavigationManager.LocationChanged += OnLocationChanged;
string? docFolder = Configuration.GetSection("DocumentationFolders").GetValue<string>("Stable");
if(docFolder is null) return;
string docPath = Path.Combine(HostEnvironment.ContentRootPath, docFolder);
if(!Directory.Exists(docPath)) return;
string sidebarPath = Path.Combine(docPath, "_sidebar.md");
if(!File.Exists(sidebarPath)) return;
string sidebarContents = File.ReadAllText(sidebarPath);
_sidebarMarkup = Markdown.ToHtml(sidebarContents);
}
void OnLocationChanged(object? sender, LocationChangedEventArgs e)
{
_currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
StateHasChanged();
}
}

View File

@@ -1,20 +0,0 @@
@page "/counter"
@rendermode InteractiveServer
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount;
private void IncrementCount()
{
currentCount++;
}
}

View File

@@ -1,74 +0,0 @@
@page "/weather"
@attribute [StreamRendering]
<PageTitle>Weather</PageTitle>
<h1>Weather</h1>
<p>This component demonstrates showing data.</p>
@if(forecasts == null)
{
<p>
<em>Loading...</em>
</p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach(WeatherForecast forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
// Simulate asynchronous loading to demonstrate streaming rendering
await Task.Delay(500);
var startDate = DateOnly.FromDateTime(DateTime.Now);
string[] summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
forecasts = Enumerable.Range(1, 5)
.Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
})
.ToArray();
}
private class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}

View File

@@ -1,12 +1,15 @@
{
"ConnectionStrings": {
"ConnectionStrings": {
"DefaultConnection": "DataSource=Data\\app.db;Cache=Shared"
},
"Logging": {
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"DocumentationFolders": {
"Stable": "Aaru.Documentation"
}
}