@@ -22,9 +22,17 @@ const opengraph = `\
22
22
<meta name="twitter:card" content="summary" />\
23
23
<meta name="twitter:title" content="CXX — safe interop between Rust and C++" />` ;
24
24
25
- const htmljs = `\
26
- var html = document.querySelector('html');
27
- html.classList.remove('no-js');
25
+ const themejs = `\
26
+ var theme;
27
+ try { theme = localStorage.getItem('mdbook-theme'); } catch(e) {}
28
+ if (theme === null || theme === undefined) { theme = default_theme; }
29
+ const html = document.documentElement;
30
+ html.classList.remove('light')
31
+ html.classList.add(theme);
32
+ html.classList.add("js");` ;
33
+
34
+ const themejsReplacement = `\
35
+ const html = document.documentElement;
28
36
html.classList.add('js');` ;
29
37
30
38
const dirs = [ 'build' ] ;
@@ -46,7 +54,6 @@ while (dirs.length) {
46
54
const $ = cheerio . load ( index , { decodeEntities : false } ) ;
47
55
48
56
$ ( 'head' ) . append ( opengraph ) ;
49
- $ ( 'script:nth-of-type(3)' ) . text ( htmljs ) ;
50
57
$ ( 'nav#sidebar ol.chapter' ) . append ( githublink ) ;
51
58
$ ( 'head link[href="tomorrow-night.css"]' ) . attr ( 'disabled' , true ) ;
52
59
$ ( 'head link[href="ayu-highlight.css"]' ) . attr ( 'disabled' , true ) ;
@@ -90,6 +97,19 @@ while (dirs.length) {
90
97
$ ( this ) . addClass ( 'hljs' ) ;
91
98
} ) ;
92
99
100
+ var foundScript = false ;
101
+ $ ( 'body script' ) . each ( function ( ) {
102
+ const node = $ ( this ) ;
103
+ if ( node . text ( ) . replace ( / \s / g, '' ) === themejs . replace ( / \s / g, '' ) ) {
104
+ node . text ( themejsReplacement ) ;
105
+ foundScript = true ;
106
+ }
107
+ } ) ;
108
+ const pathsWithoutScript = [ 'build/toc.html' , 'build/build/index.html' , 'build/binding/index.html' ] ;
109
+ if ( ! foundScript && ! pathsWithoutScript . includes ( path ) ) {
110
+ throw new Error ( 'theme script not found' ) ;
111
+ }
112
+
93
113
const out = $ . html ( ) ;
94
114
fs . writeFileSync ( path , out ) ;
95
115
} ) ;
0 commit comments