Skip to content

Commit 5bb8a7c

Browse files
committed
Support retrieving default oauth2-redirect.html absolute URL from relative URL (#4)
1 parent 2890958 commit 5bb8a7c

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
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.4.2 (2022-08-17)
2+
3+
* Support retrieving default oauth2-redirect.html absolute URL from relative URL (#4)
4+
15
mkdocs-swagger-ui-tag 0.4.1 (2022-08-16)
26

37
* Added support for Swagger UI OAuth2 redirect with oauth2-redirect.html as default (#4)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ A MkDocs plugin supports for add [Swagger UI](https://github.com/swagger-api/swa
6565
| filter | String or Boolean | Default: False. If set, enables filtering. The top bar will show an edit box that you can use to filter the tagged operations that are shown. Can be Boolean to enable or disable, or a string, in which case filtering will be enabled using that string as the filter expression. Filtering is case sensitive matching the filter expression anywhere inside the tag. |
6666
| syntaxHighlightTheme | String | Default: "agate". [Highlight.js](https://highlightjs.org/static/demo/) syntax coloring theme to use. It can be "agate", "arta", "monokai", "nord", "obsidian" or "tomorrow-night" |
6767
| tryItOutEnabled | Boolean | Default: False. Controls whether the "Try it out" section should be enabled by default. |
68-
| oauth2RedirectUrl | String | Default: [site url]/assets/swagger-ui/oauth2-redirect.html. OAuth redirect URL. |
68+
| oauth2RedirectUrl | String | Default: Absolute URL of "/assets/swagger-ui/oauth2-redirect.html" relative with site_url in mkdocs.yml or document root path on site without site_url, e.g. "[https://blueswen.github.io/mkdocs-swagger-ui-tag/assets/swagger-ui/oauth2-redirect.html](https://blueswen.github.io/mkdocs-swagger-ui-tag/assets/swagger-ui/oauth2-redirect.html)". OAuth redirect URL. |
6969
| supportedSubmitMethods | Array | Default: All Http Methods. Array=["get", "put", "post", "delete", "options", "head", "patch", "trace"]. List of HTTP methods that have the "Try it out" feature enabled. An empty array disables "Try it out" for all operations. This does not filter the operations from the display. |
7070
| validatorUrl | String | Default: "https://validator.swagger.io/validator". By default, Swagger UI attempts to validate specs against swagger.io's online validator in multiple OAS Swagger UI. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it "none" to disable validation. |
7171

mkdocs_swagger_ui_tag/plugin.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import uuid
55
from urllib.parse import unquote as urlunquote
6-
from urllib.parse import urlsplit, urlunsplit, urljoin
6+
from urllib.parse import urlsplit, urlunsplit
77

88
from bs4 import BeautifulSoup
99
from jinja2 import Environment, FileSystemLoader
@@ -88,6 +88,9 @@ def on_post_page(self, output, page, config, **kwargs):
8888
utils.normalize_url("assets/stylesheets/"), page.url)
8989
js_dir = utils.get_relative_url(
9090
utils.normalize_url("assets/javascripts/"), page.url)
91+
default_oauth2_redirect_file = utils.get_relative_url(
92+
utils.normalize_url("assets/swagger-ui/oauth2-redirect.html"),
93+
page.url)
9194
env = Environment(
9295
loader=FileSystemLoader(os.path.join(base_path, "swagger-ui")))
9396
template = env.get_template('swagger.html')
@@ -111,6 +114,9 @@ def on_post_page(self, output, page, config, **kwargs):
111114
iframe_id_list.append(cur_id)
112115
cur_options = self.process_options(config, swagger_ui_ele)
113116
cur_oath2_prop = self.process_oath2_prop(swagger_ui_ele)
117+
oauth2_redirect_url = cur_options.pop('oauth2RedirectUrl', '')
118+
if not oauth2_redirect_url:
119+
oauth2_redirect_url = default_oauth2_redirect_file
114120

115121
openapi_spec_url = self.path_to_url(page.file,
116122
swagger_ui_ele.get('src', ""))
@@ -120,8 +126,9 @@ def on_post_page(self, output, page, config, **kwargs):
120126
background=self.config['background'],
121127
id=cur_id,
122128
openapi_spec_url=openapi_spec_url,
123-
options=json.dumps(cur_options, indent=4)[1:-1],
124-
oath2_prop=json.dumps(cur_oath2_prop))
129+
oauth2_redirect_url=oauth2_redirect_url,
130+
options_str=json.dumps(cur_options, indent=4)[1:-1],
131+
oath2_prop_str=json.dumps(cur_oath2_prop))
125132
with open(os.path.join(page_dir, iframe_filename), 'w') as f:
126133
f.write(output_from_parsed_template)
127134
self.replace_with_iframe(soup, swagger_ui_ele, cur_id,
@@ -142,14 +149,19 @@ def on_post_page(self, output, page, config, **kwargs):
142149
# only use options from first grouped swagger ui tag
143150
cur_options = self.process_options(config, grouped_list[0])
144151
cur_oath2_prop = self.process_oath2_prop(grouped_list[0])
152+
oauth2_redirect_url = cur_options.pop('oauth2RedirectUrl', '')
153+
if not oauth2_redirect_url:
154+
oauth2_redirect_url = default_oauth2_redirect_file
155+
145156
output_from_parsed_template = template.render(
146157
css_dir=css_dir,
147158
js_dir=js_dir,
148159
background=self.config['background'],
149160
id=cur_id,
150161
openapi_spec_url=openapi_spec_url,
151-
options=json.dumps(cur_options, indent=4)[1:-1],
152-
oath2_prop=json.dumps(cur_oath2_prop))
162+
oauth2_redirect_url=oauth2_redirect_url,
163+
options_str=json.dumps(cur_options, indent=4)[1:-1],
164+
oath2_prop_str=json.dumps(cur_oath2_prop))
153165
with open(os.path.join(page_dir, iframe_filename), 'w') as f:
154166
f.write(output_from_parsed_template)
155167
self.replace_with_iframe(soup, grouped_list[0], cur_id,
@@ -254,8 +266,6 @@ def process_options(self, config, swagger_ui_ele):
254266
if "syntaxHighlightTheme" in cur_options:
255267
cur_options["syntaxHighlight.theme"] = cur_options.pop(
256268
"syntaxHighlightTheme")
257-
if "oauth2RedirectUrl" not in cur_options:
258-
cur_options["oauth2RedirectUrl"] = urljoin(config['site_url'], "assets/swagger-ui/oauth2-redirect.html")
259269
return cur_options
260270

261271
def process_oath2_prop(self, swagger_ui_ele):

mkdocs_swagger_ui_tag/swagger-ui/swagger.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
<meta charset="UTF-8">
66
<title>Swagger UI</title>
77
<link rel="stylesheet" type="text/css" href="{{ css_dir }}swagger-ui.css" />
8-
<link rel="stylesheet" type="text/css" id="slate-css" media="none" href="{{ css_dir }}/swagger-ui-dark.css" />
8+
<link rel="stylesheet" type="text/css" id="slate-css" media="none" href="{{ css_dir }}swagger-ui-dark.css" />
99
</head>
1010

1111
<body style="overflow:hidden;background: {{background}};">
1212
<div id="swagger-ui"></div>
13-
<script src="{{ js_dir }}/swagger-ui-bundle.js" charset="UTF-8"> </script>
14-
<script src="{{ js_dir }}/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
13+
<script src="{{ js_dir }}swagger-ui-bundle.js" charset="UTF-8"> </script>
14+
<script src="{{ js_dir }}swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
1515
<script>
1616
window.onload = function () {
1717
window.ui = SwaggerUIBundle({
@@ -30,11 +30,16 @@
3030
],
3131
layout: "StandaloneLayout",
3232
{% endif %}
33-
{{options}}
33+
{% if oauth2_redirect_url.startswith('.') %}
34+
"oauth2RedirectUrl": new URL("{{oauth2_redirect_url}}",window.location.href).href,
35+
{% else %}
36+
"oauth2RedirectUrl": "{{oauth2_redirect_url}}",
37+
{% endif %}
38+
{{options_str}}
3439
})
3540

36-
{% if oath2_prop != "{}" %}
37-
window.ui.initOAuth({{oath2_prop}})
41+
{% if oath2_prop_str != "{}" %}
42+
window.ui.initOAuth({{oath2_prop_str}})
3843
{% endif %}
3944

4045
const scheme = parent.scheme

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.4.1",
7+
version="0.4.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)