Skip to content

Commit d954b95

Browse files
committed
Sequential media highlight support
Improve text/event-stream support with comment support and generally better configuration (one rule wasn't doing anything). Add support for JSONL/NDJSON (basically JSON without the check for values separated only by whitespace) and JSON Text Sequences (which requires using `0x1E` as the record separator, and highlighting it separately).
1 parent b669542 commit d954b95

File tree

3 files changed

+121
-15
lines changed

3 files changed

+121
-15
lines changed

scripts/md2html/md2html.js

+58-6
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,70 @@ hljs.registerLanguage('eventstream', function() {
5252
return {
5353
contains: [
5454
{
55-
scope: "attr",
56-
begin: /^/,
57-
end: ":",
55+
scope: "comment",
56+
begin: /^:/,
57+
end: /$/,
5858
},
5959
{
60-
scope: "literal",
61-
begin: /: */,
62-
end: /$/,
60+
scope: "attr",
61+
match: /^[^:]+/
6362
},
6463
],
6564
}
6665
});
66+
hljs.registerLanguage('jsonseq', function() {
67+
return {
68+
keywords: ["true", "false", "null"],
69+
contains: [
70+
{
71+
scope: "meta",
72+
match: /0[xX]1[eE]/,
73+
},
74+
{
75+
scope: "attr",
76+
begin: /"(\\.|[^\\"\r\n])*"(?=\s*:)/,
77+
relevance: 1.01
78+
},
79+
{
80+
scope: "punctuation",
81+
match: /[{}[\],:]/,
82+
relevance: 0
83+
},
84+
{
85+
scope: "literals",
86+
beginKeywords: ["true", "false" , "null"].join(" "),
87+
},
88+
hljs.QUOTE_STRING_MODE,
89+
hljs.C_NUMBER_MODE
90+
]
91+
}
92+
});
93+
hljs.registerLanguage('jsonl', function() {
94+
return {
95+
aliases: ["ndjson"],
96+
keywords: ["true", "false", "null"],
97+
contains: [
98+
{
99+
scope: 'attr',
100+
begin: /"(\\.|[^\\"\r\n])*"(?=\s*:)/,
101+
relevance: 1.01
102+
},
103+
{
104+
scope: "punctuation",
105+
match: /[{}[\],:]/,
106+
relevance: 0
107+
},
108+
{
109+
scope: "literals",
110+
beginKeywords: ["true", "false" , "null"].join(" "),
111+
},
112+
hljs.QUOTE_STRING_MODE,
113+
hljs.C_NUMBER_MODE
114+
]
115+
}
116+
});
117+
118+
67119
const cheerio = require('cheerio');
68120

69121
let argv = require('yargs')

tests/md2html/fixtures/basic-new.html

+31-7
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,38 @@
4545
</code></pre>
4646
<pre class="nohighlight" tabindex="0"><code>https://foo.com/bar{<span class="hljs-attr">?baz*</span>,<span class="hljs-attr">qux</span>}
4747
</code></pre>
48-
<pre class="nohighlight" tabindex="0"><code><span class="hljs-attr">data:</span> This data is formatted
49-
<span class="hljs-attr">data:</span> across two lines
50-
<span class="hljs-attr">retry:</span> 5
48+
<pre class="nohighlight" tabindex="0"><code><span class="hljs-attr">event</span>: addString
49+
<span class="hljs-attr">data</span>: This data is formatted
50+
<span class="hljs-attr">data</span>: across two lines
51+
<span class="hljs-attr">retry</span>: 5
5152
<span class="hljs-attr">
52-
event:</span> add
53-
<span class="hljs-attr">data:</span> 1234.5678
54-
<span class="hljs-attr">unknown-field:</span> this is ignored
55-
<span class="hljs-attr"></span></code></pre>
53+
event</span>: addNumber
54+
<span class="hljs-attr">data</span>: 1234.5678
55+
<span class="hljs-attr">unknownField</span>: this is ignored
56+
<span class="hljs-attr">
57+
</span><span class="hljs-comment">: This is a comment</span>
58+
<span class="hljs-attr">event</span>: addJSON
59+
<span class="hljs-attr">data</span>: {&quot;foo&quot;: 42}
60+
</code></pre>
61+
<pre class="nohighlight" tabindex="0"><code><span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;event&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;addString&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&quot;data&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;This data is formatted\nacross two lines&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&quot;retry&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5</span><span class="hljs-punctuation">}</span>
62+
<span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;event&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;addNumber&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&quot;data&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1234.5678&quot;</span><span class="hljs-punctuation">}</span>
63+
<span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;event&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;addJSON&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&quot;data&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;{\&quot;foo\&quot;: 42}&quot;</span><span class="hljs-punctuation">}</span>
64+
</code></pre>
65+
<pre class="nohighlight" tabindex="0"><code><span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;event&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;addString&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&quot;data&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;This data is formatted\nacross two lines&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&quot;retry&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">5</span><span class="hljs-punctuation">}</span>
66+
<span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;event&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;addNumber&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&quot;data&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1234.5678&quot;</span><span class="hljs-punctuation">}</span>
67+
<span class="hljs-punctuation">{</span><span class="hljs-attr">&quot;event&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;addJSON&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">&quot;data&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;{\&quot;foo\&quot;: 42}&quot;</span><span class="hljs-punctuation">}</span>
68+
</code></pre>
69+
<pre class="nohighlight" tabindex="0"><code><span class="hljs-meta">0x1E</span><span class="hljs-punctuation">{</span>
70+
<span class="hljs-attr">&quot;timestamp&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1985-04-12T23:20:50.52Z&quot;</span><span class="hljs-punctuation">,</span>
71+
<span class="hljs-attr">&quot;level&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span>
72+
<span class="hljs-attr">&quot;message&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;Hi!&quot;</span>
73+
<span class="hljs-punctuation">}</span>
74+
<span class="hljs-meta">0x1E</span><span class="hljs-punctuation">{</span>
75+
<span class="hljs-attr">&quot;timestamp&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;1985-04-12T23:20:51.37Z&quot;</span><span class="hljs-punctuation">,</span>
76+
<span class="hljs-attr">&quot;level&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span>
77+
<span class="hljs-attr">&quot;message&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;Bye!&quot;</span>
78+
<span class="hljs-punctuation">}</span>
79+
</code></pre>
5680
</section></section><section class="appendix"><h1>Appendix A: Revision History</h1>
5781
<table>
5882
<thead>

tests/md2html/fixtures/basic-new.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,43 @@ https://foo.com/bar{?baz*,qux}
6363
```
6464

6565
```eventstream
66+
event: addString
6667
data: This data is formatted
6768
data: across two lines
6869
retry: 5
6970
70-
event: add
71+
event: addNumber
7172
data: 1234.5678
72-
unknown-field: this is ignored
73+
unknownField: this is ignored
74+
75+
: This is a comment
76+
event: addJSON
77+
data: {"foo": 42}
78+
```
79+
80+
```jsonl
81+
{"event": "addString", "data": "This data is formatted\nacross two lines", "retry": 5}
82+
{"event": "addNumber", "data": "1234.5678"}
83+
{"event": "addJSON", "data": "{\"foo\": 42}"}
84+
```
85+
86+
```ndjson
87+
{"event": "addString", "data": "This data is formatted\nacross two lines", "retry": 5}
88+
{"event": "addNumber", "data": "1234.5678"}
89+
{"event": "addJSON", "data": "{\"foo\": 42}"}
90+
```
91+
92+
```jsonseq
93+
0x1E{
94+
"timestamp": "1985-04-12T23:20:50.52Z",
95+
"level": 1,
96+
"message": "Hi!"
97+
}
98+
0x1E{
99+
"timestamp": "1985-04-12T23:20:51.37Z",
100+
"level": 1,
101+
"message": "Bye!"
102+
}
73103
```
74104

75105
## Appendix A: Revision History

0 commit comments

Comments
 (0)