@opendevise/antora-swagger-ui-extension
Version:
An Antora extension that integrates the Swagger UI (interactive API explorer) into an Antora site through AsciiDoc syntax extensions.
27 lines (25 loc) • 1.21 kB
JavaScript
;(function (source, slot, docExpansion) {
'use strict'
function findCurrentScript () {
const candidates = document.querySelectorAll('script.swagger-ui-spec-loader')
if (!window.swaggerUiSpecLoaderScriptIndex) window.swaggeUiSpecLoaderScriptIndex = 0
return candidates[window.swaggerUiSpecLoaderScriptIndex++]
}
const onComplete = function (mainContent) {
if (!this.location.hash) return
let target = mainContent.querySelector('.swagger-ui a[href="' + this.location.hash + '"]')
while (target && !target.id) target = target.parentNode
if (!target) return
const newTop = target.getBoundingClientRect().top - mainContent.getBoundingClientRect().top
this.scrollTo({ left: 0, top: newTop, behavior: 'instant' })
}.bind(this || window, document.querySelector('main.article > .content') || document.querySelector('main.article'))
const config = (document.currentScript || findCurrentScript()).dataset
SwaggerUIBundle(
Object.assign(typeof source === 'string' ? { url: source } : { spec: source }, {
domNode: document.getElementById(config.target),
docExpansion: config.docExpansion,
deepLinking: true,
onComplete: onComplete,
})
)
})