diff --git a/_examples/multi_ui/api.go b/_examples/multi_ui/api.go
index f240f97..a724be7 100644
--- a/_examples/multi_ui/api.go
+++ b/_examples/multi_ui/api.go
@@ -11,22 +11,60 @@ func (a *api) SetupRoutes(r chi.Router) error {
var apiDef = chioas.Definition{
DocOptions: chioas.DocOptions{
- ServeDocs: true, // makes docs served as interactive UI on /docs/index.htm
- UIStyle: chioas.Redoc,
+ ServeDocs: true, // makes docs served as interactive UI on /docs/index.htm
+ UIStyle: chioas.Redoc,
+ StylesOverride: styling,
+ RedocOptions: chioas.RedocOptions{
+ HeaderHtml: `
`,
+ },
AlternateUIDocs: chioas.AlternateUIDocs{
"/swagger": {
- UIStyle: chioas.Swagger,
+ UIStyle: chioas.Swagger,
+ StylesOverride: styling,
SwaggerOptions: chioas.SwaggerOptions{
DeepLinking: true,
+ HeaderHtml: ``,
},
},
"/rapidoc": {
- UIStyle: chioas.Rapidoc,
+ UIStyle: chioas.Rapidoc,
+ StylesOverride: styling,
RapidocOptions: &chioas.RapidocOptions{
ShowHeader: true,
HeadingText: "Petstore",
Theme: "dark",
- ShowMethodInNavBar: "false",
+ ShowMethodInNavBar: "as-colored-block",
+ UsePathInNavBar: true,
+ SchemaStyle: "table",
+ HeadScript: `
+function getRapiDoc(){
+ return document.getElementById("thedoc");
+}
+function toggleView() {
+ let currRender = getRapiDoc().getAttribute('render-style');
+ let newRender = currRender === "read" ? "view" : "read";
+ getRapiDoc().setAttribute('render-style', newRender );
+}
+function toggleTheme(){
+ if (getRapiDoc().getAttribute('theme') === 'dark'){
+ getRapiDoc().setAttribute('theme',"light");
+ }
+ else{
+ getRapiDoc().setAttribute('theme',"dark");
+ }
+}`,
+ InnerHtml: ``,
},
},
},
@@ -80,6 +118,26 @@ Store](https://github.com/swagger-api/swagger-petstore/blob/master/src/main/reso
Components: &components,
}
+const styling = `.chioasbtn{
+ min-width: 100px;
+ background-color: #47AFE8;
+ color: #fff;
+ font-size: 12px;
+ display: block;
+ border: none;
+ margin: 2px;
+ border-radius: 2px;
+ cursor: pointer;
+ outline: none;
+ text-align: center;
+ padding: 4px;
+ text-decoration: none;
+ font-family: sans-serif;
+}
+.chioasbtn:visited{
+ color: #fff;
+}`
+
var components = chioas.Components{
Schemas: chioas.Schemas{
{
diff --git a/_examples/petstore/api.go b/_examples/petstore/api.go
index 911b249..7829b66 100644
--- a/_examples/petstore/api.go
+++ b/_examples/petstore/api.go
@@ -17,19 +17,24 @@ import (
//go:embed status_schema.yaml petstore-logo.png
var supportFilesFS embed.FS
-// we're using our own customized docs html template
-//
-//go:embed index_template.html
-var customizedSwaggerTemplate string
-
var apiDef = chioas.Definition{
DocOptions: chioas.DocOptions{
- ServeDocs: true, // makes docs served as interactive UI on /docs/index.htm
- UIStyle: chioas.Swagger,
+ ServeDocs: true, // makes docs served as interactive UI on /docs/index.htm
+ UIStyle: chioas.Swagger,
+ StylesOverride: `.logo img {
+ padding: inherit;
+ margin: auto;
+ width: 200px;
+ display: block;
+}`,
+ SwaggerOptions: chioas.SwaggerOptions{
+ HeaderHtml: `
+
+
`,
+ },
SupportFiles: http.FileServer(http.FS(supportFilesFS)),
SupportFilesStripPrefix: true,
- DocTemplate: customizedSwaggerTemplate, // customized template to show logo
- CheckRefs: true, // make sure that any $ref's are valid!
+ CheckRefs: true, // make sure that any $ref's are valid!
},
Info: chioas.Info{
Title: "Swagger Petstore - OpenAPI 3.0",
@@ -126,44 +131,3 @@ func commenter(handlerMethod string, comments ...string) []string {
func (a *api) SetupRoutes(r chi.Router) error {
return a.Definition.SetupRoutes(r, a)
}
-
-/*
-// customizing the html template (to show a logo) - copied from chioas/doc_options.go defaultSwaggerTemplate
-const customizedSwaggerTemplate = `
-
-
- {{.title}}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-`
-*/
diff --git a/_examples/petstore/index_template.html b/_examples/petstore/index_template.html
deleted file mode 100644
index c791d19..0000000
--- a/_examples/petstore/index_template.html
+++ /dev/null
@@ -1,38 +0,0 @@
-
-
-
-
- {{.title}}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/doc_options.go b/doc_options.go
index 1881f35..582041c 100644
--- a/doc_options.go
+++ b/doc_options.go
@@ -194,6 +194,7 @@ func (d *DocOptions) getTemplateData() (specName string, data map[string]any) {
htmlTagSwaggerPlugins: plugins,
htmlTagFavIcons: optionsFavIcons(d.SwaggerOptions),
}
+ addHeaderAndScripts(data, swaggerOpts)
case Rapidoc:
data = optionsToMap(d.RapidocOptions)
data[htmlTagTitle] = defValue(d.Title, defaultTitle)
@@ -201,19 +202,36 @@ func (d *DocOptions) getTemplateData() (specName string, data map[string]any) {
data[htmlTagSpecName] = specName
data[htmlTagFavIcons] = optionsFavIcons(d.RapidocOptions)
default:
+ redocOpts := optionsToMap(d.RedocOptions)
data = map[string]any{
htmlTagTitle: defValue(d.Title, defaultTitle),
htmlTagStylesOverride: template.CSS(defValue(d.StylesOverride, defaultRedocStylesOverride)),
htmlTagSpecName: specName,
- htmlTagRedocOpts: optionsToMap(d.RedocOptions),
+ htmlTagRedocOpts: redocOpts,
htmlTagRedocUrl: defValue(d.RedocJsUrl, defaultRedocJsUrl),
htmlTagTryUrl: defValue(d.TryJsUrl, defaultTryJsUrl),
htmlTagFavIcons: optionsFavIcons(d.RedocOptions),
}
+ addHeaderAndScripts(data, redocOpts)
}
return
}
+func addHeaderAndScripts(data, opts map[string]any) {
+ if v, ok := opts[htmlTagHeaderHtml]; ok {
+ data[htmlTagHeaderHtml] = v
+ delete(opts, htmlTagHeaderHtml)
+ }
+ if v, ok := opts[htmlTagHeadScript]; ok {
+ data[htmlTagHeadScript] = v
+ delete(opts, htmlTagHeadScript)
+ }
+ if v, ok := opts[htmlTagBodyScript]; ok {
+ data[htmlTagBodyScript] = v
+ delete(opts, htmlTagBodyScript)
+ }
+}
+
func setupCachedRoutes(def *Definition, asJson bool, specData []byte, docsRoute *chi.Mux, tmp *template.Template, inData map[string]any, indexPage, specName string) (err error) {
indexData, err := buildIndexData(tmp, inData)
if err != nil {
@@ -385,6 +403,9 @@ const (
htmlTagSwaggerPresets = "swaggerpresets"
htmlTagSwaggerPlugins = "swaggerplugins"
htmlTagFavIcons = "favIcons"
+ htmlTagHeadScript = "headScript"
+ htmlTagBodyScript = "bodyScript"
+ htmlTagHeaderHtml = "headerHtml"
)
func defValue(v, def string) string {
diff --git a/doc_options_test.go b/doc_options_test.go
index 09f603d..ace0b39 100644
--- a/doc_options_test.go
+++ b/doc_options_test.go
@@ -497,6 +497,9 @@ func TestDocOptions_SwaggerOptions(t *testing.T) {
DeepLinking: true,
Plugins: []SwaggerPlugin{"MyPlugin1", "MyPlugin2"},
Presets: []SwaggerPreset{"MyPreset1", "MyPreset2"},
+ HeaderHtml: `HEADER
`,
+ HeadScript: `head();`,
+ BodyScript: `body();`,
},
},
}
@@ -516,6 +519,9 @@ func TestDocOptions_SwaggerOptions(t *testing.T) {
assert.Contains(t, data, `"url":"spec.yaml"`)
assert.Contains(t, data, `cfg.presets = [MyPreset1,MyPreset2]`)
assert.Contains(t, data, `cfg.plugins = [MyPlugin1,MyPlugin2]`)
+ assert.Contains(t, data, `HEADER
`)
+ assert.Contains(t, data, ``)
+ assert.Contains(t, data, ``)
expectedSupportFiles := map[string]string{
"favicon-16x16.png": "image/png",
diff --git a/docs_alt_ui.go b/docs_alt_ui.go
index ea2b481..ebc934f 100644
--- a/docs_alt_ui.go
+++ b/docs_alt_ui.go
@@ -108,6 +108,7 @@ func (d *AlternateUIDoc) getTemplateData() (specName string, data map[string]any
htmlTagSwaggerPlugins: plugins,
htmlTagFavIcons: optionsFavIcons(d.SwaggerOptions),
}
+ addHeaderAndScripts(data, swaggerOpts)
case Rapidoc:
data = optionsToMap(d.RapidocOptions)
data[htmlTagTitle] = defValue(d.Title, defaultTitle)
@@ -115,15 +116,17 @@ func (d *AlternateUIDoc) getTemplateData() (specName string, data map[string]any
data[htmlTagSpecName] = specName
data[htmlTagFavIcons] = optionsFavIcons(d.RapidocOptions)
default:
+ redocOpts := optionsToMap(d.RedocOptions)
data = map[string]any{
htmlTagTitle: defValue(d.Title, defaultTitle),
htmlTagStylesOverride: template.CSS(defValue(d.StylesOverride, defaultRedocStylesOverride)),
htmlTagSpecName: specName,
- htmlTagRedocOpts: optionsToMap(d.RedocOptions),
+ htmlTagRedocOpts: redocOpts,
htmlTagRedocUrl: defValue(d.RedocJsUrl, defaultRedocJsUrl),
htmlTagTryUrl: defValue(d.TryJsUrl, defaultTryJsUrl),
htmlTagFavIcons: optionsFavIcons(d.RedocOptions),
}
+ addHeaderAndScripts(data, redocOpts)
}
return
}
diff --git a/ui_rapidoc_options.go b/ui_rapidoc_options.go
index acabd76..fca1db6 100644
--- a/ui_rapidoc_options.go
+++ b/ui_rapidoc_options.go
@@ -17,12 +17,14 @@ const defaultRapidocTemplate = `
{{.favIcons}}
+ {{.headScript}}
{{.logo}}
{{.innerHtml}}
+ {{.bodyScript}}