Skip to content

Commit 6580785

Browse files
authored
Improve highlight.php syntax highlighting (#2136)
1 parent f6056a8 commit 6580785

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cloudfour/patterns': patch
3+
---
4+
5+
Improves quality of syntax highlighting for JavaScript (and similar languages) using highlight.php 9.x

src/vendor/highlight/_theme.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
.hljs-name,
2222
.hljs-built_in,
23-
.hljs-title,
23+
.hljs-variable.language_,
2424
.language-scss .hljs-variable {
2525
color: color.$base-fuchsia-lighter;
2626
}
@@ -66,6 +66,7 @@
6666
.hljs-attribute,
6767
.hljs-regexp,
6868
.hljs-template-tag,
69+
.hljs-title,
6970
.hljs-variable {
7071
color: color.$base-blue-lighter;
7172
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<pre><code class="hljs language-javascript"><span class="hljs-comment">// Sweet useless condition</span>
2+
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span> !== <span class="hljs-string">'that'</span> || truth == <span class="hljs-literal">false</span>) {
3+
<span class="hljs-keyword">new</span> <span class="hljs-built_in">RegExp</span>(<span class="hljs-regexp">/ab+c/</span>, <span class="hljs-string">'i'</span>);
4+
boyHowdy(<span class="hljs-number">5</span>, <span class="hljs-string">'something'</span>);
5+
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'hello world'</span>);
6+
<span class="hljs-keyword">return</span> (<span class="hljs-number">2</span> * <span class="hljs-number">4</span>) / <span class="hljs-number">3</span>;
7+
}
8+
9+
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Dough</span> </span>{
10+
<span class="hljs-keyword">constructor</span>(slices = 12) {
11+
<span class="hljs-keyword">if</span> (slices &gt; <span class="hljs-number">24</span>) {
12+
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'Too much bread'</span>);
13+
}
14+
15+
<span class="hljs-keyword">this</span>.slices = <span class="hljs-string">`There are <span class="hljs-subst">${slices}</span> slices`</span>;
16+
}
17+
}
18+
19+
<span class="hljs-keyword">const</span> sum = <span class="hljs-function">(<span class="hljs-params">...args</span>) =&gt;</span> {
20+
<span class="hljs-keyword">return</span> args.reduce(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a + b, <span class="hljs-number">0</span>);
21+
};</code></pre>

src/vendor/highlight/demo/samples/js.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22
if (this !== 'that' || truth == false) {
33
new RegExp(/ab+c/, 'i');
44
boyHowdy(5, 'something');
5+
console.log('hello world');
56
return (2 * 4) / 3;
67
}
78

8-
class Bread {
9+
class Bread extends Dough {
910
constructor(slices = 12) {
11+
if (slices > 24) {
12+
throw new Error('Too much bread');
13+
}
14+
1015
this.slices = `There are ${slices} slices`;
1116
}
1217
}
18+
19+
const sum = (...args) => {
20+
return args.reduce((a, b) => a + b, 0);
21+
};

src/vendor/highlight/highlight.stories.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';
22
import { basename, extname } from 'path';
33
import hljs from 'highlight.js';
44
import themeDemo from './demo/theme.twig';
5+
import legacyDemo from './demo/legacy.twig';
56
// Load samples directory as "raw" text
67
const samplesDir = require.context('!!raw-loader!./demo/samples', false);
78
// Initialize an object to store sample content in
@@ -59,6 +60,16 @@ For performance, it's strongly recommended to apply syntax highlighting on the s
5960

6061
<ArgsTable story="Theme" />
6162

63+
## Backwards Compatibility
64+
65+
As of this writing, [highlight.php is two major versions behind Highlight.js](https://github.com/scrivo/highlight.php/issues/73). As a result, syntax highlighting may differ between platforms.
66+
67+
Here is an example of JavaScript highlighted using highlight.php version 9.8.1.10.
68+
69+
<Canvas>
70+
<Story name="PHP Sample">{legacyDemo.bind()}</Story>
71+
</Canvas>
72+
6273
## Syntax-highlighting Code Block
6374

6475
The [Syntax-highlighting Code Block plugin for WordPress](https://wordpress.org/plugins/syntax-highlighting-code-block/) enhances the core code block with [highlight.php](https://github.com/scrivo/highlight.php). It also adds a few additional features, including a code language label for accessibility.

0 commit comments

Comments
 (0)