Skip to content

Commit 1a0c721

Browse files
committed
Support custom dark mode scheme name for Material for MkDocs (#10)
1 parent 100f3d8 commit 1a0c721

File tree

7 files changed

+43
-7
lines changed

7 files changed

+43
-7
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
mkdocs-swagger-ui-tag 0.6.1 (2023-03-19)
2+
3+
* Supported custom dark mode scheme name for Material for MkDocs (#10)
4+
15
mkdocs-swagger-ui-tag 0.6.0 (2023-03-10)
26

37
* Updated swagger-ui-dist to 5.0.0-alpha.0, which starting support OpenAPI 3.1

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ A MkDocs plugin supports for add [Swagger UI](https://github.com/swagger-api/swa
7373
| supportedSubmitMethods | Array | Default: All Http Methods. Array=["get", "put", "post", "delete", "options", "head", "patch", "trace"]. List of HTTP methods that have the "Try it out" feature enabled. An empty array disables "Try it out" for all operations. This does not filter the operations from the display. |
7474
| validatorUrl | String | Default: "none". By default, the validation is disabled. When setting with "https://validator.swagger.io/validator", Swagger UI attempts to validate specs against swagger.io's online validator in multiple OAS Swagger UI. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). |
7575
| extra_css | Array | Default: []. Extra CSS files in your `docs_dir` included in Swagger UI iframe target html file. |
76+
| dark_scheme_name | String | Default: "slate". Custom dark mode scheme name for [Material for MkDocs](https://squidfunk.github.io/mkdocs-material/). Check more details on [Material for MkDocs Documents](https://squidfunk.github.io/mkdocs-material/setup/changing-the-colors/). |
7677
7778
## How it works
7879

mkdocs_swagger_ui_tag/plugin.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class SwaggerUIPlugin(BasePlugin):
5555
),
5656
("validatorUrl", config_options.Type(str, default="none")),
5757
("extra_css", config_options.Type(list, default=[])),
58+
("dark_scheme_name", config_options.Type(str, default="slate")),
5859
)
5960

6061
def on_pre_page(self, page, config, files, **kwargs):
@@ -126,10 +127,12 @@ def on_post_page(self, output, page, config, **kwargs):
126127
loader=FileSystemLoader(os.path.join(base_path, "swagger-ui"))
127128
)
128129
template = env.get_template("swagger.html")
129-
extra_css_files = list(map(
130-
lambda f: utils.get_relative_url(utils.normalize_url(f), page.url),
131-
self.config["extra_css"],
132-
))
130+
extra_css_files = list(
131+
map(
132+
lambda f: utils.get_relative_url(utils.normalize_url(f), page.url),
133+
self.config["extra_css"],
134+
)
135+
)
133136

134137
page_dir = os.path.dirname(
135138
os.path.join(config["site_dir"], urlunquote(page.url))
@@ -257,6 +260,9 @@ def on_post_page(self, output, page, config, **kwargs):
257260
"""
258261
if config["theme"].name == "material":
259262
# synchronized dark mode with mkdocs-material
263+
js_code.string += f"""
264+
const dark_scheme_name = "{self.config['dark_scheme_name']}"
265+
"""
260266
js_code.string += """
261267
window.scheme = document.body.getAttribute("data-md-color-scheme")
262268
const options = {
@@ -270,7 +276,7 @@ def on_post_page(self, output, page, config, **kwargs):
270276
for(var i = 0; i < iframe_list.length; i++) {
271277
var ele = iframe_list.item(i);
272278
if (ele) {
273-
if (scheme === "slate") {
279+
if (scheme === dark_scheme_name) {
274280
ele.contentWindow.enable_dark_mode();
275281
} else {
276282
ele.contentWindow.disable_dark_mode();

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
long_description = f.read()
55

66
setup(name="mkdocs-swagger-ui-tag",
7-
version="0.6.0",
7+
version="0.6.1",
88
author="Blueswen",
99
author_email="blueswen.tw@gmail.com",
1010
url="https://blueswen.github.io/mkdocs-swagger-ui-tag",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
site_name: test mkdocs_swagger_ui_tag
2+
use_directory_urls: true
3+
4+
theme:
5+
name: material
6+
7+
markdown_extensions:
8+
- attr_list
9+
- md_in_html
10+
11+
plugins:
12+
- swagger-ui-tag:
13+
dark_scheme_name: white

tests/fixtures/mkdocs-options.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,3 @@ plugins:
1515
extra_css:
1616
- stylesheets/extra-1.css
1717
- stylesheets/sub_dir/extra-2.css
18-

tests/test_builds.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ def test_material(tmp_path):
328328
file = testproject_path / "site/index.html"
329329
contents = file.read_text(encoding="utf8")
330330
validate_additional_script_code_for_material(contents, exists=True)
331+
assert 'const dark_scheme_name = "slate"' in contents
331332

332333
iframe_content_list = validate_iframe(contents, file.parent)
333334
assert len(iframe_content_list) == 1
@@ -343,6 +344,18 @@ def test_material(tmp_path):
343344
assert (file.parent / openapi_spec_url).resolve().exists()
344345

345346

347+
def test_material_dark_scheme_name(tmp_path):
348+
"""
349+
Integrate with Material for MkDocs
350+
"""
351+
mkdocs_file = "mkdocs-material-options.yml"
352+
testproject_path = validate_mkdocs_file(tmp_path, f"tests/fixtures/{mkdocs_file}")
353+
file = testproject_path / "site/index.html"
354+
contents = file.read_text(encoding="utf8")
355+
validate_additional_script_code_for_material(contents, exists=True)
356+
assert 'const dark_scheme_name = "white"' in contents
357+
358+
346359
def test_url(tmp_path):
347360
"""
348361
Validate online OpenAPI Spec

0 commit comments

Comments
 (0)