Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple macros on the same page #640

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/swagger-ui-block-macro.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const buildSwaggerUi = ({ specUrl, bundleUrl }) => `<link rel="stylesheet" href="${bundleUrl}/swagger-ui.css">
const buildSwaggerUi = ({ specUrl, bundleUrl, divID }) => `<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}">
<script id="swagger-ui-script-${divID}" data-url="${specUrl}">
;(function (config) {
var initialHash = window.location.hash
var jumpToAnchorOnLoad = function () {
Expand Down Expand Up @@ -34,10 +34,10 @@ const buildSwaggerUi = ({ specUrl, bundleUrl }) => `<link rel="stylesheet" href=
} else {
requestAnimationFrame(jumpToAnchorOnLoad)
}
}.bind(document.getElementById('swagger-ui'))
}.bind(document.getElementById('swagger-ui-${divID}'))
SwaggerUIBundle({
url: config.url,
dom_id: '#swagger-ui',
dom_id: '#swagger-ui-${divID}',
deepLinking: true,
presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
plugins: [SwaggerUIBundle.plugins.DownloadUrl],
Expand All @@ -50,9 +50,11 @@ const buildSwaggerUi = ({ specUrl, bundleUrl }) => `<link rel="stylesheet" href=
if (initialHash.charAt(1) !== '/') window.location.hash = ''
jumpToAnchorOnLoad()
}
})(document.getElementById('swagger-ui-script').dataset)
})(document.getElementById('swagger-ui-script-${divID}').dataset)
</script>`

var divID = 0;

function blockSwaggerUiMacro ({ file }) {
return function () {
this.process((parent, specUrl, attrs) => {
Expand All @@ -64,9 +66,10 @@ function blockSwaggerUiMacro ({ file }) {
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>')
divID++
const contentScripts = buildSwaggerUi({ specUrl, bundleUrl, divID })
file.asciidoc.attributes['page-content-scripts'] += contentScripts
return this.createBlock(parent, 'pass', '<div id="swagger-ui-' + divID + '"></div>')
})
}
}
Expand Down