Skip to content

Commit de0f9c2

Browse files
authored
Merge pull request #4553 from handrews/hljs-seq
highlight.js: support for JSONL, JSON-Seq, basic multipart, improve text/event-stream
2 parents c0ec59d + a3c408a commit de0f9c2

File tree

3 files changed

+195
-14
lines changed

3 files changed

+195
-14
lines changed

scripts/md2html/md2html.js

+82-5
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,99 @@ hljs.registerLanguage('uri', function() {
4848
],
4949
}
5050
});
51-
hljs.registerLanguage('eventstream', function() {
51+
hljs.registerLanguage('multipart', function() {
5252
return {
53+
// This is a very limited approach that only
54+
// detects boundaries and headers that start
55+
// with "Content-"
5356
contains: [
5457
{
55-
scope: "attr",
56-
begin: /^/,
57-
end: ":",
58+
scope: "meta",
59+
match: /^--.*$/,
5860
},
5961
{
6062
scope: "literal",
61-
begin: /: */,
63+
begin: /^Content-/,
64+
end: /$/,
65+
contains: [
66+
{
67+
scope: "attr",
68+
begin: " ",
69+
end: /$/,
70+
},
71+
]
72+
},
73+
],
74+
}
75+
});
76+
hljs.registerLanguage('eventstream', function() {
77+
return {
78+
contains: [
79+
{
80+
scope: "comment",
81+
begin: /^:/,
6282
end: /$/,
6383
},
84+
{
85+
scope: "attr",
86+
match: /^[^:]+/
87+
},
6488
],
6589
}
6690
});
91+
hljs.registerLanguage('jsonseq', function() {
92+
return {
93+
keywords: ["true", "false", "null"],
94+
contains: [
95+
{
96+
scope: "meta",
97+
match: /0[xX]1[eE]/,
98+
},
99+
{
100+
scope: "attr",
101+
begin: /"(\\.|[^\\"\r\n])*"(?=\s*:)/,
102+
relevance: 1.01
103+
},
104+
{
105+
scope: "punctuation",
106+
match: /[{}[\],:]/,
107+
relevance: 0
108+
},
109+
{
110+
scope: "literals",
111+
beginKeywords: ["true", "false" , "null"].join(" "),
112+
},
113+
hljs.QUOTE_STRING_MODE,
114+
hljs.C_NUMBER_MODE
115+
]
116+
}
117+
});
118+
hljs.registerLanguage('jsonl', function() {
119+
return {
120+
aliases: ["ndjson"],
121+
keywords: ["true", "false", "null"],
122+
contains: [
123+
{
124+
scope: 'attr',
125+
begin: /"(\\.|[^\\"\r\n])*"(?=\s*:)/,
126+
relevance: 1.01
127+
},
128+
{
129+
scope: "punctuation",
130+
match: /[{}[\],:]/,
131+
relevance: 0
132+
},
133+
{
134+
scope: "literals",
135+
beginKeywords: ["true", "false" , "null"].join(" "),
136+
},
137+
hljs.QUOTE_STRING_MODE,
138+
hljs.C_NUMBER_MODE
139+
]
140+
}
141+
});
142+
143+
67144
const cheerio = require('cheerio');
68145

69146
let argv = require('yargs')

tests/md2html/fixtures/basic-new.html

+55-7
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,62 @@
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-meta">--boundary-example</span>
49+
<span class="hljs-literal">Content-Type:<span class="hljs-attr"> application/openapi+yaml</span></span>
50+
<span class="hljs-literal">Content-Location:<span class="hljs-attr"> https://inaccessible-domain.com/api/openapi.yaml</span></span>
51+
52+
openapi: 3.2.0
53+
info:
54+
title: Example API
55+
version: 1.0
56+
externalDocs:
57+
url: docs.html
58+
59+
<span class="hljs-meta">--boundary-example</span>
60+
<span class="hljs-literal">Content-Type:<span class="hljs-attr"> text/html</span></span>
61+
<span class="hljs-literal">Content-Location:<span class="hljs-attr"> https://example.com/api/docs.html</span></span>
62+
63+
&lt;html&gt;
64+
&lt;head&gt;
65+
&lt;title&gt;API Documentation&lt;/title&gt;
66+
&lt;/head&gt;
67+
&lt;body&gt;
68+
&lt;p&gt;Awesome documentation goes here&lt;/p&gt;
69+
&lt;/body&gt;
70+
&lt;/html&gt;
71+
</code></pre>
72+
<pre class="nohighlight" tabindex="0"><code><span class="hljs-attr">event</span>: addString
73+
<span class="hljs-attr">data</span>: This data is formatted
74+
<span class="hljs-attr">data</span>: across two lines
75+
<span class="hljs-attr">retry</span>: 5
76+
<span class="hljs-attr">
77+
event</span>: addNumber
78+
<span class="hljs-attr">data</span>: 1234.5678
79+
<span class="hljs-attr">unknownField</span>: this is ignored
5180
<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>
81+
</span><span class="hljs-comment">: This is a comment</span>
82+
<span class="hljs-attr">event</span>: addJSON
83+
<span class="hljs-attr">data</span>: {&quot;foo&quot;: 42}
84+
</code></pre>
85+
<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>
86+
<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>
87+
<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>
88+
</code></pre>
89+
<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>
90+
<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>
91+
<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>
92+
</code></pre>
93+
<pre class="nohighlight" tabindex="0"><code><span class="hljs-meta">0x1E</span><span class="hljs-punctuation">{</span>
94+
<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>
95+
<span class="hljs-attr">&quot;level&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span>
96+
<span class="hljs-attr">&quot;message&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;Hi!&quot;</span>
97+
<span class="hljs-punctuation">}</span>
98+
<span class="hljs-meta">0x1E</span><span class="hljs-punctuation">{</span>
99+
<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>
100+
<span class="hljs-attr">&quot;level&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span>
101+
<span class="hljs-attr">&quot;message&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;Bye!&quot;</span>
102+
<span class="hljs-punctuation">}</span>
103+
</code></pre>
56104
</section></section><section class="appendix"><h1>Appendix A: Revision History</h1>
57105
<table>
58106
<thead>

tests/md2html/fixtures/basic-new.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,70 @@ https://foo.com/bar?baz=qux&fred=waldo#fragment
6262
https://foo.com/bar{?baz*,qux}
6363
```
6464

65+
```multipart
66+
--boundary-example
67+
Content-Type: application/openapi+yaml
68+
Content-Location: https://inaccessible-domain.com/api/openapi.yaml
69+
70+
openapi: 3.2.0
71+
info:
72+
title: Example API
73+
version: 1.0
74+
externalDocs:
75+
url: docs.html
76+
77+
--boundary-example
78+
Content-Type: text/html
79+
Content-Location: https://example.com/api/docs.html
80+
81+
<html>
82+
<head>
83+
<title>API Documentation</title>
84+
</head>
85+
<body>
86+
<p>Awesome documentation goes here</p>
87+
</body>
88+
</html>
89+
```
90+
6591
```eventstream
92+
event: addString
6693
data: This data is formatted
6794
data: across two lines
6895
retry: 5
6996
70-
event: add
97+
event: addNumber
7198
data: 1234.5678
72-
unknown-field: this is ignored
99+
unknownField: this is ignored
100+
101+
: This is a comment
102+
event: addJSON
103+
data: {"foo": 42}
104+
```
105+
106+
```jsonl
107+
{"event": "addString", "data": "This data is formatted\nacross two lines", "retry": 5}
108+
{"event": "addNumber", "data": "1234.5678"}
109+
{"event": "addJSON", "data": "{\"foo\": 42}"}
110+
```
111+
112+
```ndjson
113+
{"event": "addString", "data": "This data is formatted\nacross two lines", "retry": 5}
114+
{"event": "addNumber", "data": "1234.5678"}
115+
{"event": "addJSON", "data": "{\"foo\": 42}"}
116+
```
117+
118+
```jsonseq
119+
0x1E{
120+
"timestamp": "1985-04-12T23:20:50.52Z",
121+
"level": 1,
122+
"message": "Hi!"
123+
}
124+
0x1E{
125+
"timestamp": "1985-04-12T23:20:51.37Z",
126+
"level": 1,
127+
"message": "Bye!"
128+
}
73129
```
74130

75131
## Appendix A: Revision History

0 commit comments

Comments
 (0)