Skip to content

Commit 9219858

Browse files
committed
Add explicit integration with mkdocs-material
1 parent 7fe7be2 commit 9219858

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

docs/options.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
1313
vega_renderer: svg
1414
use_data_path: True
1515
data_path: ""
16+
integrations:
17+
mkdocs_material:
18+
themes_light:
19+
- default
20+
themes_dark:
21+
- slate
1622
```
1723
1824
- `vega_width` (default is `container`). When not specified explicitly in the JSON schema, the `width` to use (see [vegalite customizing size](https://vega.github.io/vega-lite/docs/size.html)). When set to `container` width will be 100%.
@@ -21,4 +27,6 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
2127
- `vega_renderer` (default is `svg`). Specify one of the available [vegalite renderers](https://vega.github.io/vega-themes/).
2228
- `use_data_path` (default is `True`). When `True`, any relative urls used in the JSON schema are relative to the `data_path`. When `False`, relative urls should be relative to the URL of the page.
2329
- `data_path` (default is `""`). When `use_data_path` is enabled, the base path relative to the `docs/` folder.
30+
- `integrations.mkdocs_material.themes_light` (default is `[default]`). When using the [mkdocs-material](https://squidfunk.github.io/mkdocs-material) theme, specify the light [color schemes](https://squidfunk.github.io/mkdocs-material/setup/changing-the-colors/#color-scheme).
31+
- `integrations.mkdocs_material.themes_dark` (default is `[slate]`). When using the [mkdocs-material](https://squidfunk.github.io/mkdocs-material) theme, specify the dark [color schemes](https://squidfunk.github.io/mkdocs-material/setup/changing-the-colors/#color-scheme).
2432

mkdocs_charts_plugin/js/mkdocs-charts-plugin.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ function updateURL(url) {
122122
return url;
123123
}
124124

125+
const bodyelement = document.querySelector('body');
126+
127+
function getTheme() {
128+
// Get theme according to mkdocs-material's color scheme
129+
const materialColorScheme = bodyelement.getAttribute('data-md-color-scheme');
130+
if (materialColorScheme) {
131+
return mkdocs_chart_plugin['integrations']['mkdocs_material']['themes_dark'].includes(materialColorScheme)
132+
? mkdocs_chart_plugin['vega_theme_dark']
133+
: mkdocs_chart_plugin['vega_theme'];
134+
}
135+
// Fall back to light theme
136+
return mkdocs_chart_plugin['vega_theme'];
137+
}
138+
125139
var vegalite_charts = [];
126140

127141
function embedChart(block, schema) {
@@ -178,14 +192,10 @@ function embedChart(block, schema) {
178192
// in a different theme
179193
vegalite_charts.push({'block' : block, 'schema': schema});
180194

181-
// mkdocs-material has a dark mode
182-
// detect which one is being used
183-
var theme = (document.querySelector('body').getAttribute('data-md-color-scheme') == 'slate') ? mkdocs_chart_plugin['vega_theme_dark'] : mkdocs_chart_plugin['vega_theme'];
184-
185195
// Render the chart
186196
vegaEmbed(block, schema, {
187197
actions: false,
188-
"theme": theme,
198+
"theme": getTheme(),
189199
"renderer": mkdocs_chart_plugin['vega_renderer']
190200
});
191201
}
@@ -216,26 +226,25 @@ const chartplugin = className => {
216226

217227
}
218228
}
219-
220229

221230
// mkdocs-material has a dark mode including a toggle
222231
// We should watch when dark mode changes and update charts accordingly
223232

224-
var bodyelement = document.querySelector('body');
225233
var observer = new MutationObserver(function(mutations) {
226234
mutations.forEach(function(mutation) {
227235
if (mutation.type === "attributes") {
228236

229237
if (mutation.attributeName == "data-md-color-scheme") {
230238

231-
var theme = (bodyelement.getAttribute('data-md-color-scheme') == 'slate') ? mkdocs_chart_plugin['vega_theme_dark'] : mkdocs_chart_plugin['vega_theme'];
239+
const theme = getTheme();
232240
for (let i = 0; i < vegalite_charts.length; i++) {
233241
vegaEmbed(vegalite_charts[i].block, vegalite_charts[i].schema, {
234-
actions: false,
235-
"theme": theme,
242+
actions: false,
243+
theme,
236244
"renderer": mkdocs_chart_plugin['vega_renderer']
237245
});
238246
}
247+
239248
}
240249

241250
}
@@ -245,7 +254,6 @@ observer.observe(bodyelement, {
245254
attributes: true //configure it to listen to attribute changes
246255
});
247256

248-
249257
// Load when DOM ready
250258
if (typeof document$ !== "undefined") {
251259
// compatibility with mkdocs-material's instant loading feature

mkdocs_charts_plugin/plugin.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22

3-
from mkdocs.config import config_options
3+
from mkdocs.config import base, config_options
44
from mkdocs.exceptions import PluginError
55
from mkdocs.plugins import BasePlugin
66
from mkdocs.utils import copy_file
@@ -22,6 +22,15 @@ def check_library(libnames, dependency):
2222
)
2323

2424

25+
class MkdocsMaterialOptions(base.Config):
26+
themes_light = config_options.ListOfItems(config_options.Type(str), default=["default"])
27+
themes_dark = config_options.ListOfItems(config_options.Type(str), default=["slate"])
28+
29+
30+
class IntegrationsOptions(base.Config):
31+
mkdocs_material = config_options.SubConfig(MkdocsMaterialOptions)
32+
33+
2534
class ChartsPlugin(BasePlugin):
2635
config_scheme = (
2736
("data_path", config_options.Type(str, default="")),
@@ -31,6 +40,7 @@ class ChartsPlugin(BasePlugin):
3140
("vega_renderer", config_options.Type(str, default="svg")),
3241
("vega_width", config_options.Type(str, default="container")),
3342
("fallback_width", config_options.Type(str, default="800")),
43+
("integrations", config_options.SubConfig(IntegrationsOptions)),
3444
)
3545

3646
def on_config(self, config, **kwargs):

0 commit comments

Comments
 (0)