140 lines
4.2 KiB
Plaintext
140 lines
4.2 KiB
Plaintext
@using System.Text.RegularExpressions
|
|
@using Microsoft.AspNetCore.Components.Rendering
|
|
@using Model
|
|
@namespace Shared.Components
|
|
|
|
@if (Doc != null)
|
|
{
|
|
<div class="modal-backdrop" @onclick="HandleBackdropClick"></div>
|
|
<div class="generic-detail">
|
|
<button class="detail-close" @onclick="HandleClose"><i class="bi bi-x-lg"></i></button>
|
|
<div class="detail-layout">
|
|
<div class="detail-info">
|
|
<div class="detail-header">
|
|
@if (!string.IsNullOrEmpty(Doc.Category))
|
|
{
|
|
<span class="category-badge">@Doc.Category</span>
|
|
}
|
|
<h2>@Doc.Title</h2>
|
|
</div>
|
|
|
|
@if (!string.IsNullOrWhiteSpace(Doc.Content))
|
|
{
|
|
<div class="detail-field description">
|
|
<div class="markdown-content">
|
|
@RenderDescription(Doc.Content)
|
|
</div>
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
[Parameter] public DocData? Doc { get; set; }
|
|
[Parameter] public EventCallback OnClose { get; set; }
|
|
|
|
private void HandleClose()
|
|
{
|
|
_ = OnClose.InvokeAsync();
|
|
}
|
|
|
|
private void HandleBackdropClick()
|
|
{
|
|
_ = OnClose.InvokeAsync();
|
|
}
|
|
|
|
private RenderFragment RenderDescription(string content)
|
|
{
|
|
return builder =>
|
|
{
|
|
if (string.IsNullOrWhiteSpace(content)) return;
|
|
|
|
var seq = 0;
|
|
var lines = content.Split(['\r', '\n'], StringSplitOptions.None);
|
|
var inList = false;
|
|
|
|
for (var i = 0; i < lines.Length; i++)
|
|
{
|
|
var line = lines[i];
|
|
var trimmedLine = line.Trim();
|
|
|
|
// Handle Lists
|
|
if (trimmedLine.StartsWith("- ") || trimmedLine.StartsWith("* "))
|
|
{
|
|
if (!inList)
|
|
{
|
|
builder.OpenElement(seq++, "ul");
|
|
inList = true;
|
|
}
|
|
|
|
builder.OpenElement(seq++, "li");
|
|
RenderInlines(builder, ref seq, trimmedLine[2..]);
|
|
builder.CloseElement();
|
|
continue;
|
|
}
|
|
|
|
if (inList)
|
|
{
|
|
builder.CloseElement();
|
|
inList = false;
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(trimmedLine))
|
|
{
|
|
builder.OpenElement(seq++, "p");
|
|
RenderInlines(builder, ref seq, line);
|
|
builder.CloseElement();
|
|
}
|
|
}
|
|
|
|
if (inList) builder.CloseElement();
|
|
};
|
|
}
|
|
|
|
private void RenderInlines(RenderTreeBuilder builder, ref int seq, string text)
|
|
{
|
|
var remaining = text;
|
|
while (!string.IsNullOrEmpty(remaining))
|
|
{
|
|
var boldMatch = Regex.Match(remaining, @"\*\*(.*?)\*\*");
|
|
var italicMatch = Regex.Match(remaining, @"\*(.*?)\*");
|
|
|
|
var bestIdx = int.MaxValue;
|
|
Match? bestMatch = null;
|
|
var matchType = 0; // 1: bold, 2: italic
|
|
|
|
if (boldMatch.Success && boldMatch.Index < bestIdx)
|
|
{
|
|
bestIdx = boldMatch.Index;
|
|
bestMatch = boldMatch;
|
|
matchType = 1;
|
|
}
|
|
|
|
if (italicMatch.Success && italicMatch.Index < bestIdx)
|
|
{
|
|
bestIdx = italicMatch.Index;
|
|
bestMatch = italicMatch;
|
|
matchType = 2;
|
|
}
|
|
|
|
if (bestMatch != null)
|
|
{
|
|
if (bestIdx > 0) builder.AddContent(seq++, remaining[..bestIdx]);
|
|
var innerText = bestMatch.Groups[1].Value;
|
|
builder.OpenElement(seq++, matchType == 1 ? "strong" : "em");
|
|
builder.AddContent(seq++, innerText);
|
|
builder.CloseElement();
|
|
remaining = remaining[(bestIdx + bestMatch.Length)..];
|
|
}
|
|
else
|
|
{
|
|
builder.AddContent(seq++, remaining);
|
|
remaining = "";
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|