Skip to content

Commit 4b2d0b1

Browse files
committed
Fix extra CSS files in Swagger UI iframe bug (#7)
1 parent d2f771b commit 4b2d0b1

File tree

6 files changed

+29
-23
lines changed

6 files changed

+29
-23
lines changed

mkdocs_swagger_ui_tag/plugin.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ def on_post_page(self, output, page, config, **kwargs):
128128
loader=FileSystemLoader(os.path.join(base_path, "swagger-ui"))
129129
)
130130
template = env.get_template("swagger.html")
131-
extra_css_files = map(
132-
lambda f: os.path.basename(f),
133-
filter(lambda f: os.path.exists(f), self.config["extra_css"]),
134-
)
131+
extra_css_files = list(map(
132+
lambda f: utils.get_relative_url(utils.normalize_url(f), page.url),
133+
self.config["extra_css"],
134+
))
135135

136136
page_dir = os.path.dirname(
137137
os.path.join(config["site_dir"], urlunquote(page.url))
@@ -376,17 +376,6 @@ def on_post_build(self, config, **kwargs):
376376
os.path.join(css_path, file_name),
377377
)
378378

379-
for extra_css_file in self.config["extra_css"]:
380-
extra_css_file_path = os.path.normpath(extra_css_file)
381-
if not os.path.exists(extra_css_file_path):
382-
logging.warning(f"extra_css: {extra_css_file} dose not exist")
383-
else:
384-
file_name = os.path.basename(extra_css_file_path)
385-
utils.copy_file(
386-
os.path.join(extra_css_file_path),
387-
os.path.join(css_path, file_name),
388-
)
389-
390379
js_path = os.path.join(output_base_path, "javascripts")
391380
for file_name in os.listdir(
392381
os.path.join(base_path, "swagger-ui", "javascripts")

mkdocs_swagger_ui_tag/swagger-ui/swagger.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<link rel="stylesheet" type="text/css" href="{{ css_dir }}swagger-ui.css" />
88
<link rel="stylesheet" type="text/css" id="slate-css" media="none" href="{{ css_dir }}swagger-ui-dark.css" />
99
{% for css_file in extra_css_files %}
10-
<link rel="stylesheet" type="text/css" href="{{ css_dir }}{{ css_file }}" />
10+
<link rel="stylesheet" type="text/css" href="{{ css_file }}" />
1111
{% endfor %}
1212
</head>
1313

File renamed without changes.
File renamed without changes.

tests/fixtures/mkdocs-options.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ plugins:
1313
- get
1414
validatorUrl: "https://validator.swagger.io/validator"
1515
extra_css:
16-
- not_exist/extra.css
17-
- docs/extra-1.css
18-
- docs/sub_dir/extra-2.css
16+
- stylesheets/extra-1.css
17+
- stylesheets/sub_dir/extra-2.css
1918

tests/test_builds.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,28 @@ def test_plugin_options(tmp_path):
492492
r'"supportedSubmitMethods": \[(\n|\r) "get"(\n|\r) \]', iframe_content
493493
)
494494

495-
assert (testproject_path / "site/assets/stylesheets/extra-1.css").exists()
496-
assert (testproject_path / "site/assets/stylesheets/extra-2.css").exists()
497-
assert "assets/stylesheets/extra-1.css" in iframe_content
498-
assert "assets/stylesheets/extra-2.css" in iframe_content
495+
assert (testproject_path / "site/stylesheets/extra-1.css").exists()
496+
assert (testproject_path / "site/stylesheets/sub_dir/extra-2.css").exists()
497+
assert '"stylesheets/extra-1.css"' in iframe_content
498+
assert '"stylesheets/sub_dir/extra-2.css"' in iframe_content
499+
500+
file = testproject_path / "site/sub_dir/page_in_sub_dir/index.html"
501+
contents = file.read_text(encoding="utf8")
502+
503+
iframe_content_list = validate_iframe(contents, file.parent)
504+
assert len(iframe_content_list) == 1
505+
iframe_content = iframe_content_list[0]
506+
assert '"../../stylesheets/extra-1.css"' in iframe_content
507+
assert '"../../stylesheets/sub_dir/extra-2.css"' in iframe_content
508+
509+
file = testproject_path / "site/multiple/index.html"
510+
contents = file.read_text(encoding="utf8")
511+
512+
iframe_content_list = validate_iframe(contents, file.parent)
513+
assert len(iframe_content_list) == 3
514+
for iframe_content in iframe_content_list:
515+
assert '"../stylesheets/extra-1.css"' in iframe_content
516+
assert '"../stylesheets/sub_dir/extra-2.css"' in iframe_content
499517

500518

501519
def test_attribute_options(tmp_path):

0 commit comments

Comments
 (0)