Skip to content

Commit

Permalink
Rename filter_filenames to filter_files and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
blueswen committed May 23, 2024
1 parent ade0f07 commit 04b12ef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
17 changes: 4 additions & 13 deletions mkdocs_swagger_ui_tag/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class SwaggerUIPlugin(BasePlugin):
("extra_css", config_options.Type(list, default=[])),
("dark_scheme_name", config_options.Type(str, default="slate")),
(
"filter_filenames",
"filter_files",
config_options.ListOfItems(config_options.Type(str), default=[]),
),
)
Expand Down Expand Up @@ -120,10 +120,10 @@ def on_post_page(self, output, page, config, **kwargs):
Add javascript code to update iframe height
Create a html with Swagger UI for iframe
"""
# Using filename filter for performance
# Using file filter for performance
# https://github.com/blueswen/mkdocs-swagger-ui-tag/issues/25
if filter_list := self.config["filter_filenames"]:
if page.file.name not in filter_list:
if filter_list := self.config["filter_files"]:
if page.file.src_path not in filter_list:
return output

soup = BeautifulSoup(output, "html.parser")
Expand Down Expand Up @@ -239,16 +239,7 @@ def render_template(openapi_spec_url, swagger_ui_ele):
for (var i = 0; i < iframes.length; i++) {
iframe_id_list.push(iframes[i].getAttribute("id"))
}
"""
if len(iframe_id_list) == 0:
js_code.string += """
let ticking = true;
"""
else:
js_code.string += """
let ticking = false;
"""
js_code.string += """
document.addEventListener('scroll', function(e) {
if (!ticking) {
window.requestAnimationFrame(()=> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ use_directory_urls: true

plugins:
- swagger-ui-tag:
filter_filenames: ["index"]
filter_files:
- index.md
- sub_dir/page_in_sub_dir.md
30 changes: 28 additions & 2 deletions tests/test_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import re
import shutil
from unittest.mock import MagicMock

# other 3rd party
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -550,8 +551,8 @@ def test_template(tmp_path):
assert len(iframe_content_list) == 2


def test_filter_filenames(tmp_path):
mkdocs_file = "mkdocs-filter-filenames.yml"
def test_filter_files(tmp_path):
mkdocs_file = "mkdocs-filter-files.yml"
testproject_path = validate_mkdocs_file(
tmp_path,
f"tests/fixtures/{mkdocs_file}",
Expand All @@ -561,10 +562,35 @@ def test_filter_filenames(tmp_path):
iframe_content_list = validate_iframe(contents, file.parent)
assert len(iframe_content_list) == 1

file = testproject_path / "site/sub_dir/page_in_sub_dir/index.html"
contents = file.read_text(encoding="utf8")
iframe_content_list = validate_iframe(contents, file.parent)
assert len(iframe_content_list) == 1

file = testproject_path / "site/empty/index.html"
contents = file.read_text(encoding="utf8")
validate_additional_script_code(contents, exists=False)

file = testproject_path / "site/multiple/index.html"
contents = file.read_text(encoding="utf8")
validate_additional_script_code(contents, exists=False)


def test_import_error(monkeypatch):
# Simulate ImportError for 'get_plugin_logger'
with monkeypatch.context() as m:
m.setattr(
"mkdocs.plugins.get_plugin_logger", MagicMock(side_effect=ImportError)
)

# Reload the module to apply the monkeypatch
import importlib

from mkdocs_swagger_ui_tag import plugin

importlib.reload(plugin)
# Test that the fallback logger is used
assert plugin.log.name == f"mkdocs.plugins.{plugin.__name__}"

# Ensure the logger works without raising errors
plugin.log.info("Test message")

0 comments on commit 04b12ef

Please sign in to comment.