MyNihongo / MudBlazor.Markdown

Markdown component based on the MudBlazor environment
https://mudblazor.com/
MIT License
115 stars 13 forks source link

Issue when code block is empty #247

Closed SommerEngineering closed 2 months ago

SommerEngineering commented 2 months ago

First of all, I would like to thank you for the existence of this library. It made it effortless for me to integrate Markdown into my Blazor project. Thank you very much for your work 😀.

My issue: I have been working on a ChatGPT-like UI in Blazor (MudBlazor). I experimented with the OpenAI API, which streams the responses to the client. In doing so, I noticed a problem. Whenever the AI streams the Markdown text for a code block, Blazor crashed with a strange exception: System.NotImplementedException: Unexpected frame type during RemoveOldFrame 🤷‍♂️.

I quickly realized that it was due to empty code blocks. When the AI streams the Markdown text and there is code in the AI response, there is a temporary situation where an empty code block is created:


Then, a few milliseconds later, the Markdown code gets changed, e.g., into:
public

After some troubleshooting, I came to the conclusion that the issue lies in this library (okay, so actually the issue is in .NET, but here the situation can be easily avoided). Apparently, Blazor (at least in some places) has a problem with attributes being empty. I don't know what the underlying difficulty with this is exactly. It appears to be similar to https://github.com/dotnet/aspnetcore/issues/17821.

The issue is straightforward to catch. See line 176 in MudMarkdown.razor.cs onwards:

var text = code.CreateCodeBlockText();

builder.OpenComponent<MudCodeHighlight>(ElementIndex++);
builder.AddAttribute(ElementIndex++, nameof(MudCodeHighlight.Text), text);
builder.AddAttribute(ElementIndex++, nameof(MudCodeHighlight.Language), code.Info ?? string.Empty);
builder.AddAttribute(ElementIndex++, nameof(MudCodeHighlight.Theme), CodeBlockTheme);
builder.CloseComponent();

The crash of Blazor occurs when text is empty. I have already created a PR #246.

MihailsKuzmins commented 2 months ago

Sorry, I cannot reproduce your issue. I have tried the following markdown and it works without issues:

## Header


As well as I have set the `text` to `""` and `null` and there have been no issues.  
![image](https://github.com/MyNihongo/MudBlazor.Markdown/assets/47413092/1bcf80ed-8dff-4b4a-9c81-b10d80f1fec7)

Rendered stuff:  
![image](https://github.com/MyNihongo/MudBlazor.Markdown/assets/47413092/ce7a5aa3-27ae-4429-984f-4c269afabf66)

Therefore, could you provide a repository where your issue can be reproduced? Otherwise, I do not think that rendering an empty code block causes the issue.