Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(highlight): use <div> when wrap is disabled #229

Merged
merged 3 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function highlightUtil(str, options = {}) {
const before = useHljs ? `<pre><code class="${classNames}">` : '<pre>';
const after = useHljs ? '</code></pre>' : '</pre>';

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

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

let codeCaption = '';

if (caption) {
codeCaption = wrap ? `<figcaption>${caption}</figcaption>` : `<div class="caption">${caption}</div>`;
}

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 `<pre>${figCaption}<code class="${classNames}">${content}</code></pre>`;
return `<pre>${codeCaption}<code class="${classNames}">${content}</code></pre>`;
}

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

result += figCaption;
result += codeCaption;

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

Expand Down
2 changes: 1 addition & 1 deletion lib/prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function PrismUtil(str, options = {}) {

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

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

const startTag = `<pre class="${preTagClassArr.join(' ')}"${preTagAttr}>${codeCaption}<code class="language-${language}">`;
const endTag = '</code></pre>';
Expand Down
10 changes: 6 additions & 4 deletions test/highlight.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
'<figure class="highlight plain"><figcaption>hello world</figcaption><table><tr>',
`<figure class="highlight plain"><figcaption>${caption}</figcaption><table><tr>`,
gutter(1, 4),
code(testString),
end
Expand All @@ -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([
'<pre>',
'<figcaption>hello world</figcaption>',
`<div class="caption">${caption}</div>`,
'<code class="highlight plain">',
entities.encode(testString),
'</code></pre>'
Expand Down
2 changes: 1 addition & 1 deletion test/prism.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ describe('prismHighlight', () => {
const caption = 'foo';
const result = prismHighlight(input, { caption });

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

validateHtmlAsync(result, done);
});
Expand Down