跳到內容

設定

設定寫在專案的 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

始於 1.44.0 formatter linter --allow-empty-input

預設值: false

當指定的路徑完全沒有比對到任何檔案時,以 0 而不是 2 結束。已找到、但隨後被 exclude、extend_exclude、use_gitignore 或 require_pragma 略過的檔案,本來就會以 0 結束。

allow_empty_input=true

blank_line_after_tag

始於 0.4.2 formatter --blank-line-after-tag

預設值: 無

要在後面加上空白行的範本標籤,例如 load 和 extends。連續的一組這類標籤只會在最後一個之後加上空白行,區塊的結尾則不會加上空白行。

blank_line_after_tag=["load", "extends", "include"]

blank_line_before_tag

始於 1.8.0 formatter --blank-line-before-tag

預設值: 無

要在前面加上空白行的範本標籤,例如 load 和 extends。連續的一組這類標籤只會在第一個之前加上空白行,檔案或區塊的開頭則不會加上空白行。

blank_line_before_tag=["load", "extends", "include"]

close_void_tags

始於 1.26.0 formatter linter --close-void-tags

預設值: false

以斜線閉合已知的空元素標籤,讓 <img> 變成 <img />。在此選項下,要求相反寫法的 H018 不會被回報。

close_void_tags=true

configuration

始於 1.13.0 formatter linter --configuration

全域設定檔,即 pyproject.toml、djlint.toml 或 .djlintrc,例如放在家目錄中的檔案。當兩者設定了相同的選項時,以專案本身的設定檔為準,除非啟用了 prefer_configuration。

終端機視窗
--configuration ~/.djlintrc

custom_blocks

始於 0.3.5 formatter --custom-blocks

預設值: 無

要縮排的自訂範本區塊名稱,例如 toc 代表 {% toc %}...{% endtoc %}。

custom_blocks=["toc", "example"]

custom_html

始於 0.7.0 formatter --custom-html

預設值: 無

要縮排的自訂 HTML 標籤名稱,例如 mjml 或 simple-greeting。名稱可以是正規表示式,例如 mj-\w+。

custom_html=["mjml", "simple-greeting", 'mj-\w+']

exclude

始於 0.4.0 linter formatter --exclude

預設值: "__pypackages__,_build,_site,.bzr,.direnv,.eggs,.git,.git-rewrite,.hg,.nox,.svn,.tox,.venv,build,dist,htmlcov,node_modules,site-packages,venv"

要略過的路徑,用來取代預設清單。每個項目都是正規表示式,會與相對於專案根目錄或所檢查資料夾的路徑中完整的資料夾名稱或檔案名稱比對,例如 vendor 或 templates/legacy,因此 *.min.html 要寫成 .*\.min\.html。

exclude=[".venv", "venv", "build"]

extend_exclude

始於 0.4.0 linter formatter --extend-exclude

預設值: 無

要略過的路徑,加在 exclude 之外;未設定 exclude 時則加在預設清單之外,與 exclude 一樣以正規表示式撰寫。

extend_exclude=[".custom"]

extension

始於 0.0.5 linter formatter --extension

預設值: "html"

在資料夾中尋找的檔案副檔名。只能指定一個副檔名;以名稱指定的檔案無論副檔名為何都會被檢查。

extension="html.dj"

files

始於 1.14.0 linter formatter

預設值: 無

要檢查的路徑,用來取代命令列上的路徑;此時命令列的路徑必須是 -,就像使用 stdin 一樣。--stdin-filename 會讓 - 無論如何都代表 stdin。

[tool.djlint]
files=["index.html"]

format_attribute_js_json

始於 1.37.0 formatter --format-attribute-js-json

預設值: false

同時格式化 onclick、x-data 等屬性內的 JavaScript 和 JSON,也就是 format_attribute_js_json_pattern 所指定的屬性。屬性數量少於 format_attribute_js_json_min_props 的物件會保持在同一行。

format_attribute_js_json=true

format_attribute_js_json_min_props

始於 1.37.0 formatter --format-attribute-js-json-min-props

預設值: 2

只有在物件至少有這麼多個屬性時才格式化它。屬性數量較少的物件會保持在同一行。

format_attribute_js_json_min_props=3

format_attribute_js_json_pattern

始於 1.37.0 formatter --format-attribute-js-json-pattern

預設值: "^(?:on[a-z]+|x-[a-z\\-]+|@[a-z\\-]+|:[a-z\\-]+|v-[a-z\\-]+|\\([a-z\\-]+\\)|\\[[a-z\\-]+\\]|\\*ng[A-Z][a-zA-Z]*|[a-z\\-]+\\.(bind|delegate|call|trigger))$"

比對要以 JavaScript 或 JSON 格式化之屬性名稱的正規表示式。預設值會比對 on* 事件處理常式,以及 Alpine、Vue、Angular 和 Aurelia 的繫結。

format_attribute_js_json_pattern="^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$"

format_attribute_template_tags

始於 0.6.7 formatter --format-attribute-template-tags

預設值: false

格式化標籤屬性內的範本語法。

format_attribute_template_tags=true

format_css

始於 1.9.0 formatter --format-css

預設值: false

同時使用 css-beautify 格式化 <style> 標籤的內容。其中的範本語法並未完全支援。

css 表格中的 indent_size,或命令列的 --indent-css,用來設定 <style> 標籤內的縮排,預設為 4。這個表格也接受其他 css-beautify 選項。

[tool.djlint]
format_css=true
[tool.djlint.css]
indent_size=5

format_js

始於 1.9.0 formatter --format-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=5

github_output

始於 1.37.0 formatter linter --github-output

預設值: 無

將結果以 GitHub Actions 註記的形式輸出。在 GitHub Actions 中預設開啟,可用 --no-github-output 關閉。請參閱 GitHub Actions。

終端機視窗
--github-output

ignore

始於 0.1.5 linter --ignore

預設值: 無

要略過的 linter 代碼。

ignore=["H014", "H015"]

ignore_blocks

始於 1.24.0 formatter --ignore-blocks

預設值: 無

不為其內容增加縮排的範本區塊。其子元素會被當成該區塊的同層元素,並依此縮排。

ignore_blocks=["raw", "example"]

ignore_case

始於 1.23.0 formatter linter --ignore-case

預設值: false

不要修正已知 HTML 標籤的大小寫。在此選項下,H009 和 H010 不會被回報。

[tool.djlint]
ignore_case=true

include

始於 1.20.0 linter --include

預設值: 無

要啟用的 linter 代碼,包括預設關閉的規則和 profile 未包含的規則。

include=["H014", "H015"]

indent

始於 0.3.5 formatter --indent

預設值: 4

每一層縮排的空格數。未設定時,如果 .editorconfig 為該檔案指定了 indent_size,djLint 就會使用它。

indent=3

keep_br_inline

始於 1.45.0 formatter --keep-br-inline

預設值: false

讓 <br> 留在它所斷開的文字那一行,而不是讓它獨佔一行。渲染為前面內容下方一條橫線的 <hr> 不受影響。

keep_br_inline=true

line_break_after_multiline_tag

始於 1.27.0 formatter --line-break-after-multiline-tag

預設值: false

當標籤的屬性分散在多行時,讓其內容從新的一行開始。

line_break_after_multiline_tag=true

linter_output_format

始於 0.6.7 linter --linter-output-format

預設值: "{code} {line} {message} {match}"

每則 lint 訊息的格式,由 {filename}、{line}、{end}、{code}、{message} 和 {match} 組成。如果不包含 {filename},輸出就會依檔案分組,並為每個群組加上標頭。

linter_output_format="{filename}:{line}: {code} {message} {match}"

max_attribute_length

始於 0.5.8 formatter --max-attribute-length

預設值: 70

當標籤的屬性長度超過這個值時,格式化工具會把它們分散到多行。

max_attribute_length=10

max_blank_lines

始於 1.31.0 formatter --max-blank-lines

預設值: 0

最多保留這麼多行連續的空白行,因此設為 0 會移除空白行。啟用 preserve_blank_lines 時此選項無效。

max_blank_lines=5

max_line_length

始於 0.5.7 formatter --max-line-length

預設值: 120

只有當整行不超過這個長度時,格式化工具才會把部分 HTML 和範本標籤合併成一行。未設定時,如果 .editorconfig 為該檔案指定了 max_line_length,djLint 就會使用它。

max_line_length=100

name_endblocks

始於 1.45.0 formatter --name-endblocks

預設值: false

對於跨越多行撰寫的區塊,將區塊的名稱寫入關閉它的 {% endblock %}。這正是 T003 所要求的。

name_endblocks=true

no_entity_formatting

始於 1.45.0 formatter --no-entity-formatting

預設值: false

不要將字元實體參照改寫為它所代表的字元,因此 &copy; 會保持原樣,而不會變成 ©。帶有語法作用的實體以及不可見字元的實體,無論如何都不會被改寫。在此選項下 H023 仍會回報實體;若要保留實體,請將 H023 加入 ignore。

no_entity_formatting=true

no_function_formatting

始於 1.30.2 formatter --no-function-formatting

預設值: false

不要格式化 {{ }} 內函式呼叫的引數。

no_function_formatting=true

no_indent_inner_html

始於 1.45.0 formatter --no-indent-inner-html

預設值: false

不要將 <head> 和 <body> 縮排在 <html> 之內,這是 VS Code 內建 HTML 格式化工具排版文件的方式。

no_indent_inner_html=true

no_line_after_yaml

始於 1.29.0 formatter --no-line-after-yaml

預設值: false

不要在 YAML front matter 之後加上空白行。

no_line_after_yaml=true

no_set_formatting

始於 1.30.2 formatter --no-set-formatting

預設值: false

不要格式化 {% set %} 標籤的內容。

no_set_formatting=true

per_file_ignores

始於 1.7.0 linter --per-file-ignores

預設值: 無

在路徑符合正規表示式的檔案中要略過的 linter 代碼。在設定檔中,鍵名是使用連字號的 per-file-ignores。

[tool.djlint.per-file-ignores]
"file.html"= "H026,H025"
"file_two.html"="H001"

prefer_configuration

始於 1.45.0 formatter linter --prefer-configuration

預設值: false

讓 --configuration 所指定的檔案覆寫專案本身的設定檔。預設情況下,當兩者設定了相同的選項時,以專案本身的設定檔為準。僅限命令列使用,因為它決定的是設定檔本身如何被讀取。

終端機視窗
--prefer-configuration

preserve_blank_lines

始於 1.3.0 formatter --preserve-blank-lines

預設值: false

盡可能保留空白行。適合空白行是刻意為之的非 HTML 範本。

preserve_blank_lines=true

preserve_class_newlines

始於 1.39.0 formatter --preserve-class-newlines

預設值: false

保留跨多行撰寫之 class 屬性內的換行。

preserve_class_newlines=true

preserve_leading_space

始於 1.2.0 formatter --preserve-leading-space

預設值: false

盡可能保留文字行開頭的空白。適合文字縮排是刻意為之的非 HTML 範本。

preserve_leading_space=true

profile

始於 0.4.5 linter formatter --profile

預設值: 無

範本語言。它決定要執行哪些 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"

quiet

始於 0.0.9 formatter linter --quiet

預設值: false

不要印出 diff、進度列和摘要。

quiet=true

quote_style

始於 1.45.0 formatter linter --quote-style

預設值: "double"

範本標籤內字串所使用的引號,可以是 double 或 single。T002 會要求使用相同的引號,而格式化工具在 {% if x == "a" %} 這類條件判斷中也會使用它們,因此同一個檔案不會以兩種寫法拼寫同一個字串。HTML 屬性的引號則交由 H008 處理。

quote_style="single"

require_pragma

始於 0.5.8 formatter linter --require-pragma

預設值: false

只格式化或 lint 以 djlint:on 註解開頭的檔案。這個註解可以是 HTML 註解,或是 profile 之範本語言的註解;沒有 profile 時,則可以是任何一種範本語言的註解。

<!-- djlint:on -->
{# djlint:on #}
{% comment %} djlint:on {% endcomment %}
{{ /* djlint:on */ }}
{{!-- djlint:on --}}
require_pragma=true

rules

始於 1.41.0 formatter linter --rules

.djlint_rules.yaml 格式的自訂規則檔,會取代專案根目錄中的規則檔被讀取。

終端機視窗
--rules ../shared/.djlint_rules.yaml

sarif

始於 1.46.0 formatter linter --sarif

預設值: false

將檢查結果輸出為 SARIF 2.1.0 文件,供 GitHub 程式碼掃描及其他工具使用。請參閱 GitHub Actions。

終端機視窗
--sarif

single_attribute_per_line

始於 1.40.0 formatter --single-attribute-per-line

預設值: false

開始標籤需要換行時,將標籤名稱、每個屬性和結尾的角括號分別放在各自的行上。

single_attribute_per_line=true

sort_attributes

始於 1.45.0 formatter --sort-attributes

預設值: false

依名稱排序屬性,id 排第一,class 排第二。若標籤的屬性受範本標籤控制,則會保留原本的撰寫順序,因為將屬性移出它所在的分支會改變頁面。

sort_attributes=true

sort_load_tags

始於 1.47.0 formatter --sort-load-tags

預設值: false

排序 {% load %} 標籤中的函式庫,並移除重複寫了兩次的函式庫,因此 {% load static i18n %} 會變成 {% load i18n static %}。在 {% load a b from library %} 中,會排序 from 之前的名稱。當兩個函式庫定義了同名的標籤或篩選器時,以最後載入的為準,因此排序可能會改變範本所使用的是哪一個。

sort_load_tags=true

statistics

始於 1.16.0 linter --statistics

預設值: false

lint 完成後,印出每個代碼被找到的次數。

終端機視窗
--statistics

stdin_filename

始於 1.43.0 formatter linter --stdin-filename

預設值: "-"

在訊息中,以及比對 exclude 和 per-file-ignores 時,代表 stdin 的路徑。

終端機視窗
--stdin-filename templates/index.html

use_gitignore

始於 0.5.9 linter formatter --use-gitignore

預設值: false

同時略過專案根目錄中 .gitignore 所列出的路徑。

use_gitignore=true

version

始於 0.3.9 formatter linter --version

顯示版本並結束。

終端機視窗
--version

warn

始於 0.7.6 formatter linter --warn

預設值: false

有 linter 錯誤或需要重新格式化的檔案時,以 0 而非 1 結束。

終端機視窗
--warn