1
+ var nodemcu = nodemcu || { } ;
2
+ ( function ( ) {
3
+ 'use strict' ;
4
+
5
+ $ ( document ) . ready ( function ( ) {
6
+ fixSearch ( ) ;
7
+ } ) ;
8
+
9
+ /*
10
+ * RTD messes up MkDocs' search feature by tinkering with the search box defined in the theme, see
11
+ * https://github.com/rtfd/readthedocs.org/issues/1088. This function sets up a DOM4 MutationObserver
12
+ * to react to changes to the search form (triggered by RTD on doc ready). It then reverts everything
13
+ * the RTD JS code modified.
14
+ */
15
+ function fixSearch ( ) {
16
+ var target = document . getElementById ( 'rtd-search-form' ) ;
17
+ var config = { attributes : true , childList : true } ;
18
+
19
+ var observer = new MutationObserver ( function ( mutations ) {
20
+ // if it isn't disconnected it'll loop infinitely because the observed element is modified
21
+ observer . disconnect ( ) ;
22
+ var form = $ ( '#rtd-search-form' ) ;
23
+ form . empty ( ) ;
24
+ form . attr ( 'action' , 'https://' + window . location . hostname + '/en/' + determineSelectedBranch ( ) + '/search.html' ) ;
25
+ $ ( '<input>' ) . attr ( {
26
+ type : "text" ,
27
+ name : "q" ,
28
+ placeholder : "Search docs"
29
+ } ) . appendTo ( form ) ;
30
+ } ) ;
31
+
32
+ if ( window . location . origin . indexOf ( 'readthedocs' ) > - 1 ) {
33
+ observer . observe ( target , config ) ;
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Analyzes the URL of the current page to find out what the selected GitHub branch is. It's usually
39
+ * part of the location path. The code needs to distinguish between running MkDocs standalone
40
+ * and docs served from RTD. If no valid branch could be determined 'dev' returned.
41
+ *
42
+ * @returns GitHub branch name
43
+ */
44
+ function determineSelectedBranch ( ) {
45
+ var branch = 'dev' , path = window . location . pathname ;
46
+ if ( window . location . origin . indexOf ( 'readthedocs' ) > - 1 ) {
47
+ // path is like /en/<branch>/<lang>/build/ -> extract 'lang'
48
+ // split[0] is an '' because the path starts with the separator
49
+ var thirdPathSegment = path . split ( '/' ) [ 2 ] ;
50
+ // 'latest' is an alias on RTD for the 'dev' branch - which is the default for 'branch' here
51
+ if ( thirdPathSegment !== 'latest' ) {
52
+ branch = thirdPathSegment ;
53
+ }
54
+ }
55
+ return branch ;
56
+ }
57
+ } ( ) ) ;
0 commit comments