集成支持
有多个为 djLint 开发的编辑器集成方案。
GitHub Actions
在 GitHub Actions 工作流中,djLint 会将每条结果输出为注解(annotation),直接显示在拉取请求的差异视图中。无需任何配置:djLint 会自动识别工作流并切换输出格式。
- run: pip install djlint
- run: djlint . --lint --profile django
使用 --no-github-output 可恢复普通的控制台输出,使用 --github-output 可在 Actions 之外强制输出注解。
注解只属于某一个拉取请求。若要把结果保存在仓库的 Security 标签页中,并保留多次运行的历史记录,可以将其输出为 SARIF 并上传该文件:
- run: djlint . --lint --sarif > djlint.sarif
continue-on-error: true
- uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: djlint.sarif
上传步骤需要在 job 或 workflow 上授予 security-events: write 权限。
--sarif 只向 stdout 输出一个 SARIF 2.1.0 文档,不输出其他内容,因此可以像上面那样重定向到文件。文档列出所有已启用的规则及其文档链接,每条结果都带有行号和列号;配合 --check 使用时,需要重新格式化的文件会归入 formatting 规则下报告。Azure DevOps、GitLab 和大多数编辑器都能读取这种格式。
Pre-Commit
djLint 可以集成到 pre-commit hook 作为代码检查工具和格式化工具。
该代码仓库为特定的 djLint 配置文件(profile)提供了多个预先配置好的 hook(这些 hook 只是预先设置了 --profile 参数,并告知 pre-commit 需要查找哪些文件扩展名):
- 使用
djlint进行代码检查并使用djlint-reformat进行代码格式化。
在未设置--profile时会查找*.html。 - 使用
djlint-django和djlint-reformat-django。
这时会查找*.html并设置--profile=django。 - 使用
djlint-jinja和djlint-reformat-jinja。
这时会查找*.j2、*.jinja或*.jinja2并设置--profile=jinja。 - 使用
djlint-nunjucks和djlint-reformat-nunjucks。
这时会查找*.njk并设置--profile=nunjucks。 - 使用
djlint-handlebars和djlint-reformat-handlebars。
这时会查找*.hbs并设置--profile=handlebars。 - 使用
djlint-liquid和djlint-reformat-liquid。
这时会查找*.html并设置--profile=liquid。 - 使用
djlint-golang和djlint-reformat-golang。
这时会查找*.gotmpl并设置--profile=golang。 - 使用
djlint-tera和djlint-reformat-tera。
这时会查找*.html并设置--profile=tera。 - 使用
djlint-askama和djlint-reformat-askama。
这时会查找*.html并设置--profile=askama。
请注意,这些预定义的 hook 在它们接受的输入方面有时会过于保守(您的模板可能使用了不同的扩展名),因此 pre-commit 明确允许您覆盖这些预定义的选项。有关额外的配置信息,请参阅 pre-commit 的文档。
默认 Django 示例
repos:
- repo: https://github.com/djlint/djLint
rev: v1.47.0
hooks:
- id: djlint-reformat-django
- id: djlint-django
使用 .html 后缀而非 .hbs 后缀的 Handlebars
repos:
- repo: https://github.com/djlint/djLint
rev: v1.47.0
hooks:
- id: djlint-reformat-handlebars
files: "\\.html"
types_or: ["html"]
- id: djlint-handlebars
files: "\\.html"
types_or: ["html"]
您可以使用 files 或 exclude 参数将每个 hook 限制在其自身的目录范围内,从而允许您在同一个代码仓库中支持多种模板语言。
SublimeText 代码检查
djLint 也可用作 SublimeText 的代码检查插件。你可以通过 package-control 安装。
cmd + shift + p- 安装 SublimeLinter
- 安装 SublimeLinter-contrib-djlint
确保 djLint 安装在你的全局 python 中,或你的 PATH 变量中。
Visual Studio Code
neovim
djLint 可以在 neovim 中检查或格式化代码。
使用 none-ls 插件。
使用 coc.nvim。
使用 efm-langserver。
- 通过 Mason 安装 efm-langserver 和 djLint:
:MasonInstall efm djlint - 将
htmldjango添加到filetypes。 - 创建格式化配置:
local djlint = {
formatCommand = "djlint --reformat --quiet --warn -",
formatStdin = true,
}
- 使用
htmldjango = { djlint }将语言连接到格式化工具。
示例配置:
local djlint = {
formatCommand = "djlint --reformat --quiet --warn -",
formatStdin = true,
}
require("lspconfig").efm.setup({
filetypes = { "htmldjango" },
init_options = { documentFormatting = true },
settings = {
rootMarkers = { ".git/" },
languages = {
htmldjango = { djlint },
},
},
})
MegaLinter
djLint 已集成到 MegaLinter 的百余个代码检查工具中。
运行 npx mega-linter-runner --install 并根据提示来安装它。