設定
設定寫在專案的 pyproject.toml、djlint.toml 或 .djlint.toml,或是 .djlintrc 中。命令列選項會覆寫設定檔,但旗標只能開啟選項,因此無法關閉設定檔中開啟的開關。除非設定了 --prefer-configuration,否則專案的設定會覆寫以 --configuration 指定的全域設定檔。
djLint 會從它檢查的第一個路徑(對 - 而言是 --stdin-filename 所在的資料夾,未指定時則是目前目錄)往上尋找這些檔案。它會停在第一個含有 djlint.toml、.djlint.toml、.djlintrc、.djlint_rules.yaml 或帶有 [tool.djlint] 的 pyproject.toml 的資料夾,而且不會越過儲存庫的根目錄(含有 .git 資料夾或檔案,或 .hg 資料夾的資料夾)。沒有 djLint 設定的 pyproject.toml 不會讓它停下,因此 monorepo 中有自己 pyproject.toml 的套件也會套用儲存庫的設定。djLint 只讀取它停下的資料夾:先讀 pyproject.toml 中的 [tool.djlint](其中有設定時),沒有就讀 djlint.toml 或 .djlint.toml,再沒有就讀 .djlintrc;同一資料夾中沒有被讀取的檔案會發出警告。
命令列和設定都沒有設定 indent 或 max_line_length 時,djLint 會像編輯器一樣從 .editorconfig 檔案讀取 indent_size 或 max_line_length:每個檔案依自己的路徑取值,最近的 .editorconfig 優先,上層的檔案補上缺少的值,直到遇到含有 root = true 的檔案或到達專案根目錄。
pyproject.toml 的格式為 toml。
[tool.djlint]profile = "django"ignore = ["H014", "H015"]djlint.toml 和 .djlint.toml 的格式為 toml。
profile = "django"ignore = ["H014", "H015"].djlintrc 的格式為 json。
{ "profile": "django", "ignore": ["H014", "H015"] }像 ignore 這樣有多個值的選項接受清單。也可以寫成以逗號分隔的字串,例如 "H014,H015",djLint 1.41 之前的版本只認得這種寫法。
從 1.51 版起,遇到不認得的設定或規則代碼時,djLint 會發出警告並列出最接近的已知項目。
找不到任何結果,請試試其他搜尋。
allow_empty_input
預設值: false
當指定的路徑完全沒有比對到任何檔案時,以 0 而不是 2 結束。已找到、但隨後被 exclude、extend_exclude、use_gitignore 或 require_pragma 略過的檔案,本來就會以 0 結束。
allow_empty_input=trueallow_empty_input=true"allow_empty_input": true--allow-empty-inputblank_line_after_tag
預設值: 無
要在後面加上空白行的範本標籤,例如 load 和 extends。連續的一組這類標籤只會在最後一個之後加上空白行,區塊的結尾則不會加上空白行。
blank_line_after_tag=["load", "extends", "include"]blank_line_after_tag=["load", "extends", "include"]"blank_line_after_tag": ["load", "extends", "include"]--blank-line-after-tag "load,extends,include"blank_line_before_tag
預設值: 無
要在前面加上空白行的範本標籤,例如 load 和 extends。連續的一組這類標籤只會在第一個之前加上空白行,檔案或區塊的開頭則不會加上空白行。
blank_line_before_tag=["load", "extends", "include"]blank_line_before_tag=["load", "extends", "include"]"blank_line_before_tag": ["load", "extends", "include"]--blank-line-before-tag "load,extends,include"close_void_tags
預設值: false
以斜線閉合已知的空元素標籤,讓 <img> 變成 <img />。在此選項下,要求相反寫法的 H018 不會被回報。
close_void_tags=trueclose_void_tags=true"close_void_tags": true--close-void-tagsconfiguration
全域設定檔,即 pyproject.toml、djlint.toml 或 .djlintrc,例如放在家目錄中的檔案。當兩者設定了相同的選項時,以專案本身的設定檔為準,除非啟用了 prefer_configuration。
--configuration ~/.djlintrccustom_blocks
預設值: 無
要縮排的自訂範本區塊名稱,例如 toc 代表 {% toc %}...{% endtoc %}。
custom_blocks=["toc", "example"]custom_blocks=["toc", "example"]"custom_blocks": ["toc", "example"]--custom-blocks "toc,example"custom_html
預設值: 無
要縮排的自訂 HTML 標籤名稱,例如 mjml 或 simple-greeting。名稱可以是正規表示式,例如 mj-\w+。
custom_html=["mjml", "simple-greeting", 'mj-\w+']custom_html=["mjml", "simple-greeting", 'mj-\w+']"custom_html": ["mjml", "simple-greeting", "mj-\\w+"]--custom-html "mjml,simple-greeting,mj-\w+"exclude
預設值: "__pypackages__,
要略過的路徑,用來取代預設清單。每個項目都是正規表示式,會與相對於專案根目錄或所檢查資料夾的路徑中完整的資料夾名稱或檔案名稱比對,例如 vendor 或 templates/legacy,因此 *.min.html 要寫成 .*\.min\.html。
exclude=[".venv", "venv", "build"]exclude=[".venv", "venv", "build"]"exclude": [".venv", "venv", "build"]--exclude ".venv,venv,build"extend_exclude
預設值: 無
要略過的路徑,加在 exclude 之外;未設定 exclude 時則加在預設清單之外,與 exclude 一樣以正規表示式撰寫。
extend_exclude=[".custom"]extend_exclude=[".custom"]"extend_exclude": [".custom"]--extend-exclude ".custom"extension
預設值: "html"
在資料夾中尋找的檔案副檔名。只能指定一個副檔名;以名稱指定的檔案無論副檔名為何都會被檢查。
extension="html.dj"extension="html.dj""extension": "html.dj"--extension "html.dj"# or-e "html.dj"files
預設值: 無
要檢查的路徑,用來取代命令列上的路徑;此時命令列的路徑必須是 -,就像使用 stdin 一樣。--stdin-filename 會讓 - 無論如何都代表 stdin。
[tool.djlint]files=["index.html"]files=["index.html"]"files": [ "index.html"]index.htmlformat_attribute_js_json
預設值: false
同時格式化 onclick、x-data 等屬性內的 JavaScript 和 JSON,也就是 format_attribute_js_json_pattern 所指定的屬性。屬性數量少於 format_attribute_js_json_min_props 的物件會保持在同一行。
format_attribute_js_json=trueformat_attribute_js_json=true"format_attribute_js_json": true--format-attribute-js-jsonformat_attribute_js_json_min_props
預設值: 2
只有在物件至少有這麼多個屬性時才格式化它。屬性數量較少的物件會保持在同一行。
format_attribute_js_json_min_props=3format_attribute_js_json_min_props=3"format_attribute_js_json_min_props": 3--format-attribute-js-json-min-props 3format_attribute_js_json_pattern
預設值: "^(?:on[a-z]+|
比對要以 JavaScript 或 JSON 格式化之屬性名稱的正規表示式。預設值會比對 on* 事件處理常式,以及 Alpine、Vue、Angular 和 Aurelia 的繫結。
format_attribute_js_json_pattern="^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$"format_attribute_js_json_pattern="^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$""format_attribute_js_json_pattern": "^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$"--format-attribute-js-json-pattern "^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$"format_attribute_template_tags
預設值: false
格式化標籤屬性內的範本語法。
format_attribute_template_tags=trueformat_attribute_template_tags=true"format_attribute_template_tags": true--format-attribute-template-tagsformat_css
預設值: false
同時使用 css-beautify 格式化 <style> 標籤的內容。其中的範本語法並未完全支援。
css 表格中的 indent_size,或命令列的 --indent-css,用來設定 <style> 標籤內的縮排,預設為 4。這個表格也接受其他 css-beautify 選項。
[tool.djlint]format_css=true
[tool.djlint.css]indent_size=5format_css=true
[css]indent_size=5"format_css": true,"css": { "indent_size": 5 }--format-css --indent-css 5format_js
預設值: false
同時使用 js-beautify 格式化 <script> 標籤內的 JavaScript 和 JSON。type 既不是 JavaScript 也不是 JSON 的指令碼(例如 text/template 中的用戶端範本)會保持原樣。其中的範本語法並未完全支援。
js 表格中的 indent_size,或命令列的 --indent-js,用來設定 <script> 標籤內的縮排,預設為 4。這個表格也接受其他 js-beautify 選項。
[tool.djlint]format_js=true
[tool.djlint.js]indent_size=5format_js=true
[js]indent_size=5"format_js": true,"js": { "indent_size": 5 }--format-js --indent-js 5github_output
預設值: 無
將結果以 GitHub Actions 註記的形式輸出。在 GitHub Actions 中預設開啟,可用 --no-github-output 關閉。請參閱 GitHub Actions。
--github-outputignore
預設值: 無
要略過的 linter 代碼。
ignore=["H014", "H015"]ignore=["H014", "H015"]"ignore": ["H014", "H015"]--ignore "H014,H015"ignore_blocks
預設值: 無
不為其內容增加縮排的範本區塊。其子元素會被當成該區塊的同層元素,並依此縮排。
ignore_blocks=["raw", "example"]ignore_blocks=["raw", "example"]"ignore_blocks": ["raw", "example"]--ignore-blocks "raw,example"ignore_case
預設值: false
不要修正已知 HTML 標籤的大小寫。在此選項下,H009 和 H010 不會被回報。
[tool.djlint]ignore_case=trueignore_case=true"ignore_case": true--ignore-caseinclude
預設值: 無
要啟用的 linter 代碼,包括預設關閉的規則和 profile 未包含的規則。
include=["H014", "H015"]include=["H014", "H015"]"include": ["H014", "H015"]--include "H014,H015"indent
預設值: 4
每一層縮排的空格數。未設定時,如果 .editorconfig 為該檔案指定了 indent_size,djLint 就會使用它。
indent=3indent=3"indent": 3--indent 3keep_br_inline
預設值: false
讓 <br> 留在它所斷開的文字那一行,而不是讓它獨佔一行。渲染為前面內容下方一條橫線的 <hr> 不受影響。
keep_br_inline=truekeep_br_inline=true"keep_br_inline": true--keep-br-inlineline_break_after_multiline_tag
預設值: false
當標籤的屬性分散在多行時,讓其內容從新的一行開始。
line_break_after_multiline_tag=trueline_break_after_multiline_tag=true"line_break_after_multiline_tag": true--line-break-after-multiline-taglinter_output_format
預設值: "{code} {line} {message} {match}"
每則 lint 訊息的格式,由 {filename}、{line}、{end}、{code}、{message} 和 {match} 組成。如果不包含 {filename},輸出就會依檔案分組,並為每個群組加上標頭。
linter_output_format="{filename}:{line}: {code} {message} {match}"linter_output_format="{filename}:{line}: {code} {message} {match}""linter_output_format": "{filename}:{line}: {code} {message} {match}"--linter-output-format "{filename}:{line}: {code} {message} {match}"max_attribute_length
預設值: 70
當標籤的屬性長度超過這個值時,格式化工具會把它們分散到多行。
max_attribute_length=10max_attribute_length=10"max_attribute_length": 10--max-attribute-length 10max_blank_lines
預設值: 0
最多保留這麼多行連續的空白行,因此設為 0 會移除空白行。啟用 preserve_blank_lines 時此選項無效。
max_blank_lines=5max_blank_lines=5"max_blank_lines": 5--max-blank-lines 5max_line_length
預設值: 120
只有當整行不超過這個長度時,格式化工具才會把部分 HTML 和範本標籤合併成一行。未設定時,如果 .editorconfig 為該檔案指定了 max_line_length,djLint 就會使用它。
max_line_length=100max_line_length=100"max_line_length": 100--max-line-length 100name_endblocks
預設值: false
對於跨越多行撰寫的區塊,將區塊的名稱寫入關閉它的 {% endblock %}。這正是 T003 所要求的。
name_endblocks=truename_endblocks=true"name_endblocks": true--name-endblocksno_entity_formatting
預設值: false
不要將字元實體參照改寫為它所代表的字元,因此 © 會保持原樣,而不會變成 ©。帶有語法作用的實體以及不可見字元的實體,無論如何都不會被改寫。在此選項下 H023 仍會回報實體;若要保留實體,請將 H023 加入 ignore。
no_entity_formatting=trueno_entity_formatting=true"no_entity_formatting": true--no-entity-formattingno_function_formatting
預設值: false
不要格式化 {{ }} 內函式呼叫的引數。
no_function_formatting=trueno_function_formatting=true"no_function_formatting": true--no-function-formattingno_indent_inner_html
預設值: false
不要將 <head> 和 <body> 縮排在 <html> 之內,這是 VS Code 內建 HTML 格式化工具排版文件的方式。
no_indent_inner_html=trueno_indent_inner_html=true"no_indent_inner_html": true--no-indent-inner-htmlno_line_after_yaml
預設值: false
不要在 YAML front matter 之後加上空白行。
no_line_after_yaml=trueno_line_after_yaml=true"no_line_after_yaml": true--no-line-after-yamlno_set_formatting
預設值: false
不要格式化 {% set %} 標籤的內容。
no_set_formatting=trueno_set_formatting=true"no_set_formatting": true--no-set-formattingper_file_ignores
預設值: 無
在路徑符合正規表示式的檔案中要略過的 linter 代碼。在設定檔中,鍵名是使用連字號的 per-file-ignores。
[tool.djlint.per-file-ignores]"file.html"= "H026,H025""file_two.html"="H001"[per-file-ignores]"file.html"= "H026,H025""file_two.html"="H001""per-file-ignores": { "file.html": "H026,H025", "file_two.html":"H001" }--per-file-ignores "file.html" "H026,H025" --per-file-ignores "file_two.html" "H001"prefer_configuration
預設值: false
讓 --configuration 所指定的檔案覆寫專案本身的設定檔。預設情況下,當兩者設定了相同的選項時,以專案本身的設定檔為準。僅限命令列使用,因為它決定的是設定檔本身如何被讀取。
--prefer-configurationpreserve_blank_lines
預設值: false
盡可能保留空白行。適合空白行是刻意為之的非 HTML 範本。
preserve_blank_lines=truepreserve_blank_lines=true"preserve_blank_lines": true--preserve-blank-linespreserve_class_newlines
預設值: false
保留跨多行撰寫之 class 屬性內的換行。
preserve_class_newlines=truepreserve_class_newlines=true"preserve_class_newlines": true--preserve-class-newlinespreserve_leading_space
預設值: false
盡可能保留文字行開頭的空白。適合文字縮排是刻意為之的非 HTML 範本。
preserve_leading_space=truepreserve_leading_space=true"preserve_leading_space": true--preserve-leading-spaceprofile
預設值: 無
範本語言。它決定要執行哪些 linter 規則,以及如何格式化範本語法:例如在 handlebars 中,{{#if}} 標籤內不會有空格。
未設定 profile 時,djLint 會像 all 一樣格式化所有範本語言的語法,linter 則使用 html 的規則。
選項:
- html
- django
- jinja
- nunjucks(用於 nunjucks 和 twig)
- handlebars(用於 handlebars 和 mustache)
- liquid(shopify、jekyll、eleventy)
- golang(go 範本;hugo、helm)
- angular
- tera(也適用於 zola;minijinja 請使用 jinja)
- askama(rust 中的 jinja 風格範本;rust 運算式永遠不會被重新格式化)
- mako(控制行與
##註解保持原樣) - all(一次涵蓋所有範本語言)
profile="django"profile="django""profile": "django"--profile "django"quiet
預設值: false
不要印出 diff、進度列和摘要。
quiet=truequiet=true"quiet": true--quietquote_style
預設值: "double"
範本標籤內字串所使用的引號,可以是 double 或 single。T002 會要求使用相同的引號,而格式化工具在 {% if x == "a" %} 這類條件判斷中也會使用它們,因此同一個檔案不會以兩種寫法拼寫同一個字串。HTML 屬性的引號則交由 H008 處理。
quote_style="single"quote_style="single""quote_style": "single"--quote-style singlerequire_pragma
預設值: false
只格式化或 lint 以 djlint:on 註解開頭的檔案。這個註解可以是 HTML 註解,或是 profile 之範本語言的註解;沒有 profile 時,則可以是任何一種範本語言的註解。
<!-- djlint:on -->{# djlint:on #}{% comment %} djlint:on {% endcomment %}{{ /* djlint:on */ }}{{!-- djlint:on --}}require_pragma=truerequire_pragma=true"require_pragma": true--require-pragmarules
.djlint_rules.yaml 格式的自訂規則檔,會取代專案根目錄中的規則檔被讀取。
--rules ../shared/.djlint_rules.yamlsarif
預設值: false
將檢查結果輸出為 SARIF 2.1.0 文件,供 GitHub 程式碼掃描及其他工具使用。請參閱 GitHub Actions。
--sarifsingle_attribute_per_line
預設值: false
開始標籤需要換行時,將標籤名稱、每個屬性和結尾的角括號分別放在各自的行上。
single_attribute_per_line=truesingle_attribute_per_line=true"single_attribute_per_line": true--single-attribute-per-linesort_attributes
預設值: false
依名稱排序屬性,id 排第一,class 排第二。若標籤的屬性受範本標籤控制,則會保留原本的撰寫順序,因為將屬性移出它所在的分支會改變頁面。
sort_attributes=truesort_attributes=true"sort_attributes": true--sort-attributessort_load_tags
預設值: false
排序 {% load %} 標籤中的函式庫,並移除重複寫了兩次的函式庫,因此 {% load static i18n %} 會變成 {% load i18n static %}。在 {% load a b from library %} 中,會排序 from 之前的名稱。當兩個函式庫定義了同名的標籤或篩選器時,以最後載入的為準,因此排序可能會改變範本所使用的是哪一個。
sort_load_tags=truesort_load_tags=true"sort_load_tags": true--sort-load-tagsstatistics
預設值: false
lint 完成後,印出每個代碼被找到的次數。
--statisticsstdin_filename
預設值: "-"
在訊息中,以及比對 exclude 和 per-file-ignores 時,代表 stdin 的路徑。
--stdin-filename templates/index.htmluse_gitignore
預設值: false
同時略過專案根目錄中 .gitignore 所列出的路徑。
use_gitignore=trueuse_gitignore=true"use_gitignore": true--use-gitignoreversion
顯示版本並結束。
--versionwarn
預設值: false
有 linter 錯誤或需要重新格式化的檔案時,以 0 而非 1 結束。
--warn