Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions .markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
{
"config": {
"code-block-style": { "style": "fenced" },
"line-length": false,
"no-duplicate-heading": { "siblings_only": true },
"ol-prefix": { "style": "ordered" },

// The dprint hook runs before this linter, so these match its output.
"code-fence-style": { "style": "backtick" },
"emphasis-style": { "style": "underscore" },
"heading-style": { "style": "atx" },
"strong-style": { "style": "asterisk" },
"ul-style": { "style": "dash" },
"extends": "./.markdownlint.jsonc",
},

"gitignore": true,
Expand Down
13 changes: 13 additions & 0 deletions .markdownlint.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"code-block-style": { "style": "fenced" },
"line-length": false,
"no-duplicate-heading": { "siblings_only": true },
"ol-prefix": { "style": "ordered" },

// The dprint hook runs before this linter, so these match its output.
"code-fence-style": { "style": "backtick" },
"emphasis-style": { "style": "underscore" },
"heading-style": { "style": "atx" },
"strong-style": { "style": "asterisk" },
"ul-style": { "style": "dash" },
}
22 changes: 7 additions & 15 deletions docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ preCommit = dk.preCommit {

### Use a project configuration file

Prettier, Taplo, and markdownlint-cli2 use devkit's shared [Prettier style](../.prettierrc.json), [Taplo formatting](../.taplo.toml), and [markdownlint rules](../.markdownlint-cli2.jsonc). A `.prettierrc.json` or `.taplo.toml` at the source root replaces the shared file once the dev shell is re-entered, and a project `.markdownlint-cli2.jsonc` overrides individual shared rules.
- A root **Prettier** config (`.prettierrc.json`) replaces the [shared config](../.prettierrc.json). The hook disables EditorConfig. Use basename globs in shared overrides because Prettier resolves them from the Nix store.
- A root **Taplo** config (`.taplo.toml`) replaces the shared config. Copy the [shared file](../.taplo.toml) to the project root for native discovery.
- A project **markdownlint-cli2** config (`.markdownlint-cli2.jsonc`) overrides rules from the [shared CLI2 config](../.markdownlint-cli2.jsonc). Relative `customRules` and `markdownItPlugins` require `configPath = ".markdownlint-cli2.jsonc"`. For an explicit project config or native discovery, use `config.extends` with a workspace-relative copy of the [shared rules](../.markdownlint.jsonc).

Use basenames in the shared Prettier configuration's `overrides`, since Prettier resolves those globs from the configuration's Nix store directory.

Set `hooks.<name>.settings.configPath` to point markdownlint-cli2, Prettier, Ruff, or Taplo at another file, or to `""` to let the tool find its own. A `.markdownlint-cli2.jsonc` that loads relative `customRules` or `markdownItPlugins` needs `configPath = ".markdownlint-cli2.jsonc"`, which drops the shared rules.
Re-enter the dev shell after adding a root Prettier or Taplo config. For these hooks and Ruff, set `hooks.<name>.settings.configPath = ""` to use native discovery, or set it to a specific file.

### Add a spelling dictionary

Expand All @@ -72,13 +72,11 @@ doctoc fills a table of contents between `<!-- START doctoc -->` and `<!-- END d

### Format Markdown

Let dprint format Markdown so it preserves indentation and blank lines in fenced examples and leaves embedded HTML intact. It formats every file that prek selects, including tracked Markdown files that match Git ignore rules. Keep the Markdown override in `.editorconfig` so editors preserve two-space hard line breaks.
The dprint hook formats tracked Markdown even when it matches Git ignore rules. Native dprint can use or extend [`dprint.json`](../dprint.json), which downloads the same pinned WASM that the Nix hook loads from the store. Additional language plugins can reformat fenced examples. Pass editor buffers with `--stdin md`, since an absolute buffer path can be skipped by file selection.

### Check EditorConfig properties

Because EditorConfig has no include mechanism and editors use it outside the dev shell, check in a copy of the shared `.editorconfig` and add language-specific sections that match the repository's formatters. The Nushell section also sets the indentation the checker enforces, since no hook formats Nushell.

Exclude binary payloads that the checker treats as text through `hooks.editorconfig-checker.excludes`.
Copy the shared [`.editorconfig`](../.editorconfig) for native discovery. Keep its Markdown exception for two-space hard line breaks. Use `hooks.editorconfig-checker.excludes` for binary payloads the checker treats as text.

### Check Nushell files

Expand Down Expand Up @@ -125,13 +123,7 @@ Start the repository's `.envrc` with a shell directive so shellcheck can lint it

## Run a lockfile-pinned tool

ESLint and TypeScript already run from the project's pnpm lockfile, and mypy runs from its uv lockfile. Use `dk.nodeTool "<command>"` or `dk.uvTool "<command>"` when another hook needs project dependencies. For example, set `hooks.prettier.package = dk.nodeTool "prettier";` for Prettier plugins, or use `dk.nodeTool "cspell"` for npm dictionaries.

Add `dk.nodeToolchain` and `dk.uvToolchain` to the dev shell to match the Node.js and pnpm used by `nodeTool` and the uv version that [`mkLib` selects](flake.md) for `uvTool`.

```nix
packages = builtins.attrValues dk.nodeToolchain ++ builtins.attrValues dk.uvToolchain;
```
ESLint and TypeScript use `pnpm-lock.yaml`, and mypy uses `uv.lock`. For hooks that need project dependencies, set the package via `dk.nodeTool` or `dk.uvTool`. Include the matching toolchain in the dev shell to use the same runtime versions interactively.

### Declare pnpm

Expand Down
10 changes: 10 additions & 0 deletions dprint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"markdown": {
"emphasisKind": "underscores",
"strongKind": "asterisks",
"codeBlock.preserveIndentation": true,
"codeBlock.preserveBlankLines": true,
"html.skipFormat": true
},
"plugins": ["https://plugins.dprint.dev/markdown-0.29.0.wasm"]
}
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
mkLib =
pkgs:
import ./nix/lib.nix {
inherit pkgs git-hooks-nix;
inherit pkgs repo git-hooks-nix;
nushell = packageAtLeast pkgs "nushell" "0.116";
ruff = packageAtLeast pkgs "ruff" "0.17";
uv = packageAtLeast pkgs "uv" "0.13";
Expand Down
23 changes: 23 additions & 0 deletions nix/dprint-config.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ==================================================================================================
# Dprint Configuration
# ==================================================================================================

{
pkgs,
repo,
}:

let
inherit (pkgs) lib;
config = lib.importJSON (lib.path.append repo.root "dprint.json");
dprint-plugin-markdown = pkgs.callPackage ./packages/dprint-plugin-markdown.nix {
url = lib.head config.plugins;
};
in
assert lib.length config.plugins == 1;
pkgs.writers.writeJSON "dprint.json" (
config
// {
plugins = [ "${dprint-plugin-markdown}/plugin.wasm" ];
}
)
30 changes: 20 additions & 10 deletions nix/hooks/baseline.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

{
pkgs,
repo,
src,
configEntry,
dprint,
dprintConfig,
nodeTool,
nu-check,
nuCheckEntry,
Expand All @@ -20,16 +23,23 @@ let
# Patched packages
# ------------------------------------------------------------------------------------------------
actionlint = pkgs.callPackage ../packages/actionlint.nix { };
dprint-plugin-markdown = pkgs.callPackage ../packages/dprint-plugin-markdown.nix { };

# ------------------------------------------------------------------------------------------------
# Formatter configuration
# Linter configuration
# ------------------------------------------------------------------------------------------------
dprintConfig = import ./dprint-config.nix { inherit pkgs dprint-plugin-markdown; };
markdownlintConfig = lib.fileset.toSource {
inherit (repo) root;
fileset = lib.fileset.unions [
(lib.path.append repo.root ".markdownlint-cli2.jsonc")
(lib.path.append repo.root ".markdownlint.jsonc")
];
};

# A project config stays checkout-relative, since Prettier resolves `overrides` globs from the
# config file's directory.
projectConfig = file: shared: if builtins.pathExists "${src}/${file}" then file else "${shared}";
# ------------------------------------------------------------------------------------------------
# Formatter configuration
# ------------------------------------------------------------------------------------------------
projectConfig =
file: if builtins.pathExists "${src}/${file}" then file else "${lib.path.append repo.root file}";

# ------------------------------------------------------------------------------------------------
# Shared hook settings
Expand Down Expand Up @@ -149,7 +159,7 @@ in

dprint = {
enable = true;
package = pkgs.dprint;
package = dprint;
entry = "${lib.getExe config.hooks.dprint.package} fmt";
args = [
"--config=${dprintConfig}"
Expand All @@ -167,7 +177,7 @@ in
"--no-globs"
];
files = "\\.md$";
settings.configPath = "${../../.markdownlint-cli2.jsonc}";
settings.configPath = "${markdownlintConfig}/.markdownlint-cli2.jsonc";
};

# ----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -254,7 +264,7 @@ in
# ----------------------------------------------------------------------------------------------
taplo = {
entry = configEntry config.hooks.taplo [ "fmt" ];
settings.configPath = projectConfig ".taplo.toml" ../../.taplo.toml;
settings.configPath = projectConfig ".taplo.toml";
};

# ----------------------------------------------------------------------------------------------
Expand All @@ -267,7 +277,7 @@ in
"(^|/)pnpm-lock\\.yaml$"
];
settings = {
configPath = projectConfig ".prettierrc.json" ../../.prettierrc.json;
configPath = projectConfig ".prettierrc.json";
no-editorconfig = true;
};
};
Expand Down
6 changes: 6 additions & 0 deletions nix/hooks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

{
pkgs,
repo,
git-hooks-nix,
dprint,
dprintConfig,
nodeTool,
nu-check,
ruff,
Expand All @@ -28,8 +31,11 @@ let
baseline = import ./baseline.nix {
inherit
pkgs
repo
src
configEntry
dprint
dprintConfig
nodeTool
nu-check
nuCheckEntry
Expand Down
21 changes: 0 additions & 21 deletions nix/hooks/dprint-config.nix

This file was deleted.

12 changes: 12 additions & 0 deletions nix/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

{
pkgs,
repo,
git-hooks-nix,
nushell,
ruff,
Expand Down Expand Up @@ -60,13 +61,22 @@ let

nuTests = import ./nu-test-check.nix { inherit pkgs nushell nu-test; };

# ------------------------------------------------------------------------------------------------
# Markdown formatter
# ------------------------------------------------------------------------------------------------
inherit (pkgs) dprint;
dprintConfig = import ./dprint-config.nix { inherit pkgs repo; };

# ------------------------------------------------------------------------------------------------
# Pre-commit hooks
# ------------------------------------------------------------------------------------------------
preCommit = import ./hooks {
inherit
pkgs
repo
git-hooks-nix
dprint
dprintConfig
nodeTool
nu-check
ruff
Expand All @@ -76,6 +86,8 @@ let
in
{
inherit
dprint
dprintConfig
mkNuApplication
nodeTool
nodeToolchain
Expand Down
3 changes: 2 additions & 1 deletion nix/packages/dprint-plugin-markdown.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

{
pkgs,
url,
}:

pkgs.dprint-plugins.dprint-plugin-markdown.overrideAttrs (_: {
# Current nixpkgs pins 0.20.0, which rewrites protected content.
version = "0.29.0";

src = pkgs.fetchurl {
url = "https://plugins.dprint.dev/markdown-0.29.0.wasm";
inherit url;
hash = "sha256-cUrqdVXdmDGGG+Rm2osmstkjFtPN5VDvPh9jsx9OowI=";
};
})
2 changes: 1 addition & 1 deletion tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let
in
{
actions = import ./actions { inherit pkgs dk repo; };
dprint-markdown = import ./dprint-markdown { inherit pkgs repo; };
dprint-markdown = import ./dprint-markdown { inherit pkgs dk; };
pin-check = dk.nuTests {
src = ./pin-check;
libDirs = [ ../lib ];
Expand Down
17 changes: 3 additions & 14 deletions tests/dprint-markdown/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,18 @@

{
pkgs,
repo,
dk,
}:

let
inherit (pkgs) lib;

dprint-plugin-markdown =
pkgs.callPackage (lib.path.append repo.root "nix/packages/dprint-plugin-markdown.nix")
{ };

dprintConfig = import (lib.path.append repo.root "nix/hooks/dprint-config.nix") {
inherit pkgs dprint-plugin-markdown;
};
in
pkgs.runCommand "dprint-markdown"
{
nativeBuildInputs = [ pkgs.dprint ];
nativeBuildInputs = [ dk.dprint ];
}
''
cp ${./fixtures/input.md} formatted.md
chmod +w formatted.md
export DPRINT_CACHE_DIR="$TMPDIR/dprint-cache"
dprint fmt --config=${dprintConfig} formatted.md
dprint fmt --config=${dk.dprintConfig} formatted.md
diff -u ${./fixtures/expected.md} formatted.md
touch "$out"
''
Loading