Skip to content

Commit 62c3284

Browse files
committed
Fixed #2: Incompatible with mkdocs-material Instant loading feature
1 parent 66216e8 commit 62c3284

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
mkdocs-swagger-ui-tag 0.3.2 (2022-07-16)
2+
3+
* Fixed #2: Incompatible with mkdocs-material Instant loading feature
4+
15
mkdocs-swagger-ui-tag 0.3.1 (2022-07-16)
26

37
* Fixed #3: Incompatible with "--no-directory-urls"

mkdocs_swagger_ui_tag/plugin.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -154,54 +154,55 @@ def on_post_page(self, output, page, config, **kwargs):
154154
for rest_swagger_ui_ele in grouped_list[1:]:
155155
rest_swagger_ui_ele.extract()
156156

157-
if swagger_ui_list:
158-
js_code = soup.new_tag("script")
159-
js_code["type"] = "text/javascript"
160-
js_code.string = """
161-
update_swagger_ui_iframe_height = function (id) {
162-
var iFrameID = document.getElementById(id);
163-
if (iFrameID) {
164-
full_height = (iFrameID.contentWindow.document.body.scrollHeight + 80) + "px";
165-
iFrameID.height = full_height;
166-
iFrameID.style.height = full_height;
167-
}
157+
js_code = soup.new_tag("script")
158+
# trigger from iframe body ResizeObserver
159+
js_code.string = """
160+
window.update_swagger_ui_iframe_height = function (id) {
161+
var iFrameID = document.getElementById(id);
162+
if (iFrameID) {
163+
full_height = (iFrameID.contentWindow.document.body.scrollHeight + 80) + "px";
164+
iFrameID.height = full_height;
165+
iFrameID.style.height = full_height;
168166
}
169-
var scheme = document.body.getAttribute("data-md-color-scheme")
170-
const options = {
171-
childList: true,
172-
attributes: true,
173-
characterData: false,
174-
subtree: false,
175-
attributeFilter: ['data-md-color-scheme'],
176-
attributeOldValue: false,
177-
characterDataOldValue: false
178-
};
179-
"""
180-
js_code.string += f"""
181-
const iframe_id_list = {json.dumps(iframe_id_list)}
182-
"""
167+
}
168+
"""
169+
if config["theme"].name == "material":
170+
# synchronized dark mode with mkdocs-material
183171
js_code.string += """
184-
function color_scheme_callback(mutations) {
185-
for (let mutation of mutations) {
186-
if (mutation.attributeName === "data-md-color-scheme") {
187-
scheme = document.body.getAttribute("data-md-color-scheme")
188-
iframe_id_list.forEach((id) => {
189-
var ele = document.getElementById(id);
190-
if (ele) {
191-
if (scheme === "slate") {
192-
ele.contentWindow.enable_dark_mode();
193-
} else {
194-
ele.contentWindow.disable_dark_mode();
195-
}
172+
window.scheme = document.body.getAttribute("data-md-color-scheme")
173+
const options = {
174+
childList: true,
175+
attributes: true,
176+
characterData: false,
177+
subtree: false,
178+
attributeFilter: ['data-md-color-scheme'],
179+
attributeOldValue: false,
180+
characterDataOldValue: false
181+
};
182+
function color_scheme_callback(mutations) {
183+
for (let mutation of mutations) {
184+
if (mutation.attributeName === "data-md-color-scheme") {
185+
scheme = document.body.getAttribute("data-md-color-scheme")
186+
var iframe_list = document.getElementsByClassName("swagger-ui-iframe")
187+
for(var i = 0; i < iframe_list.length; i++) {
188+
var ele = iframe_list.item(i);
189+
if (ele) {
190+
if (scheme === "slate") {
191+
ele.contentWindow.enable_dark_mode();
192+
} else {
193+
ele.contentWindow.disable_dark_mode();
196194
}
197-
})
195+
}
198196
}
199197
}
200198
}
201-
observer = new MutationObserver(color_scheme_callback);
202-
observer.observe(document.body, options);
199+
}
200+
observer = new MutationObserver(color_scheme_callback);
201+
observer.observe(document.body, options);
203202
"""
204-
soup.body.append(js_code)
203+
# support compatible with mkdocs-material Instant loading feature
204+
js_code.string = "document$.subscribe(() => {" + js_code.string + "})"
205+
soup.body.append(js_code)
205206

206207
return str(soup)
207208

@@ -214,6 +215,7 @@ def replace_with_iframe(self, soup, swagger_ui_ele, cur_id,
214215
iframe['frameborder'] = "0"
215216
iframe['style'] = "overflow:hidden;width:100%;"
216217
iframe['width'] = "100%"
218+
iframe['class'] = "swagger-ui-iframe"
217219
swagger_ui_ele.replaceWith(iframe)
218220

219221
def process_options(self, swagger_ui_ele):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
long_description = f.read()
55

66
setup(name="mkdocs-swagger-ui-tag",
7-
version="0.3.1",
7+
version="0.3.2",
88
author="Blueswen",
99
author_email="blueswen.tw@gmail.com",
1010
url="https://blueswen.github.io/mkdocs-swagger-ui-tag",

0 commit comments

Comments
 (0)