Skip to content

Commit

Permalink
Merge pull request #225 from curbengh/wrap-mark
Browse files Browse the repository at this point in the history
feat(highlight): support 'tab' & 'mark' when wrap is disabled
  • Loading branch information
curbengh committed Jul 31, 2020
2 parents dbca653 + 06dbc0b commit fe64888
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
17 changes: 11 additions & 6 deletions lib/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ function highlightUtil(str, options = {}) {

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

if (!wrap) return `<pre>${figCaption}<code class="${classNames}">${data.value}</code></pre>`;

const lines = data.value.split('\n');
let numbers = '';
let content = '';
Expand All @@ -42,7 +40,13 @@ function highlightUtil(str, options = {}) {
let line = lines[i];
if (tab) line = replaceTabs(line, tab);
numbers += `<span class="line">${Number(firstLine) + i}</span><br>`;
content += formatLine(line, Number(firstLine) + i, mark, options);
content += formatLine(line, Number(firstLine) + i, mark, options, wrap);
}

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>`;
}

let result = `<figure class="highlight${data.language ? ` ${data.language}` : ''}">`;
Expand All @@ -61,8 +65,9 @@ function highlightUtil(str, options = {}) {
return result;
}

function formatLine(line, lineno, marked, options) {
const useHljs = options.hljs || false;
function formatLine(line, lineno, marked, options, wrap) {
const useHljs = (options.hljs || false) || !wrap;
const br = wrap ? '<br>' : '\n';
let res = useHljs ? '' : '<span class="line';
if (marked.includes(lineno)) {
// Handle marked lines.
Expand All @@ -71,7 +76,7 @@ function formatLine(line, lineno, marked, options) {
res += useHljs ? line : `">${line}</span>`;
}

res += '<br>';
res += br;
return res;
}

Expand Down
40 changes: 38 additions & 2 deletions test/highlight.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@ describe('highlight', () => {
validateHtmlAsync(result, done);
});

it('wrap: false (with mark)', done => {
const result = highlight(testString, {gutter: false, wrap: false, hljs: true, lang: 'json', mark: '1'});
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'<pre><code class="hljs json">',
hljs.highlight('json', testString).value.replace('{', '<mark>{</mark>'),
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
});

it('wrap: false (retain trailing newline)', done => {
const result = highlight(testString + '\n', {gutter: false, wrap: false, hljs: true, lang: 'json'});
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'<pre><code class="hljs json">',
hljs.highlight('json', testString).value,
'\n</code></pre>'
].join(''));
validateHtmlAsync(result, done);
});

it('firstLine', done => {
const result = highlight(testString, {firstLine: 3});
assertResult(result, gutter(3, 6), code(testString));
Expand Down Expand Up @@ -203,24 +225,38 @@ describe('highlight', () => {
});

it('tab', done => {
const spaces = ' ';
const str = [
'function fib(i){',
'\tif (i <= 1) return i;',
'\treturn fib(i - 1) + fib(i - 2);',
'}'
].join('\n');

const result = highlight(str, {tab: ' ', lang: 'js'});
const result = highlight(str, {tab: spaces, lang: 'js'});

result.should.eql([
'<figure class="highlight js"><table><tr>',
gutter(1, 4),
code(str.replace(/\t/g, ' '), 'js'),
code(str.replace(/\t/g, spaces), 'js'),
end
].join(''));
validateHtmlAsync(result, done);
});

it('tab with wrap:false', done => {
const spaces = ' ';
const result = highlight('\t' + testString, {gutter: false, wrap: false, hljs: true, lang: 'json', tab: spaces});
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'<pre><code class="hljs json">',
spaces,
hljs.highlight('json', testString).value,
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
});

it('escape html entity', done => {
const str = [
'deploy:',
Expand Down

0 comments on commit fe64888

Please sign in to comment.