diff --git a/CHANGELOG b/CHANGELOG index 11a83aa..c6b2ffe 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +mkdocs-swagger-ui-tag 0.5.2 (20223-01-24) + + * Fixed undefined function bug when using mkdocs-material instant loading feature (#9) + mkdocs-swagger-ui-tag 0.5.1 (2022-12-13) * Fixed extra CSS files in Swagger UI iframe bug (#7) diff --git a/mkdocs_swagger_ui_tag/plugin.py b/mkdocs_swagger_ui_tag/plugin.py index b36fc84..4ebc75e 100644 --- a/mkdocs_swagger_ui_tag/plugin.py +++ b/mkdocs_swagger_ui_tag/plugin.py @@ -112,101 +112,99 @@ def on_post_page(self, output, page, config, **kwargs): iframe_id_list = [] grouped_list = [] - if len(swagger_ui_list) == 0: - return str(soup) - - css_dir = utils.get_relative_url( - utils.normalize_url("assets/stylesheets/"), page.url - ) - js_dir = utils.get_relative_url( - utils.normalize_url("assets/javascripts/"), page.url - ) - default_oauth2_redirect_file = utils.get_relative_url( - utils.normalize_url("assets/swagger-ui/oauth2-redirect.html"), page.url - ) - env = Environment( - loader=FileSystemLoader(os.path.join(base_path, "swagger-ui")) - ) - template = env.get_template("swagger.html") - extra_css_files = list(map( - lambda f: utils.get_relative_url(utils.normalize_url(f), page.url), - self.config["extra_css"], - )) - - page_dir = os.path.dirname( - os.path.join(config["site_dir"], urlunquote(page.url)) - ) - if not os.path.exists(page_dir): - os.makedirs(page_dir) - - for swagger_ui_ele in swagger_ui_list: - if swagger_ui_ele.has_attr("grouped"): - grouped_list.append(swagger_ui_ele) - continue - - cur_id = str(uuid.uuid4())[:8] - iframe_filename = f"swagger-{cur_id}.html" - iframe_id_list.append(cur_id) - cur_options = self.process_options(config, swagger_ui_ele) - cur_oath2_prop = self.process_oath2_prop(swagger_ui_ele) - oauth2_redirect_url = cur_options.pop("oauth2RedirectUrl", "") - if not oauth2_redirect_url: - oauth2_redirect_url = default_oauth2_redirect_file - - openapi_spec_url = self.path_to_url( - page.file, swagger_ui_ele.get("src", "") + if len(swagger_ui_list) > 0: + css_dir = utils.get_relative_url( + utils.normalize_url("assets/stylesheets/"), page.url + ) + js_dir = utils.get_relative_url( + utils.normalize_url("assets/javascripts/"), page.url ) - output_from_parsed_template = template.render( - css_dir=css_dir, - extra_css_files=extra_css_files, - js_dir=js_dir, - background=self.config["background"], - id=cur_id, - openapi_spec_url=openapi_spec_url, - oauth2_redirect_url=oauth2_redirect_url, - validatorUrl=self.config["validatorUrl"], - options_str=json.dumps(cur_options, indent=4)[1:-1], - oath2_prop_str=json.dumps(cur_oath2_prop), + default_oauth2_redirect_file = utils.get_relative_url( + utils.normalize_url("assets/swagger-ui/oauth2-redirect.html"), page.url ) - with open(os.path.join(page_dir, iframe_filename), "w") as f: - f.write(output_from_parsed_template) - self.replace_with_iframe(soup, swagger_ui_ele, cur_id, iframe_filename) - - if grouped_list: - cur_id = str(uuid.uuid4())[:8] - iframe_filename = f"swagger-{cur_id}.html" - iframe_id_list.append(cur_id) - openapi_spec_url = [] - for swagger_ui_ele in grouped_list: - cur_url = self.path_to_url(page.file, swagger_ui_ele.get("src", "")) - cur_name = swagger_ui_ele.get("name", swagger_ui_ele.get("src", "")) - openapi_spec_url.append({"url": cur_url, "name": cur_name}) - - # only use options from first grouped swagger ui tag - cur_options = self.process_options(config, grouped_list[0]) - cur_oath2_prop = self.process_oath2_prop(grouped_list[0]) - oauth2_redirect_url = cur_options.pop("oauth2RedirectUrl", "") - if not oauth2_redirect_url: - oauth2_redirect_url = default_oauth2_redirect_file - - output_from_parsed_template = template.render( - css_dir=css_dir, - extra_css_files=extra_css_files, - js_dir=js_dir, - background=self.config["background"], - id=cur_id, - openapi_spec_url=openapi_spec_url, - oauth2_redirect_url=oauth2_redirect_url, - validatorUrl=self.config["validatorUrl"], - options_str=json.dumps(cur_options, indent=4)[1:-1], - oath2_prop_str=json.dumps(cur_oath2_prop), + env = Environment( + loader=FileSystemLoader(os.path.join(base_path, "swagger-ui")) ) - with open(os.path.join(page_dir, iframe_filename), "w") as f: - f.write(output_from_parsed_template) - self.replace_with_iframe(soup, grouped_list[0], cur_id, iframe_filename) - # only keep first grouped swagger ui tag - for rest_swagger_ui_ele in grouped_list[1:]: - rest_swagger_ui_ele.extract() + template = env.get_template("swagger.html") + extra_css_files = list(map( + lambda f: utils.get_relative_url(utils.normalize_url(f), page.url), + self.config["extra_css"], + )) + + page_dir = os.path.dirname( + os.path.join(config["site_dir"], urlunquote(page.url)) + ) + if not os.path.exists(page_dir): + os.makedirs(page_dir) + + for swagger_ui_ele in swagger_ui_list: + if swagger_ui_ele.has_attr("grouped"): + grouped_list.append(swagger_ui_ele) + continue + + cur_id = str(uuid.uuid4())[:8] + iframe_filename = f"swagger-{cur_id}.html" + iframe_id_list.append(cur_id) + cur_options = self.process_options(config, swagger_ui_ele) + cur_oath2_prop = self.process_oath2_prop(swagger_ui_ele) + oauth2_redirect_url = cur_options.pop("oauth2RedirectUrl", "") + if not oauth2_redirect_url: + oauth2_redirect_url = default_oauth2_redirect_file + + openapi_spec_url = self.path_to_url( + page.file, swagger_ui_ele.get("src", "") + ) + output_from_parsed_template = template.render( + css_dir=css_dir, + extra_css_files=extra_css_files, + js_dir=js_dir, + background=self.config["background"], + id=cur_id, + openapi_spec_url=openapi_spec_url, + oauth2_redirect_url=oauth2_redirect_url, + validatorUrl=self.config["validatorUrl"], + options_str=json.dumps(cur_options, indent=4)[1:-1], + oath2_prop_str=json.dumps(cur_oath2_prop), + ) + with open(os.path.join(page_dir, iframe_filename), "w") as f: + f.write(output_from_parsed_template) + self.replace_with_iframe(soup, swagger_ui_ele, cur_id, iframe_filename) + + if grouped_list: + cur_id = str(uuid.uuid4())[:8] + iframe_filename = f"swagger-{cur_id}.html" + iframe_id_list.append(cur_id) + openapi_spec_url = [] + for swagger_ui_ele in grouped_list: + cur_url = self.path_to_url(page.file, swagger_ui_ele.get("src", "")) + cur_name = swagger_ui_ele.get("name", swagger_ui_ele.get("src", "")) + openapi_spec_url.append({"url": cur_url, "name": cur_name}) + + # only use options from first grouped swagger ui tag + cur_options = self.process_options(config, grouped_list[0]) + cur_oath2_prop = self.process_oath2_prop(grouped_list[0]) + oauth2_redirect_url = cur_options.pop("oauth2RedirectUrl", "") + if not oauth2_redirect_url: + oauth2_redirect_url = default_oauth2_redirect_file + + output_from_parsed_template = template.render( + css_dir=css_dir, + extra_css_files=extra_css_files, + js_dir=js_dir, + background=self.config["background"], + id=cur_id, + openapi_spec_url=openapi_spec_url, + oauth2_redirect_url=oauth2_redirect_url, + validatorUrl=self.config["validatorUrl"], + options_str=json.dumps(cur_options, indent=4)[1:-1], + oath2_prop_str=json.dumps(cur_oath2_prop), + ) + with open(os.path.join(page_dir, iframe_filename), "w") as f: + f.write(output_from_parsed_template) + self.replace_with_iframe(soup, grouped_list[0], cur_id, iframe_filename) + # only keep first grouped swagger ui tag + for rest_swagger_ui_ele in grouped_list[1:]: + rest_swagger_ui_ele.extract() js_code = soup.new_tag("script") # trigger from iframe body ResizeObserver @@ -221,17 +219,31 @@ def on_post_page(self, output, page, config, **kwargs): } """ # listen scroll event to update modal position in iframe - js_code.string += f""" - const iframe_id_list = {json.dumps(iframe_id_list)}; - """ js_code.string += """ + let iframe_id_list = [] + var iframes = document.getElementsByClassName("swagger-ui-iframe"); + 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(()=> { let half_vh = window.innerHeight/2; for(var i = 0; i < iframe_id_list.length; i++) { let element = document.getElementById(iframe_id_list[i]) + if(element==null){ + return + } let diff = element.getBoundingClientRect().top if(element.contentWindow.update_top_val){ element.contentWindow.update_top_val(half_vh - diff) diff --git a/setup.py b/setup.py index f2805b7..043ee11 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = f.read() setup(name="mkdocs-swagger-ui-tag", - version="0.5.1", + version="0.5.2", author="Blueswen", author_email="blueswen.tw@gmail.com", url="https://blueswen.github.io/mkdocs-swagger-ui-tag", diff --git a/tests/test_builds.py b/tests/test_builds.py index 13398c8..cd7a841 100644 --- a/tests/test_builds.py +++ b/tests/test_builds.py @@ -3,8 +3,6 @@ import os import re import shutil -import json -import collections # MkDocs from mkdocs.__main__ import build_command @@ -179,14 +177,6 @@ def validate_iframe(html_content, iframe_src_dir): iframe_content_list.append(iframe_content) iframe_id_list.append(iframe_id) - regex_obj = re.search( - r"const iframe_id_list = (.*);", - html_content, - ) - assert collections.Counter(iframe_id_list) == collections.Counter( - json.loads(regex_obj.group(1)) - ) - return iframe_content_list @@ -556,7 +546,7 @@ def test_empty(tmp_path): file = testproject_path / "site/empty/index.html" contents = file.read_text(encoding="utf8") - validate_additional_script_code(contents, exists=False) + validate_additional_script_code(contents, exists=True) def test_error(tmp_path):