Skip to content

Commit df5bc7a

Browse files
authored
enh(http) support headers without preamble (highlightjs#3051)
1 parent 87019cb commit df5bc7a

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

src/languages/http.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@ import * as regex from '../lib/regex.js';
1111
export default function(hljs) {
1212
const VERSION = 'HTTP/(2|1\\.[01])';
1313
const HEADER_NAME = /[A-Za-z][A-Za-z0-9-]*/;
14-
const HEADERS_AND_BODY = [
15-
{
16-
className: 'attribute',
17-
begin: regex.concat('^', HEADER_NAME, '(?=\\:\\s)'),
18-
starts: {
19-
contains: [
20-
{
21-
className: "punctuation",
22-
begin: /: /,
23-
relevance: 0,
24-
starts: {
25-
end: '$',
26-
relevance: 0
27-
}
14+
const HEADER = {
15+
className: 'attribute',
16+
begin: regex.concat('^', HEADER_NAME, '(?=\\:\\s)'),
17+
starts: {
18+
contains: [
19+
{
20+
className: "punctuation",
21+
begin: /: /,
22+
relevance: 0,
23+
starts: {
24+
end: '$',
25+
relevance: 0
2826
}
29-
]
30-
}
31-
},
27+
}
28+
]
29+
}
30+
};
31+
const HEADERS_AND_BODY = [
32+
HEADER,
3233
{
3334
begin: '\\n\\n',
3435
starts: { subLanguage: [], endsWithParent: true }
@@ -85,7 +86,11 @@ export default function(hljs) {
8586
illegal: /\S/,
8687
contains: HEADERS_AND_BODY
8788
}
88-
}
89+
},
90+
// to allow headers to work even without a preamble
91+
hljs.inherit(HEADER, {
92+
relevance: 0
93+
})
8994
]
9095
};
9196
}

test/markup/cpp/function-declarations.expect.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@
2121

2222
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">A</span><span class="hljs-params">()</span>: a(<span class="hljs-number">10</span>) {</span>}
2323
<span class="hljs-function"><span class="hljs-keyword">explicit</span> <span class="hljs-title">A</span><span class="hljs-params">()</span>: a(<span class="hljs-number">10</span>) {</span>}
24-
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<span class="hljs-attribute">Host</span><span class="hljs-punctuation">: </span>example.org
2+
<span class="hljs-attribute">Content-Type</span><span class="hljs-punctuation">: </span>application/json; charset=utf-8
3+
<span class="hljs-attribute">Content-Length</span><span class="hljs-punctuation">: </span>19

test/markup/http/just_headers.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Host: example.org
2+
Content-Type: application/json; charset=utf-8
3+
Content-Length: 19

0 commit comments

Comments
 (0)