Skip to content

Commit

Permalink
Fix undefined function bug when using mkdocs-material instant loading…
Browse files Browse the repository at this point in the history
… feature (#9)
  • Loading branch information
blueswen committed Jan 24, 2023
1 parent 7def5f3 commit 5834941
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 107 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
202 changes: 107 additions & 95 deletions mkdocs_swagger_ui_tag/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]",
url="https://blueswen.github.io/mkdocs-swagger-ui-tag",
Expand Down
12 changes: 1 addition & 11 deletions tests/test_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import os
import re
import shutil
import json
import collections

# MkDocs
from mkdocs.__main__ import build_command
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 5834941

Please sign in to comment.