diff --git a/lib/highlight.js b/lib/highlight.js index 8923f756..f0856e03 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -30,7 +30,6 @@ function highlightUtil(str, options = {}) { const before = useHljs ? `
` : '
';
   const after = useHljs ? '
' : '
'; - const figCaption = caption ? `
${caption}
` : ''; const lines = data.value.split('\n'); let numbers = ''; @@ -43,15 +42,21 @@ function highlightUtil(str, options = {}) { content += formatLine(line, Number(firstLine) + i, mark, options, wrap); } + let codeCaption = ''; + + if (caption) { + codeCaption = wrap ? `
${caption}
` : `
${caption}
`; + } + if (!wrap) { // if original content has one trailing newline, replace it only once, else remove all trailing newlines content = /\r?\n$/.test(data.value) ? content.replace(/\n$/, '') : content.trimEnd(); - return `
${figCaption}${content}
`; + return `
${codeCaption}${content}
`; } let result = `
`; - result += figCaption; + result += codeCaption; result += ''; diff --git a/lib/prism.js b/lib/prism.js index 60423518..9bac50e1 100644 --- a/lib/prism.js +++ b/lib/prism.js @@ -98,7 +98,7 @@ function PrismUtil(str, options = {}) { if (tab) str = replaceTabs(str, tab); - const codeCaption = caption ? `
${caption}
` : ''; + const codeCaption = caption ? `
${caption}
` : ''; const startTag = `
${codeCaption}`;
   const endTag = '
'; diff --git a/test/highlight.spec.js b/test/highlight.spec.js index 019678f0..5f3f475d 100644 --- a/test/highlight.spec.js +++ b/test/highlight.spec.js @@ -194,12 +194,13 @@ describe('highlight', () => { // it('don\'t highlight if parse failed'); missing-unit-test it('caption', done => { + const caption = 'hello world'; const result = highlight(testString, { - caption: 'hello world' + caption }); result.should.eql([ - '
hello world
', + `
${caption}
`, gutter(1, 4), code(testString), end @@ -208,15 +209,16 @@ describe('highlight', () => { }); it('caption (wrap: false)', done => { + const caption = 'hello world'; const result = highlight(testString, { gutter: false, wrap: false, - caption: 'hello world' + caption }); result.should.eql([ '
',
-      '
hello world
', + `
${caption}
`, '', entities.encode(testString), '
' diff --git a/test/prism.spec.js b/test/prism.spec.js index cd26dfde..2855e888 100644 --- a/test/prism.spec.js +++ b/test/prism.spec.js @@ -330,7 +330,7 @@ describe('prismHighlight', () => { const caption = 'foo'; const result = prismHighlight(input, { caption }); - result.should.contains('
' + caption + '
'); + result.should.contains('
' + caption + '
'); validateHtmlAsync(result, done); });