highlightjs / highlightjs-cshtml-razor Goto Github PK
View Code? Open in Web Editor NEWHighlight.js Razor CSHTML language repository
License: Creative Commons Zero v1.0 Universal
Highlight.js Razor CSHTML language repository
License: Creative Commons Zero v1.0 Universal
I used the Static website or simple usage explained on the readme page.
<script type="text/javascript" src="/path/to/highlight.min.js"></script>
<script type="text/javascript" src="/path/to/highlightjs-cshtml-razor/dist/cshtml-razor.min.js"></script>
<script>
hljs.highlightAll();
</script>
Then how can I use it on an HTML page?
I tried the following:
<pre><code class="language-cshtml"> cshtml here </code></pre>
didn't work, then tried
<pre><code class="language-cshtml-razor"> cshtml here </code></pre>
no luck.
Thanks
Hi there - first and foremost thanks for the grammar!
I am finding that nested blocks of Razor break the formatting. Specifically I am using the following to illustrate this:
<markdown>
```cshtml
@@if (this)
{
<span>this</span>
@@if (this)
{
<span>this</span>
}
else if (that)
{
<span>that</span>
}
else {
<span>nope</span>
}
}
else if (that)
{
<span>that</span>
@@if (this)
{
<span>this</span>
}
else if (that)
{
<span>that</span>
}
else {
<span>nope</span>
}
}
else {
<span>nope</span>
@@if (this)
{
<span>this</span>
}
else if (that)
{
<span>that</span>
}
else {
<span>nope</span>
}
}
```
</markdown>
This results in the following HTML:
<code class="language-cshtml hljs language-cshtml-razor"><span class="hljs-built_in">@</span><span class="language-csharp"><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>)
</span><span class="hljs-built_in">{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp"><span class="hljs-keyword">this</span></span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">@</span><span class="language-csharp"><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>)
</span><span class="hljs-built_in">{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp"><span class="hljs-keyword">this</span></span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span><span class="language-csharp">
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (that)
{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp">that</span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span><span class="language-csharp">
<span class="hljs-keyword">else</span> </span><span class="hljs-built_in">{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp">nope</span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span>
}
else if (that)
{
<span class="hljs-tag"><<span class="hljs-name">span</span>></span>that<span class="hljs-tag"></<span class="hljs-name">span</span>></span>
<span class="hljs-built_in">@</span><span class="language-csharp"><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>)
</span><span class="hljs-built_in">{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp"><span class="hljs-keyword">this</span></span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span><span class="language-csharp">
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (that)
{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp">that</span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span><span class="language-csharp">
<span class="hljs-keyword">else</span> </span><span class="hljs-built_in">{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp">nope</span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span>
}
else {
<span class="hljs-tag"><<span class="hljs-name">span</span>></span>nope<span class="hljs-tag"></<span class="hljs-name">span</span>></span>
<span class="hljs-built_in">@</span><span class="language-csharp"><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>)
</span><span class="hljs-built_in">{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp"><span class="hljs-keyword">this</span></span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span><span class="language-csharp">
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (that)
{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp">that</span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span><span class="language-csharp">
<span class="hljs-keyword">else</span> </span><span class="hljs-built_in">{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp">nope</span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span>
}
</code>
Specifically, note the instances of e.g. }
, else if (that)
, {
, and else {
that have no surrounding HTML tags.
This ends up rendering (with the GitHub Dark theme) as such:
🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨
To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.
Since we didn’t receive a CI status on the greenkeeper/initial
branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.
If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/
.
Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.
I've been attempting to use your library with snippets of Blazor code (Razor syntax) in Google's Blogger blog to no avail. I was under the impression that the syntax isn't different to any great extent. All that appears in the
block are those tags that are preceded by an @ symbol.
Any suggestions on how to make this work?
I'm using
<link rel="stylesheet" href="/Resources/Shared/stylesheets/vs2015.css">
<script src="/Resources/Shared/scripts/highlight.pack.js"></script>
<script type="text/javascript" src="/Resources/Shared/scripts/cshtml.js"></script>
<script>
hljs.registerLanguage('cshtml', window.hljsDefineRazorCshtml);
hljs.initHighlightingOnLoad();
</script>
and getting this error
Uncaught TypeError: hljs.COMMENT is not a function
at hljsDefineRazorCshtml (cshtml.js:373)
at Object.N [as registerLanguage] (highlight.pack.js:2)
at adding-a-site:13
I'm using hljs v9.14.1 and the current cshtml.js
I want to use highlightjs to process my code which not in <code class='language'>
.But it at <div class='language'>
,How to specify language in function highlightBlock()
.
Contribute to the thought process for 3rd party language packaging if you'd like
As a maintainer of a Highlight.js language grammar you might be interested in the discussion of an official packaging format. I just created an issue to track the discussion and I've been working on this along with the new build system.
Sorry for the spam, but I couldn't think of an easier way to ping the people who might be most interested in weighing in on the subject. Feel free to simply close this issue or leave it open (whatever works best for you!).
This issue is being opened as part of our iniative to make all 3rd party grammars:
Please include a dist
folder that includes a ready-to-go CDN distributable that can be used with a single line of JS. IE, our simple 3 install should "just work" without any other code or markup:
<script type="text/javascript" src="/path/to/highlight.min.js"></script>
<script type="text/javascript" src="/path/to/highlightjs-cshtml-razor/cshtml-razor.js"></script>
<script type="text/javascript">hljs.highlightAll();</script>
Your README optimally would also document this usage. Please see the robots-txt
repository for a usage example.
Our build system can generate this file for you (and we recommend that). Using our internal build system also allows you to easily check your grammar for exponential or polynomical run-away regular expressions.
If you have any questions, feel free to ask!
Thanks,
Highlight.js Maintainer
Again, thanks very much for the grammar!
It seems like C# keywords are being highlighted when they are found in HTML that is nested inside of Razor blocks.
For instance, the following source:
<markdown>
```cshtml
@@if (this)
{
<span>Lorem is ipsum as dolor not sit this amet, for consectetur in adipiscing while elit.</span>
}
```
</markdown>
Results in the following HTML:
<code class="language-cshtml hljs language-cshtml-razor"><span class="hljs-built_in">@</span><span class="language-csharp"><span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>)
</span><span class="hljs-built_in">{</span><span class="language-csharp">
</span><span class="hljs-tag"><<span class="hljs-name">span</span>></span><span class="language-csharp">Lorem <span class="hljs-keyword">is</span> ipsum <span class="hljs-keyword">as</span> dolor <span class="hljs-keyword">not</span> sit <span class="hljs-keyword">this</span> amet, <span class="hljs-keyword">for</span> consectetur <span class="hljs-keyword">in</span> adipiscing <span class="hljs-keyword">while</span> elit.</span><span class="hljs-tag"></<span class="hljs-name">span</span>></span><span class="language-csharp">
</span><span class="hljs-built_in">}</span>
</code>
This ends up looking like the following (with the GitHub Dark theme):
Would you be open to a PR that adds an index.d.ts typings file to this library? I'm guessing it's not a super common use case but could come in handy sometimes.
It looks as if the highlighting is broken in Blazor/Razor files, where C# code inside @code
blocks are not correctly highlighted.
For example, this .razor file:
<Fixture Test="InitialHtmlIsCorrect">
<ComponentUnderTest>
<Heading />
</ComponentUnderTest>
<Fragment>
<h3 id:regex="heading-\d{4}" required>
Heading text
<small diff:ignore></small>
</h3>
</Fragment>
@code
{
void InitialHtmlIsCorrect(Fixture fixture)
{
// Arrange - Gets the Heading component
var cut = fixture.GetComponentUnderTest<Heading>();
// Assert
// Here we get expected HTML from the Fragment element above.
var expectedHtml = fixture.GetFragment();
// Here we use the HTML diffing library to assert that the rendered HTML
// from CUT is semantically the same as the expected HTML string above.
cut.MarkupMatches(expectedHtml);
}
}
</Fixture>
Becomes this HTML with highlightjs-cshtml-razor:
<code class="lang-razor hljs cshtml-razor" name="SemanticHtmlTest.razor"><span class="hljs-tag"><<span class="hljs-name">Fixture</span> <span class="hljs-attr">Test</span>=<span class="hljs-string">"InitialHtmlIsCorrect"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">ComponentUnderTest</span>></span>
<span class="hljs-tag"><<span class="hljs-name">Heading</span> /></span>
<span class="hljs-tag"></<span class="hljs-name">ComponentUnderTest</span>></span>
<span class="hljs-tag"><<span class="hljs-name">Fragment</span>></span>
<span class="hljs-tag"><<span class="hljs-name">h3</span> <span class="hljs-attr">id:regex</span>=<span class="hljs-string">"heading-\d{4}"</span> <span class="hljs-attr">required</span>></span>
Heading text
<span class="hljs-tag"><<span class="hljs-name">small</span> <span class="hljs-attr">diff:ignore</span>></span><span class="hljs-tag"></<span class="hljs-name">small</span>></span>
<span class="hljs-tag"></<span class="hljs-name">h3</span>></span>
<span class="hljs-tag"></<span class="hljs-name">Fragment</span>></span>
<span class="hljs-built_in">@</span><span class="csharp">code</span>
{
void InitialHtmlIsCorrect(Fixture fixture)
{
// Arrange - Gets the Heading component
var cut = fixture.GetComponentUnderTest<span class="hljs-tag"><<span class="hljs-name">Heading</span>></span>();
// Assert
// Here we get expected HTML from the Fragment element above.
var expectedHtml = fixture.GetFragment();
// Here we use the HTML diffing library to assert that the rendered HTML
// from CUT is semantically the same as the expected HTML string above.
cut.MarkupMatches(expectedHtml);
}
}
<span class="hljs-tag"></<span class="hljs-name">Fixture</span>></span>
</code>
And looks like this with the default highlight style:
Versions:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.