Skip to content

Commit 06dbc0b

Browse files
committed
feat(highlight): support 'tab' & 'mark' when wrap is disabled
1 parent f4abd47 commit 06dbc0b

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

lib/highlight.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ function highlightUtil(str, options = {}) {
3232

3333
const figCaption = caption ? `<figcaption>${caption}</figcaption>` : '';
3434

35-
if (!wrap) return `<pre>${figCaption}<code class="${classNames}">${data.value}</code></pre>`;
36-
3735
const lines = data.value.split('\n');
3836
let numbers = '';
3937
let content = '';
@@ -42,7 +40,13 @@ function highlightUtil(str, options = {}) {
4240
let line = lines[i];
4341
if (tab) line = replaceTabs(line, tab);
4442
numbers += `<span class="line">${Number(firstLine) + i}</span><br>`;
45-
content += formatLine(line, Number(firstLine) + i, mark, options);
43+
content += formatLine(line, Number(firstLine) + i, mark, options, wrap);
44+
}
45+
46+
if (!wrap) {
47+
// if original content has one trailing newline, replace it only once, else remove all trailing newlines
48+
content = /\r?\n$/.test(data.value) ? content.replace(/\n$/, '') : content.trimEnd();
49+
return `<pre>${figCaption}<code class="${classNames}">${content}</code></pre>`;
4650
}
4751

4852
let result = `<figure class="highlight${data.language ? ` ${data.language}` : ''}">`;
@@ -61,8 +65,9 @@ function highlightUtil(str, options = {}) {
6165
return result;
6266
}
6367

64-
function formatLine(line, lineno, marked, options) {
65-
const useHljs = options.hljs || false;
68+
function formatLine(line, lineno, marked, options, wrap) {
69+
const useHljs = (options.hljs || false) || !wrap;
70+
const br = wrap ? '<br>' : '\n';
6671
let res = useHljs ? '' : '<span class="line';
6772
if (marked.includes(lineno)) {
6873
// Handle marked lines.
@@ -71,7 +76,7 @@ function formatLine(line, lineno, marked, options) {
7176
res += useHljs ? line : `">${line}</span>`;
7277
}
7378

74-
res += '<br>';
79+
res += br;
7580
return res;
7681
}
7782

test/highlight.spec.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,28 @@ describe('highlight', () => {
133133
validateHtmlAsync(result, done);
134134
});
135135

136+
it('wrap: false (with mark)', done => {
137+
const result = highlight(testString, {gutter: false, wrap: false, hljs: true, lang: 'json', mark: '1'});
138+
hljs.configure({classPrefix: 'hljs-'});
139+
result.should.eql([
140+
'<pre><code class="hljs json">',
141+
hljs.highlight('json', testString).value.replace('{', '<mark>{</mark>'),
142+
'</code></pre>'
143+
].join(''));
144+
validateHtmlAsync(result, done);
145+
});
146+
147+
it('wrap: false (retain trailing newline)', done => {
148+
const result = highlight(testString + '\n', {gutter: false, wrap: false, hljs: true, lang: 'json'});
149+
hljs.configure({classPrefix: 'hljs-'});
150+
result.should.eql([
151+
'<pre><code class="hljs json">',
152+
hljs.highlight('json', testString).value,
153+
'\n</code></pre>'
154+
].join(''));
155+
validateHtmlAsync(result, done);
156+
});
157+
136158
it('firstLine', done => {
137159
const result = highlight(testString, {firstLine: 3});
138160
assertResult(result, gutter(3, 6), code(testString));
@@ -203,24 +225,38 @@ describe('highlight', () => {
203225
});
204226

205227
it('tab', done => {
228+
const spaces = ' ';
206229
const str = [
207230
'function fib(i){',
208231
'\tif (i <= 1) return i;',
209232
'\treturn fib(i - 1) + fib(i - 2);',
210233
'}'
211234
].join('\n');
212235

213-
const result = highlight(str, {tab: ' ', lang: 'js'});
236+
const result = highlight(str, {tab: spaces, lang: 'js'});
214237

215238
result.should.eql([
216239
'<figure class="highlight js"><table><tr>',
217240
gutter(1, 4),
218-
code(str.replace(/\t/g, ' '), 'js'),
241+
code(str.replace(/\t/g, spaces), 'js'),
219242
end
220243
].join(''));
221244
validateHtmlAsync(result, done);
222245
});
223246

247+
it('tab with wrap:false', done => {
248+
const spaces = ' ';
249+
const result = highlight('\t' + testString, {gutter: false, wrap: false, hljs: true, lang: 'json', tab: spaces});
250+
hljs.configure({classPrefix: 'hljs-'});
251+
result.should.eql([
252+
'<pre><code class="hljs json">',
253+
spaces,
254+
hljs.highlight('json', testString).value,
255+
'</code></pre>'
256+
].join(''));
257+
validateHtmlAsync(result, done);
258+
});
259+
224260
it('escape html entity', done => {
225261
const str = [
226262
'deploy:',

0 commit comments

Comments
 (0)