Skip to content

experiment: Swagger UI #12

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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v23.11.1
6 changes: 4 additions & 2 deletions about.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"about_url": "https://github.com/grid-x/discourse-rapidoc-theme-component",
"license_url": "https://github.com/grid-x/discourse-rapidoc-theme-component/blob/main/LICENSE",
"assets": {
"rapidoc_js": "assets/rapidoc-9.3.8.min.js"
"swagger_ui_bundle_js": "assets/swagger-ui-bundle.js",
"swagger_ui_standalone_preset_js": "assets/swagger-ui-standalone-preset.js",
"swagger_ui_css": "assets/swagger-ui.css"
},
"name": "Discourse Rapidoc",
"name": "Discourse Swagger UI",
"component": true
}
3,914 changes: 0 additions & 3,914 deletions assets/rapidoc-9.3.8.min.js

This file was deleted.

2 changes: 2 additions & 0 deletions assets/swagger-ui-bundle.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions assets/swagger-ui-standalone-preset.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions common/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
font-size: 0;
}
}
@import "swagger-ui.css";
3 changes: 3 additions & 0 deletions common/swagger-ui.css

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { apiInitializer } from "discourse/lib/api";
import loadScript from "discourse/lib/load-script";

async function applySwaggerUi(element) {
const apidocs = element.querySelectorAll("pre[data-code-wrap=swagger]");

if (!apidocs.length) {
return;
}

await loadScript(settings.theme_uploads_local.swagger_ui_bundle_js);
await loadScript(settings.theme_uploads_local.swagger_ui_standalone_preset_js);

const theme =
getComputedStyle(document.body).getPropertyValue("--scheme-type").trim() ===
"dark"
? "dark"
: "default";

apidocs.forEach((apidoc) => {
if (apidoc.dataset.processed) {
return;
}
});
let i = 0;
apidocs.forEach((apidoc) => {
const codeBlock = apidoc.querySelector("code");

if (!codeBlock) {
return;
}
i++;

const promise = new Promise((resolve) => resolve(codeBlock.textContent));
promise
.then((spec) => {
apidoc.outerHTML = `
<div id="swagger-ui-${i}"></div>
`;
return spec;
}).then((spec)=>{
const ui = SwaggerUIBundle({
url: `${spec}`,
dom_id: `#swagger-ui-${i}`,
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
]
})

window.ui = ui
})
.catch((e) => {
apidoc.innerText = e?.message || e;
})
.finally(() => {
apidoc.dataset.processed = true;
});
});
}

export default apiInitializer("1.13.0", (api) => {
api.decorateCookedElement(
async (elem, helper) => {
const id = helper ? `post_${helper.getModel().id}` : "composer";
applySwaggerUi(elem, id);
},
{ id: "discourse-swagger-theme-component" }
);
});
Loading
Loading