Skip to content

Commit 980adfd

Browse files
committed
Make consistency in output of "highlight()" for "wrap: false" (fix #111)
This patch is fix the behavior of `highlight()` with a option `wrap: false`. If `hljs: false`, the output of `highlight()` should include a HTML class name `highlight`. It is consistent with the case of `hljs:true`. And also, even if `hljs: false`, the output of `highlight()` should include a PRE element and a CODE element. It is consistent with the case of `hljs:true`, too.
1 parent b292332 commit 980adfd

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

lib/highlight.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ function highlightUtil(str, options = {}) {
2121
hljs.configure({ classPrefix: useHljs ? 'hljs-' : ''});
2222

2323
const data = highlight(str, options);
24+
const lang = options.lang || data.language || '';
25+
const classNames = (useHljs ? 'hljs' : 'highlight') + (lang ? ` ${lang}` : '');
2426

2527
if (useHljs && !gutter) wrap = false;
28+
if (gutter && !wrap) wrap = true; // arbitrate conflict ("gutter:true" takes priority over "wrap:false")
2629

27-
const before = useHljs ? `<pre><code class="hljs ${options.lang}">` : '<pre>';
30+
const before = useHljs ? `<pre><code class="${classNames}">` : '<pre>';
2831
const after = useHljs ? '</code></pre>' : '</pre>';
2932

30-
if (!wrap) return useHljs ? before + data.value + after : data.value;
33+
if (!wrap) return `<pre><code class="${classNames}">${data.value}</code></pre>`;
3134

3235
const lines = data.value.split('\n');
3336
let numbers = '';

test/highlight.spec.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,45 @@ describe('highlight', () => {
8585
validateHtmlAsync(result, done);
8686
});
8787

88-
it('wrap: false', done => {
89-
const result = highlight(testString, {wrap: false});
90-
result.should.eql(entities.encode(testString));
88+
it('wrap: false (without hljs, without lang)', done => {
89+
const result = highlight(testString, {gutter: false, wrap: false});
90+
result.should.eql([
91+
'<pre><code class="highlight plain">',
92+
entities.encode(testString),
93+
'</code></pre>'
94+
].join(''));
95+
validateHtmlAsync(result, done);
96+
});
97+
98+
it('wrap: false (with hljs, without lang)', done => {
99+
const result = highlight(testString, {gutter: false, wrap: false, hljs: true});
100+
result.should.eql([
101+
'<pre><code class="hljs plain">',
102+
entities.encode(testString),
103+
'</code></pre>'
104+
].join(''));
105+
validateHtmlAsync(result, done);
106+
});
107+
108+
it('wrap: false (without hljs, with lang)', done => {
109+
const result = highlight(testString, {gutter: false, wrap: false, lang: 'json'});
110+
hljs.configure({classPrefix: ''});
111+
result.should.eql([
112+
'<pre><code class="highlight json">',
113+
hljs.highlight('json', testString).value,
114+
'</code></pre>'
115+
].join(''));
116+
validateHtmlAsync(result, done);
117+
});
118+
119+
it('wrap: false (with hljs, with lang)', done => {
120+
const result = highlight(testString, {gutter: false, wrap: false, hljs: true, lang: 'json'});
121+
hljs.configure({classPrefix: 'hljs-'});
122+
result.should.eql([
123+
'<pre><code class="hljs json">',
124+
hljs.highlight('json', testString).value,
125+
'</code></pre>'
126+
].join(''));
91127
validateHtmlAsync(result, done);
92128
});
93129

0 commit comments

Comments
 (0)