feat(docs): enhance Scalar renderer with custom script and font sourc… - #1107
feat(docs): enhance Scalar renderer with custom script and font sourc…#1107Orfeo42 wants to merge 2 commits into
Conversation
|
Hi @Orfeo42, thanks for your contribution. We specifically want to avoid registering renderer-specific docs variables (e.g. I've admittedly only skimmed through this code and haven't tested it locally, but could you walk through some of the design decisions please? |
|
Hi @wolveix, and thanks for the review: I understand the suspicion and yes I used some AI during develop, mostly by making it review some part that i could have missed and some research during develop, not for the full implementation, and the part written by AI are manually reviewed!
The config passed to config.DocsRendererConfig = map[string]any{
"sources": []map[string]any{
{"url": "/openapi.json", "title": "Main API"},
{"url": "/admin/openapi.json", "title": "Admin API"},
},
}Since the init is now an inline script, I didn't want to add To be transparent, the breaking parts of this are two: If you like it better i can draft a parallel path or something similar to keep it backward compatible (add more complexity in my opinion) The non-object panic in my opinion prevent for shipping brokend documentation, but if you prefer i can skip this. The multi document part is obviously something that I added just for myself, so if you prefer to avoid keeping this part to avoid maintaining it I can understand I tested it locally in a production project and it worked (multiple huma api and goframe api rendered too in the same doc). Anyway feel free to push back as match as you like I'm not offended in any way I like to give a contribution if is possible. Again thank you for your time |
Closes: #1103
Summary
@scalar/api-reference@1.44.20to1.66.1with a fresh SRI hash, and addConfig.DocsScalar(ScalarDocsConfigwithScriptURL,ScriptIntegrity,FontSrc) to override the script location and font source without waiting for a huma release. Override values are validated at registration (rejecting;,,, quotes, whitespace,<,>) to prevent CSP and HTML injection.data-configurationattribute to theScalar.createApiReference('#app', {...})JS API, allowed via a CSPsha256hash of the static inline script. This makes the full Scalar configuration work throughDocsRendererConfig, including multi-documentsources; huma injects the defaulturlonly when the config declares none ofurl,sources, orcontent.'unsafe-eval'fromscript-src(verified unused by 1.66.1 in-browser) and addfont-srcforhttps://fonts.scalar.com(previously blocked by thedefault-src 'none'fallback, breaking font loading).style-src 'unsafe-inline'remains — Scalar applies inlinestyleattributes, which no nonce or hash can allow; tracked by the existing TODO.html.EscapeStringand precompute the CSP header once at registration inregisterDocsRoute(api.go).registerDocsRouteinto focused helpers:scalarScriptFor,scalarFontSrc,scalarConfigJSON,scalarCSP,scalarPage.TestDocsRendererswith coverage for custom script URL/integrity, font-src override, multi-document config, config-shape validation, CSP/comma injection rejection, and title escaping.Breaking Changes
DocsRendererConfigfor the Scalar renderer must now marshal to a JSON object; any other JSON shape panics at API creation (previously any JSON value was serialized intodata-configuration).<script id="api-reference" data-url=... data-configuration=...>element; anything scraping or post-processing that markup must adapt to the new<div id="app">+ inlineScalar.createApiReferencestructure.