Highlighted code interpreted as actual javascript when using custom highlighter
See original GitHub issueGiven
```js const { getByRole }= render(<Button />); ```
using a custom highlighter can lead mdsvex to consider this javascript resulting in a ReferenceError (getByRole is not defined
).
Highlighter used:
mdsvex({
extension: '.svx',
highlight: {
highlighter(code, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return (
'<pre class="hljs"><code>' +
hljs.highlight(lang, code, true).value +
'</code></pre>'
);
} catch (error) {
console.error(error);
}
}
return '';
},
},
});
which creates the following markup from the code above
<pre class="hljs"><code><span class="hljs-keyword">const</span> { fireEvent, getByRole } = render(<span class="hljs-string">'<Button />'</span>);
</code></pre>
The current behavior is already problematic if disabling syntax highlighting as documented:
function highlighter(code, lang) {
return `<pre><code>${code}</code></pre>`;
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Syntax Highlight Guide | Visual Studio Code Extension API
Syntax highlighting determines the color and style of source code displayed in the Visual Studio Code editor. It is responsible for colorizing keywords...
Read more >How to highlight text using javascript - Stack Overflow
It can highlight keywords separately or as a term, can highlight the match with your custom element and classname and can also search...
Read more >Javascript Highlight.js Syntax Highlighter to ... - YouTube
Download the full source code of application ...
Read more >How to Highlight the Searched String Result using JavaScript
First thing first when you are entering some string on the search box and press the search button then a simple JavaScript function...
Read more >How to use highlight.js
You can run highlighting inside a web worker to avoid freezing the browser window while dealing with very big chunks of code. In...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes sorry, was just pseudocode, the contesnt of
@html
needs to be an expression so a slight adjustment to your code:We need to wrap the HTML string that we want to render in backticks in order to satisfy the compiler:
{@html expression}
I don’t understand this. Did you mean to put the tokenized code after the
@html
?Otherwise I don’t know what the code you posted is supposed to do.