-
Notifications
You must be signed in to change notification settings - Fork 94
/
swagger-ui-block-macro.js
78 lines (75 loc) · 2.94 KB
/
swagger-ui-block-macro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const buildSwaggerUi = ({ specUrl, bundleUrl }) => `<link rel="stylesheet" href="${bundleUrl}/swagger-ui.css">
<script src="${bundleUrl}/swagger-ui-bundle.js"></script>
<script src="${bundleUrl}/swagger-ui-standalone-preset.js"></script>
<script id="swagger-ui-script" data-url="${specUrl}">
;(function (config) {
var initialHash = window.location.hash
var jumpToAnchorOnLoad = function () {
if (this.querySelector('.swagger-ui .swagger-ui')) {
var targetHash, targetId
if (initialHash.charAt(1) === '/') {
targetId = 'operations' + (initialHash.lastIndexOf('/') === 1 ? '-tag' : '') + initialHash.slice(1).replace(/[/]/g, '-')
} else {
targetHash = initialHash
}
Array.prototype.slice.call(this.querySelectorAll('.opblock-tag[id], .opblock[id]')).forEach(function (el) {
var id = el.id
el.removeAttribute('id')
if (targetId === id) targetHash = '#' + targetId
if (el.tagName === 'H4') {
el.parentNode.id = id
} else {
var wrapper = document.createElement('div')
wrapper.id = id
el.parentNode.insertBefore(wrapper, el)
wrapper.appendChild(el)
}
})
if (targetHash) {
setTimeout(function () {
window.location.hash = targetHash
if (targetId) window.history.pushState(null, null, initialHash)
}, 1250)
}
} else {
requestAnimationFrame(jumpToAnchorOnLoad)
}
}.bind(document.getElementById('swagger-ui'))
SwaggerUIBundle({
url: config.url,
dom_id: '#swagger-ui',
deepLinking: true,
presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
plugins: [SwaggerUIBundle.plugins.DownloadUrl],
layout: 'StandaloneLayout',
tagsSorter: 'alpha',
operationsSorter: 'alpha',
docExpansion: 'none',
})
if (initialHash && (initialHash.charAt(1) !== '/' || initialHash.length > 2)) {
if (initialHash.charAt(1) !== '/') window.location.hash = ''
jumpToAnchorOnLoad()
}
})(document.getElementById('swagger-ui-script').dataset)
</script>`
function blockSwaggerUiMacro ({ file }) {
return function () {
this.process((parent, specUrl, attrs) => {
const doc = parent.getDocument()
if (~specUrl.indexOf('{')) {
specUrl = doc.$sub_attributes(specUrl, Opal.hash({ attribute_missing: 'drop-line' }))
if (!specUrl) return
}
const bundleUrl = specUrl.startsWith('https://s3.amazonaws.com/cb-docs-swagger/')
? 'https://cb-docs-swagger.s3.amazonaws.com/dist3'
: 'https://couchbase-docs.s3.amazonaws.com/assets/swagger-ui-3.7'
const contentScripts = buildSwaggerUi({ specUrl, bundleUrl })
file.asciidoc.attributes['page-content-scripts'] = contentScripts
return this.createBlock(parent, 'pass', '<div id="swagger-ui"></div>')
})
}
}
function register (registry, context) {
registry.blockMacro('swagger_ui', blockSwaggerUiMacro(context))
}
module.exports.register = register