lab / code highlighting
Code highlighting
Demonstrates syntax highlighting for C#, SQL, Blazor, and PowerShell using the HTML <code> tag and Prism.js.
Highlighted output
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello, World!");
}
}How Prism.js syntax highlighting works
- Prism.js CSS and JS were added to wwwroot and referenced in the layout.
- A small JS function
prismHighlightAllwas created to callPrism.highlightAll(). - The Blazor page calls this JS function after every render to ensure code blocks are highlighted, even after tab changes.
- Code samples are wrapped in
<pre><code class="language-xxx">...</code></pre>blocks for Prism.js to style.
Sample usage
<pre>
<code class="language-csharp">
public class HelloWorld
{
public static void Main()
{
Console.WriteLine("Hello, World!");
}
}
</code>
</pre>References
For more detailed information and advanced scenarios, check out these helpful resources:
- Prism.js WebsiteOfficial Prism.js documentation and downloads.
- Prism.js Themes on GitHubA collection of additional Prism.js themes.