5c4aaf91df
This commit introduces the Note and NoteHistory entities, along with their respective configurations for Entity Framework Core. The AppDbContext has been updated to include DbSet properties for both entities. A new INotesService interface and its implementation, NotesService, have been created to handle CRUD operations for notes, including history tracking. Additionally, several Blazor components have been added for note management, including dialogs for editing notes and viewing note history. The UI has been enhanced to support markdown content rendering and improved user interaction for note creation and editing. These changes contribute to a comprehensive note-taking feature within the application.
49 lines
1.6 KiB
C#
49 lines
1.6 KiB
C#
using Markdig;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace WebApp.Services;
|
|
|
|
/// <summary>
|
|
/// Helper class for rendering markdown to HTML.
|
|
/// </summary>
|
|
public static class MarkdownHelper
|
|
{
|
|
private static readonly MarkdownPipeline _pipeline = new MarkdownPipelineBuilder()
|
|
.UseAdvancedExtensions()
|
|
.Build();
|
|
|
|
/// <summary>
|
|
/// Converts markdown text to HTML.
|
|
/// </summary>
|
|
/// <param name="markdown">The markdown text to convert.</param>
|
|
/// <returns>HTML string ready for rendering.</returns>
|
|
public static string ToHtml(string? markdown)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(markdown))
|
|
return string.Empty;
|
|
|
|
var html = Markdown.ToHtml(markdown, _pipeline);
|
|
|
|
// Add target="_blank" and rel="noopener noreferrer" to all links
|
|
html = Regex.Replace(html, @"<a\s+([^>]*?)href=""([^""]*?)""([^>]*?)>",
|
|
match =>
|
|
{
|
|
var beforeHref = match.Groups[1].Value;
|
|
var href = match.Groups[2].Value;
|
|
var afterHref = match.Groups[3].Value;
|
|
|
|
// Check if target is already present
|
|
if (Regex.IsMatch(beforeHref + afterHref, @"target\s*=", RegexOptions.IgnoreCase))
|
|
{
|
|
return match.Value; // Return unchanged if target already exists
|
|
}
|
|
|
|
// Add target="_blank" and rel="noopener noreferrer"
|
|
return $"<a {beforeHref}href=\"{href}\" target=\"_blank\" rel=\"noopener noreferrer\"{afterHref}>";
|
|
},
|
|
RegexOptions.IgnoreCase);
|
|
|
|
return html;
|
|
}
|
|
}
|