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

[CodeHighlight] Prevent hightlight.js from throwing error when language is unknown #5099

Closed
Songkeys opened this issue Oct 22, 2023 · 5 comments
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)

Comments

@Songkeys
Copy link
Contributor

What package has an issue

@mantine/code-highlight

Describe the bug

We get the result from highlight.js in this line without any error handling:

https://github.com/mantinedev/mantine/blob/a8e0dae3aeac876e0f016ca0b53922423661d6d3/src/mantine-code-highlight/src/use-highlight.ts#L11C46

which may resulting in the "Unknown language" error thrown by highlight.js in this line:

https://github.com/highlightjs/highlight.js/blob/17494c81dbade12834feedddf6fb208bcc9cbcce/src/highlight.js#L556

What version of @mantine/* packages do you have in package.json? (Note that all @mantine/* packages must have the same version in order to work correctly)

v7

If possible, please include a link to a codesandbox with the reproduced problem

No response

Do you know how to fix the issue

Yes

Are you willing to participate in fixing this issue and create a pull request with the fix

Yes

Possible fix

User can sovle this by wrapping a ErrorBoundary anyway but that brings some extra work.

So I'm thinking of these solutions:

  1. Fallbacking to language: plaintextif the language is unknown;
  2. or, providing a languageFallback prop if the language is unknown;
  3. or, providing a renderError prop if any error happens in the highlight and renders an alternative UI instead.

What do you think?

@rtivital
Copy link
Member

How can we know that the language is unknown?

@Songkeys
Copy link
Contributor Author

We could call the getLanguage method beforehand?

@rtivital
Copy link
Member

Why do you think it is a better idea to implement it on library side rather than on your application side?

@Songkeys
Copy link
Contributor Author

If developers like me encountering this problem, they might be rendering CodeHighlight with content from UGC (i.e. without control). The path to the solution will probably like: looking at the source code and figuring out the error coming from the underlying highlight.js library and uncatchable; choosing to

Though they are doable, neither seems ideal to me if this comes from a out-of-box UI library. Also it takes developer extra time and work to figure out and solve.

But I'm acceptable if the documentation gives a solution in this case? Like clarifying that the default built-in languages are listed here, and what to do if beyond the list.

rtivital added a commit that referenced this issue Nov 7, 2023
@rtivital rtivital added the Fixed patch Completed issues that will be published with next patch (1.0.X) label Nov 7, 2023
@rtivital
Copy link
Member

rtivital commented Nov 7, 2023

Fallback to plaintext was added in 7.2.1.

@rtivital rtivital closed this as completed Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)
Projects
None yet
Development

No branches or pull requests

2 participants