Skip to content

Commit a96335d

Browse files
authored
Merge pull request #229 from curbengh/wrap-caption
fix(highlight): use <div> when wrap is disabled
2 parents bf42b66 + 23372e0 commit a96335d

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

lib/highlight.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ function highlightUtil(str, options = {}) {
3030
const before = useHljs ? `<pre><code class="${classNames}">` : '<pre>';
3131
const after = useHljs ? '</code></pre>' : '</pre>';
3232

33-
const figCaption = caption ? `<figcaption>${caption}</figcaption>` : '';
3433

3534
const lines = data.value.split('\n');
3635
let numbers = '';
@@ -43,15 +42,21 @@ function highlightUtil(str, options = {}) {
4342
content += formatLine(line, Number(firstLine) + i, mark, options, wrap);
4443
}
4544

45+
let codeCaption = '';
46+
47+
if (caption) {
48+
codeCaption = wrap ? `<figcaption>${caption}</figcaption>` : `<div class="caption">${caption}</div>`;
49+
}
50+
4651
if (!wrap) {
4752
// if original content has one trailing newline, replace it only once, else remove all trailing newlines
4853
content = /\r?\n$/.test(data.value) ? content.replace(/\n$/, '') : content.trimEnd();
49-
return `<pre>${figCaption}<code class="${classNames}">${content}</code></pre>`;
54+
return `<pre>${codeCaption}<code class="${classNames}">${content}</code></pre>`;
5055
}
5156

5257
let result = `<figure class="highlight${data.language ? ` ${data.language}` : ''}">`;
5358

54-
result += figCaption;
59+
result += codeCaption;
5560

5661
result += '<table><tr>';
5762

lib/prism.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function PrismUtil(str, options = {}) {
9898

9999
if (tab) str = replaceTabs(str, tab);
100100

101-
const codeCaption = caption ? `<div>${caption}</div>` : '';
101+
const codeCaption = caption ? `<div class="caption">${caption}</div>` : '';
102102

103103
const startTag = `<pre class="${preTagClassArr.join(' ')}"${preTagAttr}>${codeCaption}<code class="language-${language}">`;
104104
const endTag = '</code></pre>';

test/highlight.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,13 @@ describe('highlight', () => {
194194
// it('don\'t highlight if parse failed'); missing-unit-test
195195

196196
it('caption', done => {
197+
const caption = 'hello world';
197198
const result = highlight(testString, {
198-
caption: 'hello world'
199+
caption
199200
});
200201

201202
result.should.eql([
202-
'<figure class="highlight plain"><figcaption>hello world</figcaption><table><tr>',
203+
`<figure class="highlight plain"><figcaption>${caption}</figcaption><table><tr>`,
203204
gutter(1, 4),
204205
code(testString),
205206
end
@@ -208,15 +209,16 @@ describe('highlight', () => {
208209
});
209210

210211
it('caption (wrap: false)', done => {
212+
const caption = 'hello world';
211213
const result = highlight(testString, {
212214
gutter: false,
213215
wrap: false,
214-
caption: 'hello world'
216+
caption
215217
});
216218

217219
result.should.eql([
218220
'<pre>',
219-
'<figcaption>hello world</figcaption>',
221+
`<div class="caption">${caption}</div>`,
220222
'<code class="highlight plain">',
221223
entities.encode(testString),
222224
'</code></pre>'

test/prism.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ describe('prismHighlight', () => {
330330
const caption = 'foo';
331331
const result = prismHighlight(input, { caption });
332332

333-
result.should.contains('<div>' + caption + '</div>');
333+
result.should.contains('<div class="caption">' + caption + '</div>');
334334

335335
validateHtmlAsync(result, done);
336336
});

0 commit comments

Comments
 (0)