question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Highlighted code interpreted as actual javascript when using custom highlighter

See original GitHub issue

Given

```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">&#x27;&lt;Button /&gt;&#x27;</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>`;
}

https://mdsvex.com/docs#highlight

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
pngwncommented, Aug 22, 2020

Yes sorry, was just pseudocode, the contesnt of @html needs to be an expression so a slight adjustment to your code:

return (
	'{@html \`<pre class="hljs"><code>' +
	hljs.highlight(lang, code, true).value +
	'</code></pre>\`}'
);

We need to wrap the HTML string that we want to render in backticks in order to satisfy the compiler: {@html expression}

0reactions
eps1loncommented, Aug 15, 2020

const highlight = (code, lang) => {@html \my highlighted code` }`;

I don’t understand this. Did you mean to put the tokenized code after the @html?

return (
	'{@html <pre class="hljs"><code>' +
	hljs.highlight(lang, code, true).value +
	'</code></pre>}'
);

Otherwise I don’t know what the code you posted is supposed to do.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found