@@ -2,7 +2,9 @@ package stopment
22
33import (
44 "embed"
5+ "html/template"
56 "io/fs"
7+ "strings"
68)
79
810//go:embed src/stopments/static
@@ -21,3 +23,125 @@ var Favicon, _ = files.ReadFile("src/stopments/static/favicon.ico")
2123var Styles , _ = files .ReadFile ("src/stopments/static/styles.min.css" )
2224var WebComponents , _ = files .ReadFile ("src/stopments/static/web-components.min.js" )
2325var ScalarApiReference , _ = files .ReadFile ("src/stopments/static/scalar-api-reference.js" )
26+
27+ // StoplightConfig holds the configuration for Stoplight Elements.
28+ // It allows you to customize the OpenAPI document URL, title, and other settings
29+ // for the Stoplight Elements documentation.
30+ // You can use NewConfig function to create a new StoplightConfig with default values.
31+ // The configuration can be used to generate the HTML for Stoplight Elements using GetStoplightElementsHtml function.
32+ type StoplightConfig struct {
33+ // OpenAPIURL is the URL to the OpenAPI document.
34+ // It can be a URL to a remote OpenAPI document or a local file path.
35+ OpenAPIURL string
36+
37+ // Title is the title of the API documentation.
38+ Title string
39+
40+ // StoplightElementsJSURL is the URL to the Stoplight Elements JavaScript file.
41+ // Default is "https://cdn.jsdelivr.net/npm/@stoplight/elements/web-components.min.js"
42+ StoplightElementsJSURL string
43+
44+ // StoplightElementsCSSURL is the URL to the Stoplight Elements CSS file.
45+ // Default is "https://cdn.jsdelivr.net/npm/@stoplight/elements/styles.min.css"
46+ StoplightElementsCSSURL string
47+
48+ // StoplightElementsFavicon is the URL to the favicon for Stoplight Elements.
49+ // Default is "https://docs.stoplight.io/favicons/favicon.ico"
50+ StoplightElementsFavicon string
51+
52+ // APIDescriptionDocument is the API description document in JSON or YAML format.
53+ APIDescriptionDocument string
54+
55+ // BasePath is the base path for the API.
56+ BasePath string
57+
58+ // HideInternal indicates whether to hide internal APIs in the documentation.
59+ HideInternal bool
60+
61+ // HideTryIt indicates whether to hide the "Try It" feature in the documentation.
62+ HideTryIt bool
63+
64+ // HideExport indicates whether to hide the "Export" feature in the documentation.
65+ HideExport bool
66+
67+ // TryItCORSProxy is the CORS proxy URL for the "Try It" feature.
68+ TryItCORSProxy string
69+
70+ // TryItCredentialPolicy defines the credential policy for the "Try It" feature.
71+ // default is "omit".
72+ // Possible values are "omit", "include", and "same-origin".
73+ TryItCredentialPolicy string // "omit", "include", "same-origin"
74+
75+ // Layout defines the layout of the Stoplight Elements documentation.
76+ // Default is "sidebar".
77+ // Possible values are "sidebar", "responsive", and "stacked".
78+ Layout string // "sidebar", "responsive", "stacked"
79+
80+ // Logo is the URL to the logo for the API documentation.
81+ // It can be a URL to an image or a local file path.
82+ // Default is an empty string, which means no logo is displayed.
83+ Logo string
84+
85+ // Router defines the routing strategy for Stoplight Elements.
86+ // Default is "hash".
87+ // Possible values are "history", "hash", "memory", and "static".
88+ Router string // "history", "hash", "memory", "static"
89+ }
90+
91+ // NewConfig creates a new StoplightConfig with default values for Stoplight Elements.
92+ func NewConfig (openapiURL string , title string ) StoplightConfig {
93+ return StoplightConfig {
94+ OpenAPIURL : openapiURL ,
95+ Title : title ,
96+ StoplightElementsJSURL : "https://cdn.jsdelivr.net/npm/@stoplight/elements/web-components.min.js" ,
97+ StoplightElementsCSSURL : "https://cdn.jsdelivr.net/npm/@stoplight/elements/styles.min.css" ,
98+ StoplightElementsFavicon : "https://docs.stoplight.io/favicons/favicon.ico" ,
99+ TryItCredentialPolicy : "omit" ,
100+ Layout : "sidebar" ,
101+ Router : "hash" ,
102+ }
103+ }
104+
105+ const stoplightElementsTemplate = `<!doctype html>
106+ <html lang="en">
107+ <head>
108+ <meta charset="utf-8">
109+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
110+ <title>{{.Title}}</title>
111+ {{if .StoplightElementsFavicon}}<link rel="shortcut icon" href="{{.StoplightElementsFavicon}}">{{end}}
112+ <script src="{{.StoplightElementsJSURL}}"></script>
113+ <link rel="stylesheet" href="{{.StoplightElementsCSSURL}}">
114+ </head>
115+ <body>
116+ <elements-api
117+ {{if .OpenAPIURL}}apiDescriptionUrl="{{.OpenAPIURL}}"{{end}}
118+ {{if .APIDescriptionDocument}}apiDescriptionDocument="{{.APIDescriptionDocument}}"{{end}}
119+ {{if .BasePath}}basePath="{{.BasePath}}"{{end}}
120+ {{if .HideInternal}}hideInternal="true"{{end}}
121+ {{if .HideTryIt}}hideTryIt="true"{{end}}
122+ {{if .HideExport}}hideExport="true"{{end}}
123+ {{if .TryItCORSProxy}}tryItCorsProxy="{{.TryItCORSProxy}}"{{end}}
124+ tryItCredentialPolicy="{{.TryItCredentialPolicy}}"
125+ layout="{{.Layout}}"
126+ {{if .Logo}}logo="{{.Logo}}"{{end}}
127+ router="{{.Router}}"
128+ />
129+ </body>
130+ </html>`
131+
132+ // GetStoplightElementsHtml generates the HTML for Stoplight Elements based on the provided configuration.
133+ // You can get config using NewConfig function.
134+ // It returns the HTML as a string or an error if the template execution fails.
135+ func GetStoplightElementsHtml (cfg StoplightConfig ) (string , error ) {
136+ tmpl , err := template .New ("stoplight" ).Parse (stoplightElementsTemplate )
137+ if err != nil {
138+ return "" , err
139+ }
140+ var builder strings.Builder
141+
142+ err = tmpl .Execute (& builder , cfg )
143+ if err != nil {
144+ return "" , err
145+ }
146+ return builder .String (), nil
147+ }
0 commit comments