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 }}
{{- end }}
{{- $src := .src }}
{{- $spec := "" }}
{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $page) .id }}
{{- with $page }}
{{- with .Resources.Match $src }}
{{- range . }}
{{- $src = .RelPermalink }}
{{- end }}
{{- with or
(.Resources.Get $src)
(resources.Get $src)
}}
{{- $spec = .Content }}
{{- end }}
<div class="sc-openapi-wrapper is-loading helper-loading-container">
<div
class="sc-openapi-container"
id="R-openapi-{{ $id }}"
data-openapi-url="{{ $src }}"
data-openapi-spec="{{ $spec | safeHTMLAttr }}"
></div>
</div>
{{- .Store.Set "hasOpenApi" true }}

View file

@ -446,7 +446,7 @@ function initOpenapi( update, attrs ){
const openapiPromise = new Promise( function(resolve){ resolve() });
openapiPromise
.then( function(){
SwaggerUIBundle({
var options = {
defaultModelsExpandDepth: 2,
defaultModelExpandDepth: 2,
docExpansion: isPrint || attrs.isPrintPreview ? 'full' : 'list',
@ -470,9 +470,15 @@ function initOpenapi( update, attrs ){
activated: true,
theme: swagger_code_theme,
},
url: oc.dataset.openapiUrl,
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(){
let observerCallback = function () {