#[must_use]
pub fn render_viewer_html(spec_url: &str, opts: &ViewerOptions<'_>) -> String {
let title = opts.title;
let cdn = opts.cdn_base.trim_end_matches('/');
let spec = spec_url.replace('"', """);
format!(
"<!DOCTYPE html>\n\
<html lang=\"en\">\n\
<head>\n\
<meta charset=\"utf-8\" />\n\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n\
<title>{title}</title>\n\
<link rel=\"stylesheet\" href=\"{cdn}/styles/default.min.css\" />\n\
</head>\n\
<body>\n\
<div id=\"asyncapi\"></div>\n\
<script src=\"{cdn}/browser/standalone/index.js\"></script>\n\
<script>\n\
AsyncApiStandalone.render(\n\
{{ schema: {{ url: \"{spec}\" }}, config: {{ show: {{ sidebar: true }} }} }},\n\
document.getElementById(\"asyncapi\"),\n\
);\n\
</script>\n\
</body>\n\
</html>\n"
)
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ViewerOptions<'a> {
pub title: &'a str,
pub cdn_base: &'a str,
}
impl<'a> ViewerOptions<'a> {
#[must_use]
pub const fn title(mut self, title: &'a str) -> Self {
self.title = title;
self
}
#[must_use]
pub const fn cdn_base(mut self, cdn_base: &'a str) -> Self {
self.cdn_base = cdn_base;
self
}
}
impl Default for ViewerOptions<'_> {
fn default() -> Self {
Self {
title: "AsyncAPI",
cdn_base: "https://cdn.jsdelivr.net/npm/@asyncapi/react-component@3.1.8",
}
}
}