From 3c3ffaa267257895009180da440805e50090c578 Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Sat, 15 Aug 2020 12:14:33 +0000 Subject: [PATCH 1/3] fix(highlight): use
when wrap is disabled - BREAKING CHANGE:
is replaced with
when wrap is disabled - https://github.com/hexojs/hexo-util/pull/227#issuecomment-673347477 --- lib/highlight.js | 12 +++++++++--- test/highlight.spec.js | 10 ++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/highlight.js b/lib/highlight.js index 8923f756..3070367c 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,22 @@ function highlightUtil(str, options = {}) { content += formatLine(line, Number(firstLine) + i, mark, options, wrap); } + let codeCaption = ''; + + if (caption) { + if (wrap) codeCaption = `
${caption}
`; + else codeCaption = `
${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/test/highlight.spec.js b/test/highlight.spec.js index 019678f0..a5279168 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), '
' From 1daa862f5e6b3c345986d2cd84e52dd65eb1395b Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Sun, 16 Aug 2020 01:03:45 +0000 Subject: [PATCH 2/3] refactor(highlight): condition shorthand --- lib/highlight.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/highlight.js b/lib/highlight.js index 3070367c..b794ae2f 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -45,8 +45,7 @@ function highlightUtil(str, options = {}) { let codeCaption = ''; if (caption) { - if (wrap) codeCaption = `
${caption}
`; - else codeCaption = `
${caption}
`; + codeCaption = wrap ? `
${caption}
` : `
${caption}
`; } if (!wrap) { From 23372e0f1169597d8bf41fd52500014e0545447e Mon Sep 17 00:00:00 2001 From: MDLeom <43627182+curbengh@users.noreply.github.com> Date: Mon, 17 Aug 2020 08:37:24 +0000 Subject: [PATCH 3/3] fix(highlight&prism): add 'caption' class --- lib/highlight.js | 2 +- lib/prism.js | 2 +- test/highlight.spec.js | 2 +- test/prism.spec.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/highlight.js b/lib/highlight.js index b794ae2f..f0856e03 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -45,7 +45,7 @@ function highlightUtil(str, options = {}) { let codeCaption = ''; if (caption) { - codeCaption = wrap ? `
${caption}
` : `
${caption}
`; + codeCaption = wrap ? `
${caption}
` : `
${caption}
`; } if (!wrap) { 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 a5279168..5f3f475d 100644 --- a/test/highlight.spec.js +++ b/test/highlight.spec.js @@ -218,7 +218,7 @@ describe('highlight', () => { result.should.eql([ '
',
-      `
${caption}
`, + `
${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); });