mirror of
https://github.com/radzenhq/radzen-blazor.git
synced 2026-07-08 18:16:08 +00:00
743 lines
27 KiB
C#
743 lines
27 KiB
C#
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace RadzenBlazorDemos.Tools;
|
|
|
|
class Program
|
|
{
|
|
// Pages that are not component documentation (marketing, meta, showcases).
|
|
// These produce noisy content (testimonials, repeated CTAs, sample dashboards)
|
|
// that pollutes search results when used with a RAG system.
|
|
static readonly HashSet<string> ExcludedPages = new(StringComparer.OrdinalIgnoreCase)
|
|
{
|
|
"AI",
|
|
"Changelog",
|
|
"Dashboard",
|
|
"DashboardPage",
|
|
"Index",
|
|
"NotFound",
|
|
"Playground",
|
|
"SupportPage",
|
|
"ThemesPage",
|
|
};
|
|
|
|
// Filename prefixes that match non-documentation showcase pages.
|
|
static readonly string[] ExcludedPrefixes = ["Templates", "UIBlocks"];
|
|
|
|
static bool IsExcluded(string filePath)
|
|
{
|
|
var name = Path.GetFileNameWithoutExtension(filePath);
|
|
|
|
if (ExcludedPages.Contains(name))
|
|
return true;
|
|
|
|
foreach (var prefix in ExcludedPrefixes)
|
|
{
|
|
if (name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static int Main(string[] args)
|
|
{
|
|
if (args.Length < 2)
|
|
{
|
|
Console.Error.WriteLine("Usage: GenerateLlmsTxt <outputPath> <pagesPath> [servicesPath] [modelsPath]");
|
|
return 1;
|
|
}
|
|
|
|
var outputPath = args[0];
|
|
var pagesPath = args[1];
|
|
var servicesPath = args.Length > 2 && !string.IsNullOrWhiteSpace(args[2]) ? args[2] : null;
|
|
var modelsPath = args.Length > 3 && !string.IsNullOrWhiteSpace(args[3]) ? args[3] : null;
|
|
|
|
if (!Directory.Exists(pagesPath))
|
|
{
|
|
Console.Error.WriteLine($"Pages path does not exist: {pagesPath}");
|
|
return 1;
|
|
}
|
|
|
|
try
|
|
{
|
|
Generate(outputPath, pagesPath, servicesPath, modelsPath);
|
|
Console.WriteLine($"Successfully generated llms.txt at: {outputPath}");
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Error.WriteLine($"Error generating llms.txt: {ex.Message}");
|
|
Console.Error.WriteLine(ex.StackTrace);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
static void Generate(string outputPath, string pagesPath, string servicesPath, string modelsPath)
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine("# Radzen Blazor Components - Demo Application");
|
|
sb.AppendLine();
|
|
sb.AppendLine("This file contains all demo pages and examples from the Radzen Blazor Components demo application.");
|
|
sb.AppendLine($"Generated: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss UTC}");
|
|
sb.AppendLine();
|
|
sb.AppendLine("## Table of Contents");
|
|
sb.AppendLine();
|
|
|
|
// Collect all demo files
|
|
var allPages = Directory.GetFiles(pagesPath, "*.razor", SearchOption.AllDirectories)
|
|
.Where(f => !Path.GetFileName(f).StartsWith("_"))
|
|
.ToList();
|
|
|
|
// Separate main pages (with @page directive) from example components
|
|
var mainPages = new List<string>();
|
|
var examplePages = new List<string>();
|
|
|
|
foreach (var page in allPages)
|
|
{
|
|
var content = File.ReadAllText(page, Encoding.UTF8);
|
|
// Check if it has @page directive - main pages have routes
|
|
if (Regex.IsMatch(content, @"^\s*@page\s+", RegexOptions.Multiline | RegexOptions.IgnoreCase))
|
|
{
|
|
mainPages.Add(page);
|
|
}
|
|
else
|
|
{
|
|
examplePages.Add(page);
|
|
}
|
|
}
|
|
|
|
// Sort main pages ascending by filename and apply exclusions
|
|
mainPages = mainPages
|
|
.Where(p => !IsExcluded(p))
|
|
.OrderBy(p => Path.GetFileName(p)).ToList();
|
|
|
|
// Keep all non-excluded pages for content generation (sorted ascending)
|
|
var pages = allPages
|
|
.Where(p => !IsExcluded(p))
|
|
.OrderBy(p => Path.GetFileName(p)).ToList();
|
|
|
|
var pageCs = Directory.GetFiles(pagesPath, "*.cs", SearchOption.AllDirectories)
|
|
.OrderBy(f => Path.GetFileName(f))
|
|
.ToList();
|
|
|
|
var services = !string.IsNullOrEmpty(servicesPath) && Directory.Exists(servicesPath)
|
|
? Directory.GetFiles(servicesPath, "*.cs", SearchOption.AllDirectories).OrderBy(f => Path.GetFileName(f)).ToList()
|
|
: Enumerable.Empty<string>();
|
|
|
|
var models = !string.IsNullOrEmpty(modelsPath) && Directory.Exists(modelsPath)
|
|
? Directory.GetFiles(modelsPath, "*.cs", SearchOption.AllDirectories).OrderBy(f => Path.GetFileName(f)).ToList()
|
|
: Enumerable.Empty<string>();
|
|
|
|
// Generate table of contents - only include main pages
|
|
sb.AppendLine("### Demo Pages");
|
|
foreach (var page in mainPages)
|
|
{
|
|
var relativePath = Path.GetRelativePath(pagesPath, page).Replace('\\', '/');
|
|
var fileName = Path.GetFileNameWithoutExtension(page);
|
|
sb.AppendLine($"- [{fileName}](#{SanitizeAnchor(fileName)}) - `{relativePath}`");
|
|
}
|
|
|
|
if (pageCs.Any())
|
|
{
|
|
sb.AppendLine();
|
|
sb.AppendLine("### Code-Behind Files");
|
|
foreach (var csFile in pageCs)
|
|
{
|
|
var fileName = Path.GetFileNameWithoutExtension(csFile);
|
|
sb.AppendLine($"- [{fileName}](#{SanitizeAnchor(fileName)}-code-behind)");
|
|
}
|
|
}
|
|
|
|
sb.AppendLine();
|
|
sb.AppendLine("---");
|
|
sb.AppendLine();
|
|
|
|
// Add demo pages
|
|
sb.AppendLine("## Demo Pages");
|
|
sb.AppendLine();
|
|
|
|
foreach (var page in pages)
|
|
{
|
|
var relativePath = Path.GetRelativePath(pagesPath, page).Replace('\\', '/');
|
|
var fileName = Path.GetFileNameWithoutExtension(page);
|
|
var fileContent = File.ReadAllText(page, Encoding.UTF8);
|
|
var extractedContent = ExtractDescriptionsAndExamples(fileContent, page);
|
|
|
|
if (string.IsNullOrWhiteSpace(extractedContent))
|
|
continue;
|
|
|
|
// Skip sections that produce negligible content — these are typically
|
|
// sub-component pages whose rendered placeholder text leaked through
|
|
// (e.g. "km/h", "Value is:", single comma).
|
|
var textOnly = Regex.Replace(extractedContent, @"```[\s\S]*?```", ""); // strip code blocks
|
|
textOnly = Regex.Replace(textOnly, @"^#{1,6}\s+.*$", "", RegexOptions.Multiline); // strip headings
|
|
textOnly = Regex.Replace(textOnly, @"^\*\*Path:\*\*.*$", "", RegexOptions.Multiline); // strip path
|
|
textOnly = Regex.Replace(textOnly, @"^Example:$", "", RegexOptions.Multiline); // strip "Example:" labels
|
|
textOnly = textOnly.Trim();
|
|
if (textOnly.Length < 20)
|
|
continue;
|
|
|
|
sb.AppendLine($"### {fileName}");
|
|
sb.AppendLine();
|
|
sb.AppendLine($"**Path:** `{relativePath}`");
|
|
sb.AppendLine();
|
|
sb.AppendLine(extractedContent);
|
|
sb.AppendLine();
|
|
sb.AppendLine("---");
|
|
sb.AppendLine();
|
|
}
|
|
|
|
// Add C# code-behind files
|
|
if (pageCs.Any())
|
|
{
|
|
sb.AppendLine("## Code-Behind Files");
|
|
sb.AppendLine();
|
|
|
|
foreach (var csFile in pageCs)
|
|
{
|
|
var relativePath = Path.GetRelativePath(pagesPath, csFile).Replace('\\', '/');
|
|
var fileName = Path.GetFileNameWithoutExtension(csFile);
|
|
var fileContent = File.ReadAllText(csFile, Encoding.UTF8);
|
|
|
|
sb.AppendLine($"### {fileName} (Code-Behind)");
|
|
sb.AppendLine();
|
|
sb.AppendLine($"**Path:** `{relativePath}`");
|
|
sb.AppendLine();
|
|
sb.AppendLine("```csharp");
|
|
sb.AppendLine(fileContent);
|
|
sb.AppendLine("```");
|
|
sb.AppendLine();
|
|
sb.AppendLine("---");
|
|
sb.AppendLine();
|
|
}
|
|
}
|
|
|
|
// Add services
|
|
if (services.Any())
|
|
{
|
|
sb.AppendLine("## Services");
|
|
sb.AppendLine();
|
|
|
|
foreach (var service in services)
|
|
{
|
|
var relativePath = Path.GetRelativePath(servicesPath, service).Replace('\\', '/');
|
|
var fileName = Path.GetFileNameWithoutExtension(service);
|
|
var fileContent = File.ReadAllText(service, Encoding.UTF8);
|
|
|
|
sb.AppendLine($"### {fileName}");
|
|
sb.AppendLine();
|
|
sb.AppendLine($"**Path:** `{relativePath}`");
|
|
sb.AppendLine();
|
|
sb.AppendLine("```csharp");
|
|
sb.AppendLine(fileContent);
|
|
sb.AppendLine("```");
|
|
sb.AppendLine();
|
|
sb.AppendLine("---");
|
|
sb.AppendLine();
|
|
}
|
|
}
|
|
|
|
// Add models
|
|
if (models.Any())
|
|
{
|
|
sb.AppendLine("## Data Models");
|
|
sb.AppendLine();
|
|
|
|
foreach (var model in models)
|
|
{
|
|
var relativePath = Path.GetRelativePath(modelsPath, model).Replace('\\', '/');
|
|
var fileName = Path.GetFileNameWithoutExtension(model);
|
|
var fileContent = File.ReadAllText(model, Encoding.UTF8);
|
|
|
|
sb.AppendLine($"### {fileName}");
|
|
sb.AppendLine();
|
|
sb.AppendLine($"**Path:** `{relativePath}`");
|
|
sb.AppendLine();
|
|
sb.AppendLine("```csharp");
|
|
sb.AppendLine(fileContent);
|
|
sb.AppendLine("```");
|
|
sb.AppendLine();
|
|
sb.AppendLine("---");
|
|
sb.AppendLine();
|
|
}
|
|
}
|
|
|
|
// Write to file
|
|
var outputDir = Path.GetDirectoryName(outputPath);
|
|
if (!string.IsNullOrEmpty(outputDir))
|
|
{
|
|
Directory.CreateDirectory(outputDir);
|
|
}
|
|
File.WriteAllText(outputPath, sb.ToString(), Encoding.UTF8);
|
|
}
|
|
|
|
static string SanitizeAnchor(string text)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
return "";
|
|
|
|
// Markdown heading anchors are generated by:
|
|
// 1. Convert to lowercase
|
|
// 2. Replace spaces and special chars with hyphens
|
|
// 3. Remove multiple consecutive hyphens
|
|
// 4. Trim hyphens from start/end
|
|
|
|
var anchor = text.ToLower()
|
|
.Replace(" ", "-")
|
|
.Replace("_", "-")
|
|
.Replace(".", "-")
|
|
.Replace("(", "")
|
|
.Replace(")", "")
|
|
.Replace("[", "")
|
|
.Replace("]", "")
|
|
.Replace("{", "")
|
|
.Replace("}", "")
|
|
.Replace("&", "")
|
|
.Replace(":", "")
|
|
.Replace(";", "")
|
|
.Replace("!", "")
|
|
.Replace("?", "")
|
|
.Replace("*", "")
|
|
.Replace("/", "-")
|
|
.Replace("\\", "-")
|
|
.Replace("+", "-")
|
|
.Replace("=", "-")
|
|
.Replace("@", "")
|
|
.Replace("#", "")
|
|
.Replace("%", "")
|
|
.Replace("$", "")
|
|
.Replace("^", "")
|
|
.Replace("~", "")
|
|
.Replace("`", "")
|
|
.Replace("'", "")
|
|
.Replace("\"", "");
|
|
|
|
// Remove multiple consecutive hyphens
|
|
anchor = Regex.Replace(anchor, @"-+", "-");
|
|
|
|
// Trim hyphens from start and end
|
|
anchor = anchor.Trim('-');
|
|
|
|
return anchor;
|
|
}
|
|
|
|
static string ExtractDescriptionsAndExamples(string razorContent, string pagePath)
|
|
{
|
|
var result = new StringBuilder();
|
|
var pagesDirectory = Path.GetDirectoryName(pagePath) ?? "";
|
|
var seenText = new HashSet<string>(StringComparer.Ordinal);
|
|
|
|
// Remove @code blocks entirely
|
|
razorContent = Regex.Replace(razorContent,
|
|
@"@code\s*\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}",
|
|
"",
|
|
RegexOptions.Singleline | RegexOptions.IgnoreCase);
|
|
|
|
// Remove @page, @inject, @layout directives
|
|
razorContent = Regex.Replace(razorContent,
|
|
@"@(page|inject|layout|using|namespace|implements)[^\r\n]*",
|
|
"",
|
|
RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
|
|
// Split content into lines to process sequentially
|
|
var lines = razorContent.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
|
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
var line = lines[i];
|
|
|
|
// Check for RadzenText
|
|
if (line.Contains("<RadzenText"))
|
|
{
|
|
var textContent = ExtractRadzenTextContent(lines, ref i);
|
|
if (!string.IsNullOrWhiteSpace(textContent.Content) && seenText.Add(textContent.Content))
|
|
{
|
|
// Format as heading if it's a heading style
|
|
if (textContent.IsHeading)
|
|
{
|
|
result.AppendLine();
|
|
result.AppendLine(textContent.Content);
|
|
result.AppendLine();
|
|
}
|
|
else
|
|
{
|
|
result.AppendLine(textContent.Content);
|
|
}
|
|
}
|
|
}
|
|
// Check for RadzenExample
|
|
else if (line.Contains("<RadzenExample"))
|
|
{
|
|
var exampleContent = ExtractRadzenExampleContent(lines, ref i, pagesDirectory);
|
|
if (!string.IsNullOrWhiteSpace(exampleContent))
|
|
{
|
|
result.AppendLine();
|
|
result.AppendLine("Example:");
|
|
result.AppendLine("```razor");
|
|
result.AppendLine(exampleContent);
|
|
result.AppendLine("```");
|
|
result.AppendLine();
|
|
}
|
|
}
|
|
}
|
|
|
|
return result.ToString().Trim();
|
|
}
|
|
|
|
static (string Content, bool IsHeading) ExtractRadzenTextContent(string[] lines, ref int index)
|
|
{
|
|
var fullTag = new StringBuilder();
|
|
var depth = 0;
|
|
|
|
// Collect the complete RadzenText tag (may span multiple lines)
|
|
for (int i = index; i < lines.Length; i++)
|
|
{
|
|
var line = lines[i];
|
|
fullTag.AppendLine(line);
|
|
|
|
// Count opening and closing tags
|
|
var openMatches = Regex.Matches(line, @"<RadzenText", RegexOptions.IgnoreCase);
|
|
var closeMatches = Regex.Matches(line, @"</RadzenText>", RegexOptions.IgnoreCase);
|
|
|
|
depth += openMatches.Count - closeMatches.Count;
|
|
|
|
if (closeMatches.Count > 0 && depth == 0)
|
|
{
|
|
index = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
var tagContent = fullTag.ToString();
|
|
|
|
// Only treat RadzenText as a heading when it has an explicit TextStyle.
|
|
// TagName (e.g. TagName="TagName.H2") is just an HTML rendering hint used
|
|
// for visual styling (marketing text, testimonials, CTAs) and does NOT
|
|
// indicate a semantic documentation heading.
|
|
var textStyleMatch = Regex.Match(tagContent, @"TextStyle=""TextStyle\.(H[2-6])""", RegexOptions.IgnoreCase);
|
|
|
|
bool isHeading = false;
|
|
int headingLevel = 0;
|
|
|
|
if (textStyleMatch.Success)
|
|
{
|
|
var hMatch = Regex.Match(textStyleMatch.Groups[1].Value, @"H(\d)");
|
|
if (hMatch.Success)
|
|
{
|
|
headingLevel = int.Parse(hMatch.Groups[1].Value);
|
|
isHeading = headingLevel >= 2 && headingLevel <= 6;
|
|
}
|
|
}
|
|
|
|
// Extract inner content
|
|
var contentMatch = Regex.Match(tagContent, @"<RadzenText[^>]*>([\s\S]*?)</RadzenText>", RegexOptions.IgnoreCase);
|
|
if (!contentMatch.Success)
|
|
return (string.Empty, false);
|
|
|
|
var content = contentMatch.Groups[1].Value.Trim();
|
|
|
|
// Convert <code> tags to markdown inline code BEFORE converting links
|
|
content = ConvertCodeTagsToMarkdown(content);
|
|
|
|
// For headings, strip RadzenLink elements entirely (CTA buttons are not heading text).
|
|
// For non-headings, convert them to markdown links.
|
|
if (isHeading)
|
|
content = Regex.Replace(content, @"<RadzenLink[^>]*/\s*>|<RadzenLink[^>]*>[\s\S]*?</RadzenLink>", "", RegexOptions.IgnoreCase);
|
|
else
|
|
content = ConvertRadzenLinksToMarkdown(content);
|
|
|
|
// Remove all HTML tags (except markdown links which are already converted)
|
|
content = Regex.Replace(content, @"<[^>]+>", "");
|
|
|
|
// Remove Razor/C# expressions: @(...), @variable, $"...", ?.Member, etc.
|
|
content = Regex.Replace(content, @"@\([^)]*\)", ""); // @(...) expressions
|
|
content = Regex.Replace(content, @"@[A-Za-z0-9_.()]+", ""); // @variable references
|
|
content = Regex.Replace(content, @"\$""[^""]*""", ""); // $"..." string interpolations
|
|
content = Regex.Replace(content, @"\?\.\w+", ""); // ?.Member null-conditional access
|
|
|
|
// Clean up whitespace
|
|
content = Regex.Replace(content, @"\s+", " ").Trim();
|
|
|
|
// Skip content that looks like leaked C#/Razor rather than documentation text.
|
|
// Indicators: unbalanced parentheses, remaining code artifacts like => or {}.
|
|
if (content.Contains("=>") || content.Contains("FilterOperator") || content.Contains("FilterValue"))
|
|
return (string.Empty, false);
|
|
|
|
// Format as heading if needed.
|
|
// Markdown heading budget:
|
|
// # = document title
|
|
// ## = major sections (Demo Pages, Code-Behind)
|
|
// ### = page name (AccordionPage)
|
|
// #### = component heading (Accordion)
|
|
// ##### = sub-feature (Accordion with single expand)
|
|
// ###### = detail
|
|
if (isHeading && !string.IsNullOrWhiteSpace(content))
|
|
{
|
|
int markdownLevel = headingLevel switch
|
|
{
|
|
2 => 4, // H2 -> #### component heading
|
|
3 => 5, // H3 -> ##### sub-feature
|
|
4 => 5, // H4 -> ##### sub-feature
|
|
5 => 6, // H5 -> ###### detail
|
|
6 => 6, // H6 -> ###### detail
|
|
_ => 4
|
|
};
|
|
|
|
content = new string('#', markdownLevel) + " " + content;
|
|
}
|
|
|
|
return (content, isHeading);
|
|
}
|
|
|
|
static string ConvertCodeTagsToMarkdown(string content)
|
|
{
|
|
// Match <code>...</code> tags
|
|
var codeMatches = Regex.Matches(content,
|
|
@"<code>([\s\S]*?)</code>",
|
|
RegexOptions.IgnoreCase);
|
|
|
|
// Process in reverse to maintain indices
|
|
var matchesArray = new Match[codeMatches.Count];
|
|
for (int i = 0; i < codeMatches.Count; i++)
|
|
{
|
|
matchesArray[i] = codeMatches[i];
|
|
}
|
|
for (int i = matchesArray.Length - 1; i >= 0; i--)
|
|
{
|
|
var match = matchesArray[i];
|
|
var codeContent = match.Groups[1].Value.Trim();
|
|
|
|
// Remove nested HTML tags from code content
|
|
codeContent = Regex.Replace(codeContent, @"<[^>]+>", "");
|
|
|
|
// Clean up @("@bind-Selected") to @bind-Selected (remove extra quotes and parentheses)
|
|
// Match: @("...") pattern and extract the content inside quotes
|
|
// Pattern: @("@bind-Selected") -> @bind-Selected
|
|
// Use non-verbatim string to avoid quote escaping issues
|
|
codeContent = Regex.Replace(codeContent, "@\\(\"([^\"]+)\"\\)", "$1");
|
|
codeContent = Regex.Replace(codeContent, "@\\('([^']+)'\\)", "$1");
|
|
|
|
if (!string.IsNullOrWhiteSpace(codeContent))
|
|
{
|
|
var markdownCode = $"`{codeContent}`";
|
|
content = content.Substring(0, match.Index) + markdownCode +
|
|
content.Substring(match.Index + match.Length);
|
|
}
|
|
}
|
|
|
|
return content;
|
|
}
|
|
|
|
static string ConvertRadzenLinksToMarkdown(string content)
|
|
{
|
|
// Handle both self-closing and opening/closing RadzenLink tags
|
|
// Match the entire tag first, then extract attributes
|
|
|
|
// Pattern for self-closing: <RadzenLink ... />
|
|
var selfClosingPattern = @"<RadzenLink([^>]*?)\s*/>";
|
|
// Pattern for opening/closing: <RadzenLink ...>...</RadzenLink>
|
|
var openClosePattern = @"<RadzenLink([^>]*?)>([\s\S]*?)</RadzenLink>";
|
|
|
|
// Process self-closing tags
|
|
var selfClosingMatches = Regex.Matches(content, selfClosingPattern, RegexOptions.IgnoreCase);
|
|
var selfClosingArray = new Match[selfClosingMatches.Count];
|
|
for (int i = 0; i < selfClosingMatches.Count; i++)
|
|
{
|
|
selfClosingArray[i] = selfClosingMatches[i];
|
|
}
|
|
for (int i = selfClosingArray.Length - 1; i >= 0; i--)
|
|
{
|
|
var match = selfClosingArray[i];
|
|
var attributes = match.Groups[1].Value;
|
|
var (path, text) = ExtractLinkAttributes(attributes, "");
|
|
|
|
if (!string.IsNullOrWhiteSpace(path))
|
|
{
|
|
string linkText = !string.IsNullOrWhiteSpace(text) ? text : path;
|
|
var markdownLink = $"[{linkText}]({path})";
|
|
content = content.Substring(0, match.Index) + markdownLink +
|
|
content.Substring(match.Index + match.Length);
|
|
}
|
|
}
|
|
|
|
// Process opening/closing tags
|
|
var linkMatches = Regex.Matches(content, openClosePattern, RegexOptions.IgnoreCase);
|
|
var matchesArray = new Match[linkMatches.Count];
|
|
for (int i = 0; i < linkMatches.Count; i++)
|
|
{
|
|
matchesArray[i] = linkMatches[i];
|
|
}
|
|
for (int i = matchesArray.Length - 1; i >= 0; i--)
|
|
{
|
|
var match = matchesArray[i];
|
|
var attributes = match.Groups[1].Value;
|
|
var innerContent = match.Groups[2].Value.Trim();
|
|
var (path, text) = ExtractLinkAttributes(attributes, innerContent);
|
|
|
|
if (!string.IsNullOrWhiteSpace(path))
|
|
{
|
|
string linkText = !string.IsNullOrWhiteSpace(text) ? text :
|
|
(!string.IsNullOrWhiteSpace(innerContent) ? Regex.Replace(innerContent, @"<[^>]+>", "").Trim() : path);
|
|
|
|
if (string.IsNullOrWhiteSpace(linkText))
|
|
linkText = path;
|
|
|
|
var markdownLink = $"[{linkText}]({path})";
|
|
content = content.Substring(0, match.Index) + markdownLink +
|
|
content.Substring(match.Index + match.Length);
|
|
}
|
|
}
|
|
|
|
return content;
|
|
}
|
|
|
|
static (string Path, string Text) ExtractLinkAttributes(string attributes, string innerContent)
|
|
{
|
|
string path = "";
|
|
string text = "";
|
|
|
|
// Extract Path attribute (can be in any order)
|
|
var pathMatch = Regex.Match(attributes, @"Path=[""]?([^""\s>]+)[""]?", RegexOptions.IgnoreCase);
|
|
if (pathMatch.Success)
|
|
{
|
|
path = pathMatch.Groups[1].Value;
|
|
}
|
|
|
|
// Extract Text attribute
|
|
var textMatch = Regex.Match(attributes, @"Text=[""]?([^""]+)[""]?", RegexOptions.IgnoreCase);
|
|
if (textMatch.Success)
|
|
{
|
|
text = textMatch.Groups[1].Value.Trim();
|
|
}
|
|
|
|
// If no Text attribute and there's inner content, use that
|
|
if (string.IsNullOrWhiteSpace(text) && !string.IsNullOrWhiteSpace(innerContent))
|
|
{
|
|
text = Regex.Replace(innerContent, @"<[^>]+>", "").Trim();
|
|
}
|
|
|
|
return (path, text);
|
|
}
|
|
|
|
static string ExtractRadzenExampleContent(string[] lines, ref int index, string pagesDirectory)
|
|
{
|
|
var fullTag = new StringBuilder();
|
|
var depth = 0;
|
|
|
|
// Collect the complete RadzenExample tag
|
|
for (int i = index; i < lines.Length; i++)
|
|
{
|
|
var line = lines[i];
|
|
fullTag.AppendLine(line);
|
|
|
|
var openMatches = Regex.Matches(line, @"<RadzenExample", RegexOptions.IgnoreCase);
|
|
var closeMatches = Regex.Matches(line, @"</RadzenExample>", RegexOptions.IgnoreCase);
|
|
|
|
depth += openMatches.Count - closeMatches.Count;
|
|
|
|
if (closeMatches.Count > 0 && depth == 0)
|
|
{
|
|
index = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
var tagContent = fullTag.ToString();
|
|
|
|
// Extract Example attribute
|
|
var exampleMatch = Regex.Match(tagContent, @"Example=[""]?([^""\s>]+)[""]?", RegexOptions.IgnoreCase);
|
|
if (exampleMatch.Success)
|
|
{
|
|
var exampleName = exampleMatch.Groups[1].Value.Trim();
|
|
var exampleFilePath = Path.Combine(pagesDirectory, $"{exampleName}.razor");
|
|
|
|
if (File.Exists(exampleFilePath))
|
|
{
|
|
var exampleContent = File.ReadAllText(exampleFilePath, Encoding.UTF8);
|
|
return CleanExampleFile(exampleContent);
|
|
}
|
|
}
|
|
|
|
// Fallback: extract inline content
|
|
var inlineMatch = Regex.Match(tagContent, @"<RadzenExample[^>]*>([\s\S]*?)</RadzenExample>", RegexOptions.IgnoreCase);
|
|
if (inlineMatch.Success)
|
|
{
|
|
return CleanExampleContent(inlineMatch.Groups[1].Value);
|
|
}
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
static string CleanTextContent(string content)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(content))
|
|
return string.Empty;
|
|
|
|
var result = content;
|
|
|
|
// Remove all HTML tags (including <strong>, <code>, <Radzen*>, etc.)
|
|
result = Regex.Replace(result, @"<[^>]+>", "");
|
|
|
|
// Remove @ expressions (like @variable, @ExampleService)
|
|
result = Regex.Replace(result, @"@[A-Za-z0-9_.()]+", "");
|
|
|
|
// Clean up multiple spaces but preserve single newlines for readability
|
|
result = Regex.Replace(result, @"[ \t]+", " ");
|
|
|
|
// Clean up multiple newlines (max 2 consecutive)
|
|
result = Regex.Replace(result, @"(\r?\n){3,}", Environment.NewLine + Environment.NewLine);
|
|
|
|
return result.Trim();
|
|
}
|
|
|
|
static string CleanExampleContent(string content)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(content))
|
|
return string.Empty;
|
|
|
|
var result = content;
|
|
|
|
// Remove nested RadzenExample tags if any
|
|
result = Regex.Replace(result,
|
|
@"<RadzenExample[^>]*>[\s\S]*?</RadzenExample>",
|
|
"",
|
|
RegexOptions.IgnoreCase);
|
|
|
|
// Trim each line and remove empty lines
|
|
var lines = result.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None)
|
|
.Select(l => l.Trim())
|
|
.Where(l => !string.IsNullOrWhiteSpace(l))
|
|
.ToList();
|
|
|
|
return string.Join(Environment.NewLine, lines);
|
|
}
|
|
|
|
static string CleanExampleFile(string content)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(content))
|
|
return string.Empty;
|
|
|
|
var result = content;
|
|
|
|
// Remove @code blocks
|
|
result = Regex.Replace(result,
|
|
@"@code\s*\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}",
|
|
"",
|
|
RegexOptions.Singleline | RegexOptions.IgnoreCase);
|
|
|
|
// Remove @using, @inject, @page directives
|
|
result = Regex.Replace(result,
|
|
@"@(using|inject|page|layout|namespace|implements)[^\r\n]*",
|
|
"",
|
|
RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
|
|
|
// Clean up multiple blank lines
|
|
result = Regex.Replace(result, @"(\r?\n\s*){3,}", Environment.NewLine + Environment.NewLine);
|
|
|
|
return result.Trim();
|
|
}
|
|
}
|
|
|