Skip to content

Commit

Permalink
feat(prism): caption
Browse files Browse the repository at this point in the history
  • Loading branch information
curbengh committed Aug 14, 2020
1 parent b34456a commit 874e9a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ Option | Description | Default
`isPreprocess` | Enable preprocess or not | true
`mark` | Highlight specific line |
`firstLine` | First line number |
`caption` | Caption |

When `isPreprocess` is enabled, `prismHighlight()` will return PrismJS processed HTML snippet. Otherwise `str` will only be escaped and `prismHighlight()` will return the HTML snippet that is suitable for `prism.js` working in the Browser.

Expand Down
7 changes: 5 additions & 2 deletions lib/prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ function PrismUtil(str, options = {}) {
tab,
mark,
firstLine,
isPreprocess = true
isPreprocess = true,
caption
} = options;

// To be consistent with highlight.js
Expand Down Expand Up @@ -97,7 +98,9 @@ function PrismUtil(str, options = {}) {

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

const startTag = `<pre class="${preTagClassArr.join(' ')}"${preTagAttr}><code class="language-${language}">`;
const codeCaption = caption ? `<div>${caption}</div>` : '';

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

let parsedCode = '';
Expand Down
21 changes: 18 additions & 3 deletions test/prism.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe('prismHighlight', () => {
- using 32 bit cells causes an overflow after several millions of digits
It's about ~38 times shorter than pi16.b, ~364 times faster and works with
not-wrapping (bignum) implementations.
not-wrapping (bignum) implementations.
by Felix Nawothnig (felix.nawothnig@t-online.de) ]
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('prismHighlight', () => {
- using 32 bit cells causes an overflow after several millions of digits
It's about ~38 times shorter than pi16.b, ~364 times faster and works with
not-wrapping (bignum) implementations.
not-wrapping (bignum) implementations.
by Felix Nawothnig (felix.nawothnig@t-online.de) ]
Expand Down Expand Up @@ -292,7 +292,7 @@ describe('prismHighlight', () => {
- using 32 bit cells causes an overflow after several millions of digits
It's about ~38 times shorter than pi16.b, ~364 times faster and works with
not-wrapping (bignum) implementations.
not-wrapping (bignum) implementations.
by Felix Nawothnig (felix.nawothnig@t-online.de) ]
Expand Down Expand Up @@ -320,4 +320,19 @@ describe('prismHighlight', () => {
// Only validate the result2
validateHtmlAsync(result2, done);
});

it('caption', done => {
const input = `
{
"foo": 1,
"bar": 2
}`;
const caption = 'foo';
const result = prismHighlight(input, { caption });

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

validateHtmlAsync(result, done);
});

});

0 comments on commit 874e9a4

Please sign in to comment.