File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -130,7 +130,7 @@ class DocSearch {
130130
131131 // Translate hits into smaller objects to be send to the template
132132 return groupedHits . map ( ( hit ) => {
133- let url = hit . anchor ? ` ${ hit . url } # ${ hit . anchor } ` : hit . url ;
133+ let url = DocSearch . formatURL ( hit ) ;
134134 let category = utils . getHighlightedValue ( hit , 'lvl0' ) ;
135135 let subcategory = utils . getHighlightedValue ( hit , 'lvl1' ) || category ;
136136 let displayTitle = utils . compact ( [
@@ -154,6 +154,21 @@ class DocSearch {
154154 } ) ;
155155 }
156156
157+ static formatURL ( hit ) {
158+ const { url, anchor} = hit ;
159+ if ( url ) {
160+ const containsAnchor = url . indexOf ( '#' ) !== - 1 ;
161+ if ( containsAnchor ) return url ;
162+ else if ( anchor ) return `${ hit . url } #${ hit . anchor } ` ;
163+ return url ;
164+ }
165+ else if ( anchor ) return `#${ hit . anchor } ` ;
166+ /* eslint-disable */
167+ console . warn ( 'no anchor nor url for : ' , JSON . stringify ( hit ) ) ;
168+ /* eslint-enable */
169+ return null ;
170+ }
171+
157172 static getSuggestionTemplate ( ) {
158173 const template = Hogan . compile ( templates . suggestion ) ;
159174 return ( suggestion ) => {
Original file line number Diff line number Diff line change @@ -708,6 +708,70 @@ describe('DocSearch', () => {
708708 // Then
709709 expect ( actual [ 0 ] . url ) . toEqual ( 'http://foo.bar/#anchor' ) ;
710710 } ) ;
711+ it ( 'should not add the anchor to the url if one is set but it is already in the URL' , ( ) => {
712+ // Given
713+ let input = [ {
714+ hierarchy : {
715+ lvl0 : 'Ruby' ,
716+ lvl1 : 'API' ,
717+ lvl2 : null ,
718+ lvl3 : null ,
719+ lvl4 : null ,
720+ lvl5 : null
721+ } ,
722+ content : 'foo bar' ,
723+ url : 'http://foo.bar/#anchor' ,
724+ anchor : 'anchor'
725+ } ] ;
726+
727+ // When
728+ let actual = DocSearch . formatHits ( input ) ;
729+
730+ // Then
731+ expect ( actual [ 0 ] . url ) . toEqual ( 'http://foo.bar/#anchor' ) ;
732+ } ) ;
733+ it ( 'should just use the URL if no anchor is provided' , ( ) => {
734+ // Given
735+ let input = [ {
736+ hierarchy : {
737+ lvl0 : 'Ruby' ,
738+ lvl1 : 'API' ,
739+ lvl2 : null ,
740+ lvl3 : null ,
741+ lvl4 : null ,
742+ lvl5 : null
743+ } ,
744+ content : 'foo bar' ,
745+ url : 'http://foo.bar/'
746+ } ] ;
747+
748+ // When
749+ let actual = DocSearch . formatHits ( input ) ;
750+
751+ // Then
752+ expect ( actual [ 0 ] . url ) . toEqual ( input [ 0 ] . url ) ;
753+ } ) ;
754+ it ( 'should return the anchor if there is no URL' , ( ) => {
755+ // Given
756+ let input = [ {
757+ hierarchy : {
758+ lvl0 : 'Ruby' ,
759+ lvl1 : 'API' ,
760+ lvl2 : null ,
761+ lvl3 : null ,
762+ lvl4 : null ,
763+ lvl5 : null
764+ } ,
765+ content : 'foo bar' ,
766+ anchor : 'anchor'
767+ } ] ;
768+
769+ // When
770+ let actual = DocSearch . formatHits ( input ) ;
771+
772+ // Then
773+ expect ( actual [ 0 ] . url ) . toEqual ( '#' + input [ 0 ] . anchor ) ;
774+ } ) ;
711775 } ) ;
712776
713777 describe ( 'getSuggestionTemplate' , ( ) => {
You can’t perform that action at this time.
0 commit comments