Skip to content

Commit 6531cfa

Browse files
authored
Merge pull request #20 from oprypin/iframeid
Generate iframe ids deterministically instead of randomly
2 parents 663400d + b2b5eec commit 6531cfa

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
lines changed

mkdocs_swagger_ui_tag/plugin.py

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import hashlib
12
import json
23
import logging
34
import os
4-
import uuid
55
from urllib.parse import unquote as urlunquote
66
from urllib.parse import urlsplit, urlunsplit
77

@@ -140,71 +140,55 @@ def on_post_page(self, output, page, config, **kwargs):
140140
if not os.path.exists(page_dir):
141141
os.makedirs(page_dir)
142142

143-
for swagger_ui_ele in swagger_ui_list:
144-
if swagger_ui_ele.has_attr("grouped"):
145-
grouped_list.append(swagger_ui_ele)
146-
continue
147-
148-
cur_id = str(uuid.uuid4())[:8]
149-
iframe_filename = f"swagger-{cur_id}.html"
150-
iframe_id_list.append(cur_id)
143+
def render_template(openapi_spec_url, swagger_ui_ele):
151144
cur_options = self.process_options(config, swagger_ui_ele)
152145
cur_oath2_prop = self.process_oath2_prop(swagger_ui_ele)
153146
oauth2_redirect_url = cur_options.pop("oauth2RedirectUrl", "")
154147
if not oauth2_redirect_url:
155148
oauth2_redirect_url = default_oauth2_redirect_file
156149

157-
openapi_spec_url = self.path_to_url(
158-
page.file, swagger_ui_ele.get("src", "")
159-
)
160-
output_from_parsed_template = template.render(
150+
template_output = template.render(
161151
css_dir=css_dir,
162152
extra_css_files=extra_css_files,
163153
js_dir=js_dir,
164154
background=self.config["background"],
165-
id=cur_id,
155+
id="{{ID_PLACEHOLDER}}", # ID is unknown yet - it's the hash of the content.
166156
openapi_spec_url=openapi_spec_url,
167157
oauth2_redirect_url=oauth2_redirect_url,
168158
validatorUrl=self.config["validatorUrl"],
169159
options_str=json.dumps(cur_options, indent=4)[1:-1],
170160
oath2_prop_str=json.dumps(cur_oath2_prop),
171161
)
162+
cur_id = hashlib.sha256(template_output.encode()).hexdigest()[:8]
163+
iframe_filename = f"swagger-{cur_id}.html"
164+
template_output = template_output.replace("{{ID_PLACEHOLDER}}", cur_id)
172165
with open(os.path.join(page_dir, iframe_filename), "w") as f:
173-
f.write(output_from_parsed_template)
166+
f.write(template_output)
174167
self.replace_with_iframe(soup, swagger_ui_ele, cur_id, iframe_filename)
175168

169+
for swagger_ui_ele in swagger_ui_list:
170+
if swagger_ui_ele.has_attr("grouped"):
171+
grouped_list.append(swagger_ui_ele)
172+
continue
173+
174+
openapi_spec_url = self.path_to_url(
175+
page.file, swagger_ui_ele.get("src", "")
176+
)
177+
render_template(
178+
openapi_spec_url=openapi_spec_url, swagger_ui_ele=swagger_ui_ele
179+
)
180+
176181
if grouped_list:
177-
cur_id = str(uuid.uuid4())[:8]
178-
iframe_filename = f"swagger-{cur_id}.html"
179-
iframe_id_list.append(cur_id)
180182
openapi_spec_url = []
181183
for swagger_ui_ele in grouped_list:
182184
cur_url = self.path_to_url(page.file, swagger_ui_ele.get("src", ""))
183185
cur_name = swagger_ui_ele.get("name", swagger_ui_ele.get("src", ""))
184186
openapi_spec_url.append({"url": cur_url, "name": cur_name})
185187

186188
# only use options from first grouped swagger ui tag
187-
cur_options = self.process_options(config, grouped_list[0])
188-
cur_oath2_prop = self.process_oath2_prop(grouped_list[0])
189-
oauth2_redirect_url = cur_options.pop("oauth2RedirectUrl", "")
190-
if not oauth2_redirect_url:
191-
oauth2_redirect_url = default_oauth2_redirect_file
192-
193-
output_from_parsed_template = template.render(
194-
css_dir=css_dir,
195-
extra_css_files=extra_css_files,
196-
js_dir=js_dir,
197-
background=self.config["background"],
198-
id=cur_id,
199-
openapi_spec_url=openapi_spec_url,
200-
oauth2_redirect_url=oauth2_redirect_url,
201-
validatorUrl=self.config["validatorUrl"],
202-
options_str=json.dumps(cur_options, indent=4)[1:-1],
203-
oath2_prop_str=json.dumps(cur_oath2_prop),
189+
render_template(
190+
openapi_spec_url=openapi_spec_url, swagger_ui_ele=grouped_list[0]
204191
)
205-
with open(os.path.join(page_dir, iframe_filename), "w") as f:
206-
f.write(output_from_parsed_template)
207-
self.replace_with_iframe(soup, grouped_list[0], cur_id, iframe_filename)
208192
# only keep first grouped swagger ui tag
209193
for rest_swagger_ui_ele in grouped_list[1:]:
210194
rest_swagger_ui_ele.extract()

0 commit comments

Comments
 (0)