openapi: make it run for file:// protocol #806

This commit is contained in:
Sören Weber 2024-03-17 00:27:38 +01:00
parent 567b930220
commit b18bca9172
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
2 changed files with 16 additions and 7 deletions

View file

@ -4,18 +4,21 @@
{{- warnf "%q: DEPRECATED parameter 'context' for shortcode 'openapi' found, use 'page' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#5180" $page.File.Filename }} {{- warnf "%q: DEPRECATED parameter 'context' for shortcode 'openapi' found, use 'page' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#5180" $page.File.Filename }}
{{- end }} {{- end }}
{{- $src := .src }} {{- $src := .src }}
{{- $spec := "" }}
{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $page) .id }} {{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $page) .id }}
{{- with $page }} {{- with $page }}
{{- with .Resources.Match $src }} {{- with or
{{- range . }} (.Resources.Get $src)
{{- $src = .RelPermalink }} (resources.Get $src)
{{- end }} }}
{{- $spec = .Content }}
{{- end }} {{- end }}
<div class="sc-openapi-wrapper is-loading helper-loading-container"> <div class="sc-openapi-wrapper is-loading helper-loading-container">
<div <div
class="sc-openapi-container" class="sc-openapi-container"
id="R-openapi-{{ $id }}" id="R-openapi-{{ $id }}"
data-openapi-url="{{ $src }}" data-openapi-url="{{ $src }}"
data-openapi-spec="{{ $spec | safeHTMLAttr }}"
></div> ></div>
</div> </div>
{{- .Store.Set "hasOpenApi" true }} {{- .Store.Set "hasOpenApi" true }}

View file

@ -446,7 +446,7 @@ function initOpenapi( update, attrs ){
const openapiPromise = new Promise( function(resolve){ resolve() }); const openapiPromise = new Promise( function(resolve){ resolve() });
openapiPromise openapiPromise
.then( function(){ .then( function(){
SwaggerUIBundle({ var options = {
defaultModelsExpandDepth: 2, defaultModelsExpandDepth: 2,
defaultModelExpandDepth: 2, defaultModelExpandDepth: 2,
docExpansion: isPrint || attrs.isPrintPreview ? 'full' : 'list', docExpansion: isPrint || attrs.isPrintPreview ? 'full' : 'list',
@ -470,9 +470,15 @@ function initOpenapi( update, attrs ){
activated: true, activated: true,
theme: swagger_code_theme, theme: swagger_code_theme,
}, },
url: oc.dataset.openapiUrl,
validatorUrl: 'none', validatorUrl: 'none',
}); };
if( oc.dataset.openapiSpec ){
Object.assign( options, { spec: JSON.parse( oc.dataset.openapiSpec ) });
}
else{
Object.assign( options, { url: oc.dataset.openapiUrl });
}
SwaggerUIBundle( options );
}) })
.then( function(){ .then( function(){
let observerCallback = function () { let observerCallback = function () {