|
| 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 | +} |
0 commit comments