Skip to content

Commit 9eca0e0

Browse files
committed
Additional theme
1 parent 19146c4 commit 9eca0e0

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
@@ -541,6 +541,8 @@ pub struct HtmlConfig {
541541
pub google_analytics: Option<String>,
542542
/// Additional CSS stylesheets to include in the rendered page's `<head>`.
543543
pub additional_css: Vec<PathBuf>,
544+
/// Additional theme.
545+
pub additional_theme: Vec<PathBuf>,
544546
/// Additional JS scripts to include at the bottom of the rendered page's
545547
/// `<body>`.
546548
pub additional_js: Vec<PathBuf>,
@@ -604,6 +606,7 @@ impl Default for HtmlConfig {
604606
copy_fonts: true,
605607
google_analytics: None,
606608
additional_css: Vec::new(),
609+
additional_theme: Vec::new(),
607610
additional_js: Vec::new(),
608611
fold: Fold::default(),
609612
playground: Playground::default(),
@@ -822,6 +825,7 @@ mod tests {
822825
smart-punctuation = true
823826
google-analytics = "123456"
824827
additional-css = ["./foo/bar/baz.css"]
828+
additional-theme = ["foobar"]
825829
git-repository-url = "https://foo.com/"
826830
git-repository-icon = "fa-code-fork"
827831
@@ -869,6 +873,7 @@ mod tests {
869873
smart_punctuation: true,
870874
google_analytics: Some(String::from("123456")),
871875
additional_css: vec![PathBuf::from("./foo/bar/baz.css")],
876+
additional_theme: vec![PathBuf::from("foobar")],
872877
theme: Some(PathBuf::from("./themedir")),
873878
default_theme: Some(String::from("rust")),
874879
playground: playground_should_be,
@@ -1049,6 +1054,7 @@ mod tests {
10491054
smart-punctuation = true
10501055
google-analytics = "123456"
10511056
additional-css = ["custom.css", "custom2.css"]
1057+
additional-theme = ["barfoo"]
10521058
additional-js = ["custom.js"]
10531059
"#;
10541060

@@ -1074,6 +1080,7 @@ mod tests {
10741080
smart_punctuation: true,
10751081
google_analytics: Some(String::from("123456")),
10761082
additional_css: vec![PathBuf::from("custom.css"), PathBuf::from("custom2.css")],
1083+
additional_theme: vec![PathBuf::from("barfoo")],
10771084
additional_js: vec![PathBuf::from("custom.js")],
10781085
..Default::default()
10791086
};

src/renderer/html_handlebars/hbs_renderer.rs

+12
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,18 @@ fn make_data(
566566
data.insert("additional_css".to_owned(), json!(css));
567567
}
568568

569+
// Add check to see if there are additional themes
570+
if !html_config.additional_theme.is_empty() {
571+
let mut theme = Vec::new();
572+
for name in &html_config.additional_theme {
573+
match name.strip_prefix(root) {
574+
Ok(p) => theme.push(p.to_str().expect("Could not convert to str")),
575+
Err(_) => theme.push(name.to_str().expect("Could not convert to str")),
576+
}
577+
}
578+
data.insert("additional_theme".to_owned(), json!(theme));
579+
}
580+
569581
// Add check to see if there is an additional script
570582
if !html_config.additional_js.is_empty() {
571583
let mut js = Vec::new();

src/theme/book.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ function playground_text(playground, hidden = true) {
309309
themePopup.querySelectorAll('.theme-selected').forEach(function (el) {
310310
el.classList.remove('theme-selected');
311311
});
312-
themePopup.querySelector("button#" + get_theme()).classList.add('theme-selected');
312+
try {
313+
themePopup.querySelector("button#" + get_theme()).classList.add('theme-selected');
314+
} catch (e) { }
313315
}
314316

315317
function hideThemes() {
@@ -322,9 +324,9 @@ function playground_text(playground, hidden = true) {
322324
var theme;
323325
try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
324326
if (theme === null || theme === undefined || !themeIds.includes(theme)) {
325-
return default_theme;
327+
return default_theme.replace(/\W+/g, '_').toLowerCase();
326328
} else {
327-
return theme;
329+
return theme.replace(/\W+/g, '_').toLowerCase();
328330
}
329331
}
330332

@@ -335,13 +337,17 @@ function playground_text(playground, hidden = true) {
335337
stylesheets.ayuHighlight.disabled = true;
336338
stylesheets.tomorrowNight.disabled = false;
337339
stylesheets.highlight.disabled = true;
338-
339340
ace_theme = "ace/theme/tomorrow_night";
340341
} else if (theme == 'ayu') {
341342
stylesheets.ayuHighlight.disabled = false;
342343
stylesheets.tomorrowNight.disabled = true;
343344
stylesheets.highlight.disabled = true;
344345
ace_theme = "ace/theme/tomorrow_night";
346+
} else if (theme == 'rust' || theme == 'light') {
347+
stylesheets.ayuHighlight.disabled = true;
348+
stylesheets.tomorrowNight.disabled = true;
349+
stylesheets.highlight.disabled = false;
350+
ace_theme = "ace/theme/dawn";
345351
} else {
346352
stylesheets.ayuHighlight.disabled = true;
347353
stylesheets.tomorrowNight.disabled = true;
@@ -359,17 +365,23 @@ function playground_text(playground, hidden = true) {
359365
});
360366
}
361367

362-
var previousTheme = get_theme();
363-
368+
var previousTheme = get_theme().replace(/\W+/g, '_').toLowerCase();
369+
var selectedTheme = theme.replace(/\W+/g, '_').toLowerCase();
364370
if (store) {
365-
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
371+
try { localStorage.setItem('mdbook-theme', selectedTheme); } catch (e) { }
366372
}
367373

368-
html.classList.remove(previousTheme);
369-
html.classList.add(theme);
374+
try {
375+
html.classList.remove( previousTheme );
376+
html.classList.add( selectedTheme );
377+
} catch (e) { }
378+
370379
updateThemeSelected();
371380
}
372381

382+
// Sanitize theme id names
383+
themePopup.querySelectorAll("button").forEach(e=>{e.id=e.id.replace(/\W+/g, '_').toLowerCase();});
384+
373385
// Set theme
374386
var theme = get_theme();
375387

src/theme/index.hbs

+8-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<script>
5858
var path_to_root = "{{ path_to_root }}";
5959
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "{{ preferred_dark_theme }}" : "{{ default_theme }}";
60+
default_theme = default_theme.replace(/\W+/g, '_').toLowerCase();
6061
</script>
6162
<!-- Start loading toc.js asap -->
6263
<script src="{{ resource "toc.js" }}"></script>
@@ -85,8 +86,10 @@
8586
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
8687
if (theme === null || theme === undefined) { theme = default_theme; }
8788
const html = document.documentElement;
88-
html.classList.remove('{{ default_theme }}')
89-
html.classList.add(theme);
89+
try {
90+
html.classList.remove('{{ default_theme }}');
91+
html.classList.add(theme.replace(/\W+/g, '_').toLowerCase());
92+
} catch(e) { }
9093
html.classList.add("js");
9194
</script>
9295

@@ -137,6 +140,9 @@
137140
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
138141
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
139142
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
143+
{{#each additional_theme}}
144+
<li role="none"><button role="menuitem" class="theme" id="{{ this }}">{{ this }}</button></li>
145+
{{/each}}
140146
</ul>
141147
{{#if search_enabled}}
142148
<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
@@ -10,6 +10,8 @@ edition = "2018"
1010
[output.html]
1111
mathjax-support = true
1212
hash-files = true
13+
additional-css = ["orange.css"]
14+
additional-theme = ["Orange"]
1315

1416
[output.html.playground]
1517
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)