MyNihongo / MudBlazor.Markdown

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

Bug: Unknown language in MudCodeHighlight #260

Closed SommerEngineering closed 1 month ago

SommerEngineering commented 1 month ago

Thanks for this great library πŸ˜€.

I am encountering an exception when using a Markdown code block with an unknown language, in this case razor:

Unhandled exception rendering component: Unknown language: "razor"
      p@http://localhost:5000/_content/MudBlazor.Markdown/MudBlazor.Markdown.min.js:1:15271
      m@http://localhost:5000/_content/MudBlazor.Markdown/MudBlazor.Markdown.min.js:1:12107
      @http://localhost:5000/_content/MudBlazor.Markdown/MudBlazor.Markdown.min.js:1:864294
      @http://localhost:5000/_framework/blazor.web.js:1:3244
      Promise@[native code]
      beginInvokeJSFromDotNet@http://localhost:5000/_framework/blazor.web.js:1:3212
      @http://localhost:5000/_framework/blazor.web.js:1:62919
      _invokeClientMethod@http://localhost:5000/_framework/blazor.web.js:1:63726
      _processIncomingData@http://localhost:5000/_framework/blazor.web.js:1:60408
      @http://localhost:5000/_framework/blazor.web.js:1:82184
Microsoft.JSInterop.JSException: Unknown language: "razor"
      p@http://localhost:5000/_content/MudBlazor.Markdown/MudBlazor.Markdown.min.js:1:15271
      m@http://localhost:5000/_content/MudBlazor.Markdown/MudBlazor.Markdown.min.js:1:12107
      @http://localhost:5000/_content/MudBlazor.Markdown/MudBlazor.Markdown.min.js:1:864294
      @http://localhost:5000/_framework/blazor.web.js:1:3244
      Promise@[native code]
      beginInvokeJSFromDotNet@http://localhost:5000/_framework/blazor.web.js:1:3212
      @http://localhost:5000/_framework/blazor.web.js:1:62919
      _invokeClientMethod@http://localhost:5000/_framework/blazor.web.js:1:63726
      _processIncomingData@http://localhost:5000/_framework/blazor.web.js:1:60408
      @http://localhost:5000/_framework/blazor.web.js:1:82184
         at Microsoft.JSInterop.JSRuntime.InvokeAsync[TValue](Int64 targetInstanceId, String identifier, Object[] args)
         at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)
         at MudBlazor.MudCodeHighlight.OnAfterRenderAsync(Boolean firstRender)
         at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

I have checked whether the markdig library is responsible for the error. However, it seems like markdig is not the source of the problem, as demonstrated by the following working code:

var result = Markdown.ToHtml("""
```foo
this is a test

"""); Console.WriteLine(result);



Therefore, I believe the issue arises from the `MudCodeHighlight` component in `MudBlazor.Markdown`. Unfortunately, I couldn't pinpoint the exact location in the code where the error was being produced, likely due to my limited familiarity with the codebase.

When an unknown language is encountered, I expect the code to be rendered without syntax highlighting.

Can we resolve this issue easily πŸ€·β€β™‚οΈ?
MihailsKuzmins commented 1 month ago

Recently code related to highlight.js has been changed, so most likely it's the cause of the issue. The exception seems to come from the js file and it makes the assumption more plausible.

I need to investigate why highlight.js that is used in the package does not recognise razor 😒

MihailsKuzmins commented 1 month ago

According to https://github.com/highlightjs/highlight.js/blob/main/SUPPORTED_LANGUAGES.md it seems that highlightjs-cshtml-razor also needs to be added to npm packages.. so let's see whether it solves the issue.