在Swagger UI中使用自定义样式来美化你的HTML标签。

huangapple go评论70阅读模式
英文:

Use custom styles in Swagger UI for your html tags

问题

我可以在C#示例下面添加自定义的HTML信息

c.SwaggerDoc("v1", new OpenApiInfo { Description ="...在这里..."});

这里支持HTML标签,但不支持样式。

要使用自定义样式(着色的span、非默认蓝色的链接等),我必须添加自定义CSS swagger.css(可以更改名称并放在更深的文件夹结构中)

在项目根目录下的/wwwwroot/swagger.css下,将其作为嵌入资源,并在Configure方法中注入该自定义CSS

app.UseSwaggerUI(options =>
{
  options.SwaggerEndpoint("./swagger/v1/swagger.json", "v1");
  options.RoutePrefix = string.Empty;
  options.DocExpansion(DocExpansion.List);
  options.InjectStylesheet("/swagger.css");  // <<< inject css
});

其中一个限制是不支持传统的CSS,例如

//swagger.css
.rcolor {color:red !important;}

//OpenApiInfo/Description中的HTML
<a class='rcolor' href='...'>这应该是红色链接</a>

不能改变链接的颜色。

要应用颜色,应该使用Swagger UI标记导航:对您的元素应用唯一的HTML标记模式 - 而不是简单的<a>...</a>,请将<a><span>...</span></a>放置到<a>中,例如

并更改CSS(.swagger-ui、.info是与Swagger一起提供的预定义样式 - 您可以在浏览器的开发工具/检查中看到它,并且我们的OpenApiInfo/Description部分正好在这个样式.info之内)

.info a span {
    color: red !important;
}

.info span a {
    color: green !important;
}

现在这个

"&lt;pre&gt;colored samples &lt;span&gt;&lt;a id='111'&gt;span / a&lt;/a&gt;&lt;/span&gt; , &lt;a id='111'&gt;&lt;span&gt;a / span&lt;/span&gt;&lt;/a&gt; &quot;

将会看起来像

在Swagger UI中使用自定义样式来美化你的HTML标签。

请注意,.info a span不是一个好主意,因为它也为http://localhost/./swagger/v1/swagger.json链接着色 - 实际上在浏览器检查器中您可以看到它是<a>...<span>...。但您可以通过应用唯一的HTML标记嵌套来轻松修复它。

有没有更好的解决方案来自定义Swagger UI的样式?

英文:

I can add custom html info in (down below C# samples)

c.SwaggerDoc(&quot;v1&quot;, new OpenApiInfo { Description =&quot;... here ...&quot;

html tags are supported here, but styles are not.
To use custom style (colored span, link with not default blue color, etc.) I have to add custom css swagger.css (can change name and put in deeper folder structure)
in project root under /wwwwroot/swagger.css, make it embedded resource and inject that custom css in Configure method

app.UseSwaggerUI(options =&gt;
{
  options.SwaggerEndpoint(&quot;./swagger/v1/swagger.json&quot;, &quot;v1&quot;); 
  options.RoutePrefix = string.Empty;
  options.DocExpansion(DocExpansion.List);
  options.InjectStylesheet(&quot;/swagger.css&quot;);  // &lt;&lt;&lt; inject css
});

The caveat is that traditional css is not supported, for example,

//swagger.css
.rcolor {color:red !important;}

//html in OpenApiInfo/Description
&lt;a class=&#39;rcolor&#39; href=&#39;...&#39;&gt;this should be red link&lt;/a&gt;

does not change color of the link.

To apply colors swagger ui tag navigation should be used: apply unique html tag pattern to your element - instead of simple &lt;a&gt;...&lt;/a&gt; put &lt;a&gt;&lt;span&gt;...&lt;/span&gt;&lt;/a&gt; for example
and change css (.swagger-ui, .info are predefined styles which comes with swagger - you can see it in the browser dev tools/inspect and our OpenApiInfo/Description section is right inside this style .info) to

.info a span {
    color: red !important;
}

.info span a {
    color: green !important;
}

and now this

&quot;&lt;pre&gt;colored samples &lt;span&gt;&lt;a id=&#39;111&#39;&gt;span / a&lt;/a&gt;&lt;/span&gt; , &lt;a id=&#39;111&#39;&gt;&lt;span&gt;a / span&lt;/span&gt;&lt;/a&gt; &quot; 

will look like

在Swagger UI中使用自定义样式来美化你的HTML标签。

Note, that .info a span was not a good idea since it also colored http://localhost/./swagger/v1/swagger.json link - indeed in the browser inspector you can see that it is &lt;a&gt;...&lt;span&gt;.... But you can easily fix it by apply your unique html tag nesting.

Does anybody have better solution for custom styles in swagger ui?

答案1

得分: 1

Swagger UI出于安全原因在Markdown内容中忽略了HTML的styleclassdata-*属性 详情请看此链接。有一个名为useUnsafeMarkdown的配置选项用于禁用此行为,但它已被弃用,您不应该依赖它。

你提出的解决方案似乎是内联颜色样式的可行解决方法。

英文:

Swagger UI ignores HTML style, class, and data-* attributes in Markdown content for security reasons. There's the useUnsafeMarkdown configuration option to disable this behavior, but it's deprecated and you should not rely on it.

The solution you came up with looks like a viable workaround for inline color styling.

huangapple
  • 本文由 发表于 2023年6月15日 00:31:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475744.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定