Adding Syndicate Pages
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
@using System.Text.RegularExpressions
|
||||
@using Microsoft.AspNetCore.Components.Rendering
|
||||
@namespace Chrono.Components
|
||||
|
||||
@if (Card != null)
|
||||
@@ -50,7 +51,9 @@
|
||||
{
|
||||
<div class="detail-field description">
|
||||
<span class="field-label"><i class="bi bi-chat-quote-fill"></i></span>
|
||||
<span class="field-value">@RenderDescription(Card.Description)</span>
|
||||
<div class="markdown-content">
|
||||
@RenderDescription(Card.Description)
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@@ -90,7 +93,7 @@
|
||||
if (target != null)
|
||||
{
|
||||
<button class="inline-card-btn"
|
||||
@onclick="() => Navigate(target)">@targetName</button>
|
||||
@onclick="() => Navigate(targetName)">@targetName</button>
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -111,7 +114,7 @@
|
||||
if (fromCard != null)
|
||||
{
|
||||
<button class="inline-card-btn"
|
||||
@onclick="() => Navigate(fromCard)">@Card.ImmortalizeFrom</button>
|
||||
@onclick="() => Navigate(Card.ImmortalizeFrom)">@Card.ImmortalizeFrom</button>
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -142,7 +145,7 @@
|
||||
@code {
|
||||
[Parameter] public CardData? Card { get; set; }
|
||||
[Parameter] public EventCallback OnClose { get; set; }
|
||||
[Parameter] public EventCallback<CardData> OnNavigate { get; set; }
|
||||
[Parameter] public EventCallback<string> OnNavigate { get; set; }
|
||||
[Parameter] public bool ShowNotes { get; set; }
|
||||
|
||||
[Inject] private HttpClient Http { get; set; } = default!;
|
||||
@@ -203,9 +206,19 @@
|
||||
_ = OnClose.InvokeAsync();
|
||||
}
|
||||
|
||||
private void Navigate(CardData card)
|
||||
private void Navigate(string name)
|
||||
{
|
||||
_ = OnNavigate.InvokeAsync(card);
|
||||
var card = LookupCard(name);
|
||||
if (card != null)
|
||||
{
|
||||
Card = card;
|
||||
_ = LoadNote();
|
||||
StateHasChanged();
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = OnNavigate.InvokeAsync(name);
|
||||
}
|
||||
}
|
||||
|
||||
private static CardData? LookupCard(string cardName)
|
||||
@@ -214,68 +227,205 @@
|
||||
string.Equals(c.Name, cardName, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private RenderFragment RenderDescription(string description)
|
||||
private static DocData? LookupDoc(string title)
|
||||
{
|
||||
return DocDatabase.Docs.FirstOrDefault(d =>
|
||||
string.Equals(d.Title, title, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private RenderFragment RenderDescription(string content)
|
||||
{
|
||||
return builder =>
|
||||
{
|
||||
var cardNames = CardDatabase.Cards
|
||||
.Select(c => c.Name)
|
||||
.Where(n => !string.IsNullOrWhiteSpace(n))
|
||||
.OrderByDescending(n => n.Length)
|
||||
.ToList();
|
||||
if (string.IsNullOrWhiteSpace(content)) return;
|
||||
|
||||
var seq = 0;
|
||||
var remaining = description;
|
||||
var lines = content.Split(['\r', '\n'], StringSplitOptions.None);
|
||||
var inList = false;
|
||||
var tableLines = new List<string>();
|
||||
var inTable = false;
|
||||
|
||||
while (!string.IsNullOrEmpty(remaining))
|
||||
for (var i = 0; i < lines.Length; i++)
|
||||
{
|
||||
var bestIdx = -1;
|
||||
string? bestName = null;
|
||||
var line = lines[i];
|
||||
var trimmedLine = line.Trim();
|
||||
|
||||
foreach (var name in cardNames)
|
||||
// Handle Tables
|
||||
if (trimmedLine.StartsWith("|") && trimmedLine.EndsWith("|"))
|
||||
{
|
||||
var pattern = $@"\b{Regex.Escape(name)}\b";
|
||||
var match = Regex.Match(remaining, pattern, RegexOptions.IgnoreCase);
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
if (bestIdx == -1 || match.Index < bestIdx)
|
||||
{
|
||||
bestIdx = match.Index;
|
||||
bestName = match.Value;
|
||||
}
|
||||
}
|
||||
inTable = true;
|
||||
tableLines.Add(trimmedLine);
|
||||
continue;
|
||||
}
|
||||
else if (inTable)
|
||||
{
|
||||
RenderTable(builder, ref seq, tableLines);
|
||||
tableLines.Clear();
|
||||
inTable = false;
|
||||
}
|
||||
|
||||
if (bestIdx != -1 && bestName != null)
|
||||
// Handle Lists
|
||||
if (trimmedLine.StartsWith("- ") || trimmedLine.StartsWith("* "))
|
||||
{
|
||||
if (bestIdx > 0)
|
||||
builder.AddContent(seq++, remaining[..bestIdx]);
|
||||
if (!inList)
|
||||
{
|
||||
builder.OpenElement(seq++, "ul");
|
||||
inList = true;
|
||||
}
|
||||
var isCardListItem = trimmedLine.Contains("[[") && trimmedLine.Contains("]]");
|
||||
builder.OpenElement(seq++, "li");
|
||||
RenderInlines(builder, ref seq, trimmedLine[2..], true);
|
||||
builder.CloseElement();
|
||||
continue;
|
||||
}
|
||||
else if (inList)
|
||||
{
|
||||
builder.CloseElement(); // Close ul
|
||||
inList = false;
|
||||
}
|
||||
|
||||
var card = LookupCard(bestName);
|
||||
if (card != null)
|
||||
// Handle Headings
|
||||
if (trimmedLine.StartsWith("### "))
|
||||
{
|
||||
builder.OpenElement(seq++, "h4");
|
||||
RenderInlines(builder, ref seq, trimmedLine[4..], false);
|
||||
builder.CloseElement();
|
||||
}
|
||||
else if (trimmedLine.StartsWith("## "))
|
||||
{
|
||||
builder.OpenElement(seq++, "h3");
|
||||
RenderInlines(builder, ref seq, trimmedLine[3..], false);
|
||||
builder.CloseElement();
|
||||
}
|
||||
else if (trimmedLine.StartsWith("# "))
|
||||
{
|
||||
builder.OpenElement(seq++, "h2");
|
||||
RenderInlines(builder, ref seq, trimmedLine[2..], false);
|
||||
builder.CloseElement();
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(trimmedLine))
|
||||
{
|
||||
builder.OpenElement(seq++, "p");
|
||||
RenderInlines(builder, ref seq, line, false);
|
||||
builder.CloseElement();
|
||||
}
|
||||
}
|
||||
|
||||
if (inTable) RenderTable(builder, ref seq, tableLines);
|
||||
if (inList) builder.CloseElement();
|
||||
};
|
||||
}
|
||||
|
||||
private void RenderTable(RenderTreeBuilder builder, ref int seq, List<string> lines)
|
||||
{
|
||||
if (lines.Count < 2) return;
|
||||
|
||||
builder.OpenElement(seq++, "div");
|
||||
builder.AddAttribute(seq++, "class", "table-responsive");
|
||||
builder.OpenElement(seq++, "table");
|
||||
builder.AddAttribute(seq++, "class", "markdown-table");
|
||||
|
||||
var hasHeader = lines.Count > 2 && lines[1].Contains("---");
|
||||
var startIdx = 0;
|
||||
|
||||
if (hasHeader)
|
||||
{
|
||||
builder.OpenElement(seq++, "thead");
|
||||
builder.OpenElement(seq++, "tr");
|
||||
var cells = lines[0].Split('|', StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var cell in cells)
|
||||
{
|
||||
builder.OpenElement(seq++, "th");
|
||||
RenderInlines(builder, ref seq, cell.Trim(), false);
|
||||
builder.CloseElement();
|
||||
}
|
||||
builder.CloseElement();
|
||||
builder.CloseElement();
|
||||
startIdx = 2;
|
||||
}
|
||||
|
||||
builder.OpenElement(seq++, "tbody");
|
||||
for (var i = startIdx; i < lines.Count; i++)
|
||||
{
|
||||
if (i == 1 && hasHeader) continue;
|
||||
builder.OpenElement(seq++, "tr");
|
||||
var cells = lines[i].Split('|', StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var cell in cells)
|
||||
{
|
||||
builder.OpenElement(seq++, "td");
|
||||
RenderInlines(builder, ref seq, cell.Trim(), false);
|
||||
builder.CloseElement();
|
||||
}
|
||||
builder.CloseElement();
|
||||
}
|
||||
builder.CloseElement();
|
||||
builder.CloseElement();
|
||||
builder.CloseElement();
|
||||
}
|
||||
|
||||
private void RenderInlines(RenderTreeBuilder builder, ref int seq, string text, bool renderAsImage)
|
||||
{
|
||||
var remaining = text;
|
||||
|
||||
while (!string.IsNullOrEmpty(remaining))
|
||||
{
|
||||
var boldMatch = Regex.Match(remaining, @"\*\*(.*?)\*\*");
|
||||
var italicMatch = Regex.Match(remaining, @"\*(.*?)\*");
|
||||
var cardMatch = Regex.Match(remaining, @"\[\[(.*?)\]\]");
|
||||
|
||||
var bestIdx = int.MaxValue;
|
||||
Match? bestMatch = null;
|
||||
int matchType = 0; // 1: bold, 2: italic, 3: card
|
||||
|
||||
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 (cardMatch.Success && cardMatch.Index < bestIdx) { bestIdx = cardMatch.Index; bestMatch = cardMatch; matchType = 3; }
|
||||
|
||||
if (bestMatch != null)
|
||||
{
|
||||
if (bestIdx > 0)
|
||||
builder.AddContent(seq++, remaining[..bestIdx]);
|
||||
|
||||
var innerText = bestMatch.Groups[1].Value;
|
||||
|
||||
if (matchType == 1) // Bold
|
||||
{
|
||||
builder.OpenElement(seq++, "strong");
|
||||
builder.AddContent(seq++, innerText);
|
||||
builder.CloseElement();
|
||||
}
|
||||
else if (matchType == 2) // Italic
|
||||
{
|
||||
builder.OpenElement(seq++, "em");
|
||||
builder.AddContent(seq++, innerText);
|
||||
builder.CloseElement();
|
||||
}
|
||||
else if (matchType == 3) // Card Link / Doc Link
|
||||
{
|
||||
var card = LookupCard(innerText);
|
||||
var doc = LookupDoc(innerText);
|
||||
if (card != null || doc != null)
|
||||
{
|
||||
builder.OpenElement(seq++, "button");
|
||||
builder.AddAttribute(seq++, "class", "inline-card-btn");
|
||||
builder.AddAttribute(seq++, "onclick",
|
||||
EventCallback.Factory.Create(this, () => Navigate(card)));
|
||||
builder.AddContent(seq++, bestName);
|
||||
builder.AddAttribute(seq++, "onclick", EventCallback.Factory.Create(this, () => Navigate(innerText)));
|
||||
builder.AddContent(seq++, innerText);
|
||||
builder.CloseElement();
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.AddContent(seq++, bestName);
|
||||
builder.AddContent(seq++, innerText);
|
||||
}
|
||||
}
|
||||
|
||||
remaining = remaining[(bestIdx + bestName.Length)..];
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.AddContent(seq++, remaining);
|
||||
remaining = "";
|
||||
}
|
||||
remaining = remaining[(bestIdx + bestMatch.Length)..];
|
||||
}
|
||||
};
|
||||
else
|
||||
{
|
||||
builder.AddContent(seq++, remaining);
|
||||
remaining = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user