Skip to content

Commit e9b9785

Browse files
authored
Merge pull request #4107 from ralfhandl/fix/respec-erratic-syntax-highlighting
ReSpec build script: no automatic syntax highlighting for unspecified or "unknown" languages
2 parents 0e3eec8 + 281116b commit e9b9785

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

scripts/md2html/md2html.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,41 @@ const path = require('path');
1313
const url = require('url');
1414

1515
const hljs = require('highlight.js');
16+
hljs.registerLanguage('uritemplate', function() {
17+
return {
18+
case_insensitive: true,
19+
contains: [
20+
{
21+
scope: "attr",
22+
match: /(?<=[{,])[^,}\n\r]+/,
23+
}
24+
],
25+
}
26+
});
27+
hljs.registerLanguage('uri', function() {
28+
return {
29+
case_insensitive: true,
30+
classNameAliases: {
31+
pathsegment: "attr",
32+
option: "attr",
33+
value: "literal"
34+
},
35+
contains: [
36+
{
37+
scope: "pathsegment",
38+
match: /(?<=[/])[^/?#\n\r]+/,
39+
},
40+
{
41+
scope: "option",
42+
match: /(?<=[?&#])[^=?&#\n\r]+/,
43+
},
44+
{
45+
scope: "value",
46+
match: /(?<=\=)[^?&#\n\r]+/,
47+
}
48+
],
49+
}
50+
});
1651
const cheerio = require('cheerio');
1752

1853
let argv = require('yargs')
@@ -36,7 +71,8 @@ const md = require('markdown-it')({
3671
'</code></pre>';
3772
}
3873

39-
return '<pre class="highlight '+lang+'" tabindex="0"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
74+
if (lang) console.warn('highlight.js does not support language',lang);
75+
return '<pre class="nohighlight" tabindex="0"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
4076
}
4177
});
4278

tests/md2html/fixtures/basic-new.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
</code></pre>
2525
<pre class="nohighlight" tabindex="0"><code>text/plain
2626
</code></pre>
27-
<pre class="highlight " tabindex="0"><code>no language
27+
<pre class="nohighlight" tabindex="0"><code>no language
28+
</code></pre>
29+
<pre class="nohighlight" tabindex="0"><code>unknown language
30+
</code></pre>
31+
<pre class="nohighlight" tabindex="0"><code>https://<span class="hljs-attr">foo.com</span>/<span class="hljs-attr">bar</span>?<span class="hljs-attr">baz</span>=<span class="hljs-literal">qux</span>&amp;<span class="hljs-attr">fred</span>=<span class="hljs-literal">waldo</span>#<span class="hljs-attr">fragment</span>
32+
</code></pre>
33+
<pre class="nohighlight" tabindex="0"><code>https://foo.com/bar{<span class="hljs-attr">?baz*</span>,<span class="hljs-attr">qux</span>}
2834
</code></pre>
2935
</section></section><section class="appendix"><h1>Appendix A: Revision History</h1>
3036
<table>

tests/md2html/fixtures/basic-new.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ text/plain
5050
no language
5151
```
5252

53+
```unknown
54+
unknown language
55+
```
56+
57+
```uri
58+
https://foo.com/bar?baz=qux&fred=waldo#fragment
59+
```
60+
61+
```uritemplate
62+
https://foo.com/bar{?baz*,qux}
63+
```
64+
5365
## Appendix A: Revision History
5466

5567
Version | Date

0 commit comments

Comments
 (0)