Skip to content

Commit a132734

Browse files
committed
Additional Theme
add tests for additional-themes Update book.js sanitized default sanitize the id name in the DOM catch error if attempting to select or deselect a non-existent theme. sanitize theme names Update book.js Update book.js Update book.js Revert "Update book.js" This reverts commit 0ed9477. Update book.js Update config.rs Revert "Update hbs_renderer.rs" This reverts commit 54ad1d5. Revert "Update config.rs" This reverts commit 06909a7. Revert "Update hbs_renderer.rs" This reverts commit dc0f959. Update hbs_renderer.rs Update hbs_renderer.rs Update config.rs Update book.js Additional Theme
1 parent 660cbfa commit a132734

File tree

6 files changed

+89
-11
lines changed

6 files changed

+89
-11
lines changed

src/config.rs

+7
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ pub struct HtmlConfig {
536536
pub google_analytics: Option<String>,
537537
/// Additional CSS stylesheets to include in the rendered page's `<head>`.
538538
pub additional_css: Vec<PathBuf>,
539+
/// Additional theme.
540+
pub additional_theme: Vec<PathBuf>,
539541
/// Additional JS scripts to include at the bottom of the rendered page's
540542
/// `<body>`.
541543
pub additional_js: Vec<PathBuf>,
@@ -595,6 +597,7 @@ impl Default for HtmlConfig {
595597
copy_fonts: true,
596598
google_analytics: None,
597599
additional_css: Vec::new(),
600+
additional_theme: Vec::new(),
598601
additional_js: Vec::new(),
599602
fold: Fold::default(),
600603
playground: Playground::default(),
@@ -801,6 +804,7 @@ mod tests {
801804
curly-quotes = true
802805
google-analytics = "123456"
803806
additional-css = ["./foo/bar/baz.css"]
807+
additional-theme = ["foobar"]
804808
git-repository-url = "https://foo.com/"
805809
git-repository-icon = "fa-code-fork"
806810
@@ -848,6 +852,7 @@ mod tests {
848852
curly_quotes: true,
849853
google_analytics: Some(String::from("123456")),
850854
additional_css: vec![PathBuf::from("./foo/bar/baz.css")],
855+
additional_theme: vec![PathBuf::from("foobar")],
851856
theme: Some(PathBuf::from("./themedir")),
852857
default_theme: Some(String::from("rust")),
853858
playground: playground_should_be,
@@ -1028,6 +1033,7 @@ mod tests {
10281033
curly-quotes = true
10291034
google-analytics = "123456"
10301035
additional-css = ["custom.css", "custom2.css"]
1036+
additional-theme = ["barfoo"]
10311037
additional-js = ["custom.js"]
10321038
"#;
10331039

@@ -1053,6 +1059,7 @@ mod tests {
10531059
curly_quotes: true,
10541060
google_analytics: Some(String::from("123456")),
10551061
additional_css: vec![PathBuf::from("custom.css"), PathBuf::from("custom2.css")],
1062+
additional_theme: vec![PathBuf::from("barfoo")],
10561063
additional_js: vec![PathBuf::from("custom.js")],
10571064
..Default::default()
10581065
};

src/renderer/html_handlebars/hbs_renderer.rs

+12
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,18 @@ fn make_data(
715715
data.insert("additional_css".to_owned(), json!(css));
716716
}
717717

718+
// Add check to see if there are additional themes
719+
if !html_config.additional_theme.is_empty() {
720+
let mut theme = Vec::new();
721+
for name in &html_config.additional_theme {
722+
match name.strip_prefix(root) {
723+
Ok(p) => theme.push(p.to_str().expect("Could not convert to str")),
724+
Err(_) => theme.push(name.to_str().expect("Could not convert to str")),
725+
}
726+
}
727+
data.insert("additional_theme".to_owned(), json!(theme));
728+
}
729+
718730
// Add check to see if there is an additional script
719731
if !html_config.additional_js.is_empty() {
720732
let mut js = Vec::new();

src/theme/book.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ function playground_text(playground, hidden = true) {
305305
themePopup.querySelectorAll('.theme-selected').forEach(function (el) {
306306
el.classList.remove('theme-selected');
307307
});
308-
themePopup.querySelector("button#" + get_theme()).classList.add('theme-selected');
308+
try {
309+
themePopup.querySelector("button#" + get_theme()).classList.add('theme-selected');
310+
} catch (e) { }
309311
}
310312

311313
function hideThemes() {
@@ -318,9 +320,9 @@ function playground_text(playground, hidden = true) {
318320
var theme;
319321
try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
320322
if (theme === null || theme === undefined) {
321-
return default_theme;
323+
return default_theme.replace(/\W+/g, '_').toLowerCase();
322324
} else {
323-
return theme;
325+
return theme.replace(/\W+/g, '_').toLowerCase();
324326
}
325327
}
326328

@@ -331,13 +333,17 @@ function playground_text(playground, hidden = true) {
331333
stylesheets.ayuHighlight.disabled = true;
332334
stylesheets.tomorrowNight.disabled = false;
333335
stylesheets.highlight.disabled = true;
334-
335336
ace_theme = "ace/theme/tomorrow_night";
336337
} else if (theme == 'ayu') {
337338
stylesheets.ayuHighlight.disabled = false;
338339
stylesheets.tomorrowNight.disabled = true;
339340
stylesheets.highlight.disabled = true;
340341
ace_theme = "ace/theme/tomorrow_night";
342+
} else if (theme == 'rust' || theme == 'light') {
343+
stylesheets.ayuHighlight.disabled = true;
344+
stylesheets.tomorrowNight.disabled = true;
345+
stylesheets.highlight.disabled = false;
346+
ace_theme = "ace/theme/dawn";
341347
} else {
342348
stylesheets.ayuHighlight.disabled = true;
343349
stylesheets.tomorrowNight.disabled = true;
@@ -355,17 +361,23 @@ function playground_text(playground, hidden = true) {
355361
});
356362
}
357363

358-
var previousTheme = get_theme();
359-
364+
var previousTheme = get_theme().replace(/\W+/g, '_').toLowerCase();
365+
var selectedTheme = theme.replace(/\W+/g, '_').toLowerCase();
360366
if (store) {
361-
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
367+
try { localStorage.setItem('mdbook-theme', selectedTheme); } catch (e) { }
362368
}
363369

364-
html.classList.remove(previousTheme);
365-
html.classList.add(theme);
370+
try {
371+
html.classList.remove( previousTheme );
372+
html.classList.add( selectedTheme );
373+
} catch (e) { }
374+
366375
updateThemeSelected();
367376
}
368377

378+
// Sanitize theme id names
379+
themePopup.querySelectorAll("button").forEach(e=>{e.id=e.id.replace(/\W+/g, '_').toLowerCase();});
380+
369381
// Set theme
370382
var theme = get_theme();
371383

src/theme/index.hbs

+8-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<script>
6060
var path_to_root = "{{ path_to_root }}";
6161
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "{{ preferred_dark_theme }}" : "{{ default_theme }}";
62+
default_theme = default_theme.replace(/\W+/g, '_').toLowerCase();
6263
</script>
6364

6465
<!-- Work around some values being stored in localStorage wrapped in quotes -->
@@ -83,8 +84,10 @@
8384
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
8485
if (theme === null || theme === undefined) { theme = default_theme; }
8586
var html = document.querySelector('html');
86-
html.classList.remove('{{ default_theme }}')
87-
html.classList.add(theme);
87+
try {
88+
html.classList.remove('{{ default_theme }}');
89+
html.classList.add(theme.replace(/\W+/g, '_').toLowerCase());
90+
} catch(e) { }
8891
var body = document.querySelector('body');
8992
body.classList.remove('no-js')
9093
body.classList.add('js');
@@ -156,6 +159,9 @@
156159
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
157160
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
158161
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
162+
{{#each additional_theme}}
163+
<li role="none"><button role="menuitem" class="theme" id="{{ this }}">{{ this }}</button></li>
164+
{{/each}}
159165
</ul>
160166
{{#if search_enabled}}
161167
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">

test_book/book.toml

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ edition = "2018"
99

1010
[output.html]
1111
mathjax-support = true
12+
additional-css = ["orange.css"]
13+
additional-theme = ["Orange"]
1214

1315
[output.html.playground]
1416
editable = true

test_book/orange.css

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.orange {
2+
--bg: #f6f6ef;
3+
--fg: Black;
4+
5+
--sidebar-bg: #ff6600;
6+
--sidebar-fg: Black;
7+
--sidebar-non-existant: color-mix(in srgb, var(--sidebar-bg) 75%, Black);
8+
--sidebar-active: White;
9+
--sidebar-spacer: color-mix(in srgb, var(--sidebar-bg) 95%, Black);
10+
11+
--scrollbar: #8F8F8F;
12+
13+
--icons: #747474;
14+
--icons-hover: #000000;
15+
16+
--links: #828282;
17+
18+
--inline-code-color: Black
19+
20+
--theme-popup-bg: #fafafa;
21+
--theme-popup-border: #cccccc;
22+
--theme-hover: #e6e6e6;
23+
24+
--quote-bg: #e9e9ed;
25+
--quote-border: #8f8f9d;
26+
27+
--table-border-color: hsl(0, 0%, 95%);
28+
--table-header-bg: hsl(0, 0%, 80%);
29+
--table-alternate-bg: hsl(0, 0%, 97%);
30+
31+
--searchbar-border-color: #aaa;
32+
--searchbar-bg: #fafafa;
33+
--searchbar-fg: #000;
34+
--searchbar-shadow-color: #aaa;
35+
--searchresults-header-fg: #666;
36+
--searchresults-border-color: #888;
37+
--searchresults-li-bg: #e4f2fe;
38+
--search-mark-bg: #a2cff5;
39+
}

0 commit comments

Comments
 (0)