Skip to content

Commit

Permalink
Merge pull request #18 from Antonhansel/master
Browse files Browse the repository at this point in the history
Add option for afterSecond markup
  • Loading branch information
Neamar committed Feb 9, 2015
2 parents 9770032 + a08f709 commit 75ab47c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
6 changes: 4 additions & 2 deletions lib/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ module.exports = function highlightHtml(html, query, options) {

// Update HTML with mapping
var highlightBefore = (i === 0 ? options.before : options.beforeSecond);
html = before + highlightBefore + content + options.after + after;
globalIndexDelta += highlightBefore.length + options.after.length;
var highlightAfter = (i === 0 ? options.after : options.afterSecond);
html = before + highlightBefore + content + highlightAfter + after;
globalIndexDelta += highlightBefore.length + highlightAfter.length;

}
});

Expand Down
2 changes: 2 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = function(options) {

options.beforeSecond = options.beforeSecond || options.before || module.exports.defaultOptions.beforeSecond || module.exports.defaultOptions.before;
options.before = options.before || module.exports.defaultOptions.before;
options.afterSecond = options.afterSecond || options.after || module.exports.defaultOptions.afterSecond || module.exports.defaultOptions.after;
options.after = options.after || module.exports.defaultOptions.after;
options.language = options.language ? options.language.replace(/[^a-z]/g, '') : module.exports.defaultOptions.language;

Expand All @@ -21,5 +22,6 @@ module.exports.defaultOptions = {
before: '<strong>',
after: '</strong>',
beforeSecond: '<strong class="secondary">',
afterSecond: '</strong>',
language: 'en'
};
33 changes: 22 additions & 11 deletions test/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ describe('Standard mode', function() {
var expected = {
text: '<strong>Farewell</strong> and welcome to the real <strong>world</strong>.',
indices: [
{
startIndex: 0,
endIndex: 8,
content: 'Farewell'
},
{
startIndex: 33,
endIndex: 38,
content: 'world'
}
{
startIndex: 0,
endIndex: 8,
content: 'Farewell'
},
{
startIndex: 33,
endIndex: 38,
content: 'world'
}
]
};

Expand Down Expand Up @@ -259,6 +259,17 @@ describe('Standard mode', function() {
},
expected: '<strong>Hello and welcome to the real <span>world</span></strong><span class=secondary> Neo</span>.',
},
'should use before second and after second highlight': {
text: '<i>Hello and welcome to the real world</i> Neo.',
query: 'world Neo',
expected: '<i>Hello and welcome to the real <strong>world</strong></i><span class=secondary> Neo</span>.',
options: {
before: '<strong>',
after: '</strong>',
beforeSecond: '<span class=secondary>',
afterSecond: '</span>',
}
},
'should skip markup with non-textual content': {
text: '<style>abbr { font-size:2em; }</style> <p>This font</p>',
query: 'font',
Expand All @@ -278,7 +289,7 @@ describe('Standard mode', function() {
text: 'I just sent you a meeting invitation.<div><br></div><div>Mark</div></div>',
query: 'Mark',
expected: 'I just sent you a meeting invitation.<div><br></div><div>*Mark*</div></div>',
}
},
};
generateIts(its, generateHtmlIt);

Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Highlight options', function() {
});

it('should allow override of beforeSecond', function() {
documentHighlight.html("my <strong>text is awesome</strong>", "my text", {before: '<span>', beforeSecond:'<span class=sec>', after: '</span>'}).html.should.eql("<span>my </span><strong><span class=sec>text</span> is awesome</strong>");
documentHighlight.html("my <strong>text is awesome</strong>", "my text", {before: '<span>', beforeSecond: '<span class=sec>', after: '</span>'}).html.should.eql("<span>my </span><strong><span class=sec>text</span> is awesome</strong>");
});

it('should allow global override of before and after', function() {
Expand Down

0 comments on commit 75ab47c

Please sign in to comment.