Skip to content

Commit

Permalink
Improve markdown rendering performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
edgett committed Jul 20, 2024
1 parent a7876bd commit 4c82672
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
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

0 comments on commit 4c82672

Please sign in to comment.