Commit 348df1c Alexandre Stanislawski
committed
1 parent bc12b23 commit 348df1c Copy full SHA for 348df1c
File tree 2 files changed +80
-1
lines changed
2 files changed +80
-1
lines changed Original file line number Diff line number Diff line change @@ -130,7 +130,7 @@ class DocSearch {
130
130
131
131
// Translate hits into smaller objects to be send to the template
132
132
return groupedHits . map ( ( hit ) => {
133
- let url = hit . anchor ? ` ${ hit . url } # ${ hit . anchor } ` : hit . url ;
133
+ let url = DocSearch . formatURL ( hit ) ;
134
134
let category = utils . getHighlightedValue ( hit , 'lvl0' ) ;
135
135
let subcategory = utils . getHighlightedValue ( hit , 'lvl1' ) || category ;
136
136
let displayTitle = utils . compact ( [
@@ -154,6 +154,21 @@ class DocSearch {
154
154
} ) ;
155
155
}
156
156
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
+
157
172
static getSuggestionTemplate ( ) {
158
173
const template = Hogan . compile ( templates . suggestion ) ;
159
174
return ( suggestion ) => {
Original file line number Diff line number Diff line change @@ -708,6 +708,70 @@ describe('DocSearch', () => {
708
708
// Then
709
709
expect ( actual [ 0 ] . url ) . toEqual ( 'http://foo.bar/#anchor' ) ;
710
710
} ) ;
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
+ } ) ;
711
775
} ) ;
712
776
713
777
describe ( 'getSuggestionTemplate' , ( ) => {
You can’t perform that action at this time.
0 commit comments