Skip to content

Commit df0d1cf

Browse files
committed
hljs - Fixed invalid line numbering.
1 parent 707862d commit df0d1cf

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

lib/highlight.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ function highlightUtil(str, options) {
1818
var tab = options.tab;
1919

2020
hljs.configure({ classPrefix: useHljs ? 'hljs-' : ''});
21-
// If modern hljs support is enabled and there's no gutter, don't wrap the code block.
22-
if (useHljs && !gutter) wrap = false;
2321

2422
var data = highlight(str, options);
2523

26-
if (!wrap) return data.value;
24+
if (useHljs && !gutter) wrap = false;
25+
26+
var before = useHljs ? '<pre><code class="hljs ' + options.lang + '">' : '<pre>';
27+
var after = useHljs ? '</code></pre>' : '</pre>';
28+
29+
if (!wrap) return useHljs ? before + data.value + after : data.value;
2730

2831
var lines = data.value.split('\n');
2932
var numbers = '';
@@ -50,7 +53,7 @@ function highlightUtil(str, options) {
5053
result += '<td class="gutter"><pre>' + numbers + '</pre></td>';
5154
}
5255

53-
result += '<td class="code"><pre>' + content + '</pre></td>';
56+
result += '<td class="code">' + before + content + after + '</td>';
5457
result += '</tr></table></figure>';
5558

5659
return result;
@@ -130,17 +133,13 @@ function highlight(str, options) {
130133
return result;
131134
}
132135

133-
if (options.hljs) {
134-
var res = hljs.highlight(lang, str);
135-
res.value = ['<pre><code class="hljs ' + lang + '">', res.value, '</code></pre>'].join('\n');
136-
return res;
137-
}
138-
139136
if (!tryLanguage(result.language)) {
140137
result.language = 'plain';
141138
return result;
142139
}
143140

141+
if (options.hljs) return hljs.highlight(lang, str);
142+
144143
return tryHighlight(str, result.language) || result;
145144
}
146145

test/scripts/highlight.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ describe('highlight', function() {
268268
validateHtmlAsync(result, done);
269269
});
270270

271-
it('hljs compatibility - with lines', () => {
271+
it('hljs compatibility - with lines', (done) => {
272272
var str = [
273273
'function (a) {',
274274
' if (a > 3)',
@@ -281,9 +281,11 @@ describe('highlight', function() {
281281
result.should.include(codeStart);
282282
result.should.include('code class="hljs javascript"');
283283
result.should.include('class="hljs-function"');
284+
result.should.include(gutter(1, 5));
285+
validateHtmlAsync(result, done);
284286
});
285287

286-
it('hljs compatibility - no lines', () => {
288+
it('hljs compatibility - no lines', (done) => {
287289
var str = [
288290
'function (a) {',
289291
' if (a > 3)',
@@ -296,5 +298,6 @@ describe('highlight', function() {
296298
result.should.not.include(codeStart);
297299
result.should.include('code class="hljs javascript"');
298300
result.should.include('class="hljs-function"');
301+
validateHtmlAsync(result, done);
299302
});
300303
});

0 commit comments

Comments
 (0)