Swagger UI 提供了多种自定义主题的方式,以下是在 Linux 系统中实现自定义主题的几种方法:
下载预构建主题:
wget https://github.com/ostranme/swagger-ui-themes/archive/refs/heads/master.zip
unzip master.zip
引入主题CSS: 在你的 Swagger UI HTML 文件中,替换默认的 CSS 文件:
<link rel="stylesheet" href="path/to/swagger-ui-themes-master/themes/3.x/theme-material.css" />
可用的预构建主题包括:theme-material.css, theme-monokai.css, theme-muted.css, theme-flattop.css, theme-newspaper.css
创建自定义 CSS 文件:
mkdir -p /var/www/swagger/css
nano /var/www/swagger/css/custom-theme.css
添加自定义样式:
/* 示例自定义样式 */
.swagger-ui {
font-family: "Open Sans", sans-serif;
}
.opblock-summary {
background-color: #4a4a4a;
color: white;
}
.topbar {
background-color: #003366;
}
在 HTML 中引入:
<link rel="stylesheet" href="css/custom-theme.css" />
安装必要的 Node.js 工具:
sudo apt update
sudo apt install nodejs npm
npm install swagger-ui-themes
创建自定义主题文件:
const { SwaggerTheme } = require('swagger-themes');
const theme = new SwaggerTheme();
const darkStyle = theme.getBuffer('dark');
// 将 darkStyle 写入文件或直接使用
克隆 Swagger UI 仓库:
git clone https://github.com/swagger-api/swagger-ui.git
cd swagger-ui
npm install
修改 src/style/_variables.scss 文件中的变量值
重新构建:
npm run build
如果你通过 Nginx 提供 Swagger UI 服务,可以这样配置:
server {
listen 80;
server_name api.example.com;
location / {
root /var/www/swagger;
index index.html;
}
location /css/ {
alias /var/www/swagger/css/;
}
}
重启你的 web 服务器:
sudo systemctl restart nginx
访问你的 Swagger UI 页面,检查主题是否已应用
希望这些方法能帮助你在 Linux 系统中成功自定义 Swagger UI 主题!