Skip to content

Commit

Permalink
Merge pull request algolia/algoliasearch-helper-js#117 from algolia/f…
Browse files Browse the repository at this point in the history
…ix/113

FIX algolia/algoliasearch-helper-js#113 : let the user change the tags for highlighting
  • Loading branch information
bobylito committed Jun 23, 2015
2 parents dccb449 + f4b204c commit 8b7bc5c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/algoliasearch-helper/src/SearchParameters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ var SearchParameters = function( newParameters ) {
* @member {string}
*/
this.attributesToHighlight = params.attributesToHighlight;
/**
* @see https://www.algolia.com/doc#highlightPreTag
* @member {string}
*/
this.highlightPreTag = params.highlightPreTag;
/**
* @see https://www.algolia.com/doc#highlightPostTag
* @member {string}
*/
this.highlightPostTag = params.highlightPostTag;
/**
* @see https://www.algolia.com/doc#attributesToSnippet
* @member {string}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";

var test = require( "tape" );
var algoliasearchHelper = require( "../../index" );
var setup = require( "../integration-utils.js" ).setup;

test( "[INT][HIGHLIGHT] The highlight should be consistent with the parameters", function( t ) {
var indexName = "helper_highlight";

setup( indexName, function( client, index ) {
return index.addObjects( [
{ facet : [ "f1", "f2" ] },
{ facet : [ "f1", "f3" ] },
{ facet : [ "f2", "f3" ] }
] )
.then( function() {
return index.setSettings( {
attributesToIndex : [ "facet" ],
attributesForFaceting : [ "facet" ]
} );
} )
.then( function( content ) {
return index.waitTask( content.taskID );
} ).then( function() {
return client;
} );
} ).then( function( client ) {
var helper = algoliasearchHelper( client, indexName, {
attributesToHighlight : ["facet"],
facets : [ "facet" ]
} );

var calls = 0;
helper.on( "result", function( content ) {
calls++;
if( calls === 1 ) {
t.equal( content.hits[0]._highlightResult.facet[0].value,
"<em>f1</em>",
"should be hightlighted with em (default)" );
t.equal( content.hits[1]._highlightResult.facet[0].value,
"<em>f1</em>",
"should be hightlighted with em (default)" );
}
else if( calls === 2 ) {
t.equal( content.hits[0]._highlightResult.facet[0].value,
"<strong>f1</strong>",
"should be hightlighted with strong (setting)" );
t.equal( content.hits[1]._highlightResult.facet[0].value,
"<strong>f1</strong>",
"should be hightlighted with strong (setting)" );
t.end();
}
} );

helper.setQuery( "f1" )
.search()
.setQueryParameter( "highlightPostTag", "</strong>" )
.setQueryParameter( "highlightPreTag", "<strong>" )
.search();
} );
} );

0 comments on commit 8b7bc5c

Please sign in to comment.