Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve markdown rendering performance. #16

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,73 @@ namespace PalmHill.BlazorChat.Client.Components.Markdown;
public partial class MarkdownSection : FluentComponentBase
{
private string? _content;
private bool _raiseContentConverted;
private bool _contentChanged;
private static readonly MarkdownPipeline _markdownPipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();

/// <summary>
/// Gets or sets the Markdown content
/// </summary>
[Parameter]
public string? Content { get; set; }

/// <summary>
/// Event callback for when the Markdown content is converted to HTML.
/// </summary>
[Parameter]
public EventCallback<string> ContentChanged { get; set; }

[Parameter]
public EventCallback OnContentConverted { get; set; }

public string? InternalContent
public MarkupString HtmlContent { get; private set; }

protected override void OnParametersSet()
{
get => _content;
set
if (Content != _content)
{
_content = value;
HtmlContent = ConvertToMarkupString(_content);


if (OnContentConverted.HasDelegate)
{
OnContentConverted.InvokeAsync();
}
_raiseContentConverted = true;
StateHasChanged();
_content = Content;
UpdateHtmlContent();
_contentChanged = true;
}
}

public MarkupString HtmlContent { get; private set; }


protected override void OnInitialized()
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (Content is null)
throw new ArgumentException("You need to provide either Content or FromAsset parameter");

InternalContent = Content;
if (_contentChanged)
{
_contentChanged = false;
await ContentChanged.InvokeAsync(_content);
await OnContentConverted.InvokeAsync();
}
}

protected override async Task OnAfterRenderAsync(bool firstRender)
public void SetContent(string content)
{
if (_raiseContentConverted)
if (content != _content)
{
_raiseContentConverted = false;
if (OnContentConverted.HasDelegate)
{
await OnContentConverted.InvokeAsync();
}

_content = content;
UpdateHtmlContent();
_contentChanged = true;
StateHasChanged();
}
}

public void RefreshContent()
private void UpdateHtmlContent()
{
InternalContent = Content;
HtmlContent = ConvertToMarkupString(_content);
}


private static MarkupString ConvertToMarkupString(string? value)
{
if (!string.IsNullOrWhiteSpace(value))
if (string.IsNullOrWhiteSpace(value))
{
// Convert markdown string to HTML
string? html = Markdig.Markdown.ToHtml(value, new MarkdownPipelineBuilder().UseAdvancedExtensions().Build());
return new MarkupString();
}

// Return sanitized HTML as a MarkupString that Blazor can render
try
{
string html = Markdig.Markdown.ToHtml(value, _markdownPipeline);
return new MarkupString(html);
}

return new MarkupString();
catch (Exception ex)
{
Console.Error.WriteLine($"Error converting Markdown to HTML: {ex.Message}");
return new MarkupString($"<p>Error rendering Markdown: {ex.Message}</p>");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
{
// Update the markdownToRender variable based on the new response
_inferenceMarkdownToRender = WebSocketChatMessage?.Resonse ?? string.Empty;
mdSection?.RefreshContent();
// Invoke a StateHasChanged to refresh the UI if needed
StateHasChanged();
}

Expand Down
2 changes: 1 addition & 1 deletion PalmHill.Llama/LlamaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static InjectedModel AddLlamaModel(this IHostApplicationBuilder builder,
var modelExsists = System.IO.File.Exists(modelConfig.ModelPath);
if (!modelExsists)
{
throw new FileNotFoundException($"Model file does not exsist.", modelConfig.ModelPath);
throw new FileNotFoundException($"Model file does not exist.", modelConfig.ModelPath);
}

//Initlize Llama
Expand Down
Loading