Skip to content

Commit 9f7d1dc

Browse files
committed
Add markdownlint.configFile configuration setting to expose markdownlint-cli2's --config parameter (fixes #366).
1 parent 937390e commit 9f7d1dc

File tree

4 files changed

+61
-25
lines changed

4 files changed

+61
-25
lines changed

.markdownlint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"### Disable",
6464
"## Configure",
6565
"### markdownlint.config",
66+
"### markdownlint.configFile",
6667
"### markdownlint.focusMode",
6768
"### markdownlint.run",
6869
"### markdownlint.customRules",

README.md

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ To temporarily disable linting of Markdown documents, run the `markdownlint.togg
167167
168168
## Configure
169169

170-
### markdownlint.config
171-
172170
By default (i.e., without customizing anything), all rules are enabled *except* [`MD013`/`line-length`](https://github.com/DavidAnson/markdownlint/blob/v0.37.4/doc/md013.md) because many files include lines longer than the conventional 80 character limit:
173171

174172
```json
@@ -178,22 +176,22 @@ By default (i.e., without customizing anything), all rules are enabled *except*
178176
```
179177

180178
Rules can be enabled, disabled, and customized by creating a [JSON](https://en.wikipedia.org/wiki/JSON) file named `.markdownlint.jsonc`/`.markdownlint.json` or a [YAML](https://en.wikipedia.org/wiki/YAML) file named `.markdownlint.yaml`/`.markdownlint.yml` or a [JavaScript](https://en.wikipedia.org/wiki/JavaScript) file named `.markdownlint.cjs` in any directory of a project.
181-
Additionally, options (which include rules and things like [`markdown-it` plugins](https://www.npmjs.com/search?q=keywords:markdown-it-plugin) and other settings) can be configured by creating a JSON file named `.markdownlint-cli2.jsonc` or a YAML file named `.markdownlint-cli2.yaml` or a JavaScript file named `.markdownlint-cli2.cjs` in any directory of a project.
179+
Additionally, options (which include rules and other settings) can be configured by creating a JSON file named `.markdownlint-cli2.jsonc` or a YAML file named `.markdownlint-cli2.yaml` or a JavaScript file named `.markdownlint-cli2.cjs` in any directory of a project.
180+
Rules can also be configured using VS Code's support for [user and workspace settings](https://code.visualstudio.com/docs/customization/userandworkspace).
182181

183182
> For more information about configuration file precedence and complete examples, see the [Configuration section of the markdownlint-cli2 README.md](https://github.com/DavidAnson/markdownlint-cli2#configuration).
184183
185-
A custom configuration is often defined by a `.markdownlint.json` file in the root of the project:
184+
A custom rule configuration is often defined by a `.markdownlint.json` file in the root of the project:
186185

187186
```json
188187
{
189-
"default": true,
190188
"MD003": { "style": "atx_closed" },
191189
"MD007": { "indent": 4 },
192190
"no-hard-tabs": false
193191
}
194192
```
195193

196-
To extend another configuration file, such a file can use the `extends` property to provide a relative path:
194+
To extend another configuration file, use the `extends` property to provide a relative path:
197195

198196
```json
199197
{
@@ -204,42 +202,63 @@ To extend another configuration file, such a file can use the `extends` property
204202

205203
Files referenced via `extends` do not need to be part of the current project (but usually are).
206204

207-
Rules can also be configured using VS Code's support for [user and workspace settings](https://code.visualstudio.com/docs/customization/userandworkspace).
205+
Configuration sources have the following precedence (in decreasing order):
206+
207+
* `.markdownlint-cli2.{jsonc,yaml,cjs}` file in the same or parent directory
208+
* `.markdownlint.{jsonc,json,yaml,yml,cjs}` file in the same or parent directory
209+
* Visual Studio Code user/workspace settings (see [markdownlint.config](#markdownlintconfig) and [markdownlint.configFile](#markdownlintconfigfile) below)
210+
* Default configuration (see above)
211+
212+
Configuration changes saved to any location take effect immediately.
213+
Files referenced via `extends` are not monitored for changes.
214+
Inherited configuration can be explicitly disabled (or re-enabled) in any configuration file.
215+
216+
When a workspace is open, running the `markdownlint.openConfigFile` command (from the Command Palette or by binding it to a keyboard shortcut) will open an editor for the `.markdownlint-cli2.{jsonc,yaml,cjs}` or `.markdownlint.{jsonc,json,yaml,yml,cjs}` configuration file in the root of the workspace.
217+
If none of these files exist, a new `.markdownlint.json` containing the default rule configuration will be opened in the editor in the "pending save" state.
218+
219+
> **Note**: Because JavaScript is cached by VS Code after being loaded, edits to `.markdownlint.cjs`/`.markdownlint-cli2.cjs` require a restart of VS Code.
208220
209-
The above configuration might look like the following in VS Code's user settings file:
221+
### markdownlint.config
222+
223+
> **Note**: Using a project-local configuration file is preferred because doing so works with command-line tools and is easier for collaboration.
224+
225+
The configuration above might look like the following in VS Code's user settings file:
210226

211227
```json
212228
{
213229
"editor.someSetting": true,
214230
"markdownlint.config": {
215-
"default": true,
216231
"MD003": { "style": "atx_closed" },
217232
"MD007": { "indent": 4 },
218233
"no-hard-tabs": false
219234
}
220235
}
221236
```
222237

223-
When using `extends`:
238+
When using `extends` in this context:
224239

225240
* File paths referenced by `extends` from configuration files within a workspace are resolved relative to that configuration file.
226241
* When running VS Code locally:
227242
* File paths referenced by `extends` from user settings are resolved relative to the user's home directory (e.g., `%USERPROFILE%` on Windows or `$HOME` on macOS/Linux).
228243
* File paths referenced by `extends` from workspace settings are resolved relative to the workspace folder.
229244
* VS Code's [predefined variables](https://code.visualstudio.com/docs/editor/variables-reference) `${userHome}` and `${workspaceFolder}` can be used within an `extends` path from user or workspace settings to override the default behavior.
230245

231-
Configuration sources have the following precedence (in decreasing order):
246+
### markdownlint.configFile
232247

233-
* `.markdownlint-cli2.{jsonc,yaml,cjs}` file in the same or parent directory
234-
* `.markdownlint.{jsonc,json,yaml,yml,cjs}` file in the same or parent directory
235-
* Visual Studio Code user/workspace settings
236-
* Default configuration (see above)
248+
The default behavior of storing configuration files in the root of a project works well most of the time.
249+
However, projects that need to store configuration files in a different location can set `configFile` to the project-relative path of that file.
250+
All [`markdownlint-cli2` configuration files used with `--config`](https://github.com/DavidAnson/markdownlint-cli2?tab=readme-ov-file#command-line) are supported.
237251

238-
Configuration changes saved to any location take effect immediately. Files referenced via `extends` are not monitored for changes. Inherited configuration can be explicitly disabled (or re-enabled) in any configuration file.
252+
This looks like the following in VS Code's user settings:
239253

240-
When a workspace is open, running the `markdownlint.openConfigFile` command (from the Command Palette or by binding it to a keyboard shortcut) will open an editor for the `.markdownlint-cli2.{jsonc,yaml,cjs}` or `.markdownlint.{jsonc,json,yaml,yml,cjs}` configuration file in the root of the workspace. If none of these files exist, a new `.markdownlint.json` containing the default rule configuration will be opened in the editor in the "pending save" state.
254+
```json
255+
{
256+
"editor.someSetting": true,
257+
"markdownlint.configFile": "./config/.markdownlint.jsonc"
258+
}
259+
```
241260

242-
> **Note**: Because JavaScript is cached by VS Code after being loaded, edits to `.markdownlint.cjs`/`.markdownlint-cli2.cjs` require a restart of VS Code.
261+
If [markdownlint.config](#markdownlintconfig) is also set, the settings from `configFile` take precedence.
243262

244263
### markdownlint.focusMode
245264

extension.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const clickForConfigureUrl = "https://github.com/DavidAnson/vscode-markdownlint#
8181
const errorExceptionPrefix = "Exception while linting with markdownlint-cli2:\n";
8282
const openCommand = "vscode.open";
8383
const sectionConfig = "config";
84+
const sectionConfigFile = "configFile";
8485
const sectionCustomRules = "customRules";
8586
const sectionFocusMode = "focusMode";
8687
const sectionLintWorkspaceGlobs = "lintWorkspaceGlobs";
@@ -364,6 +365,14 @@ async function getConfig (fs, configuration, uri) {
364365
};
365366
}
366367

368+
// Returns an array of args entries for the config path for the user/workspace
369+
function getConfigFileArgs (configuration) {
370+
const configFile = configuration.get(sectionConfigFile);
371+
/** @type {string[]} */
372+
const configFileArgs = (configFile?.length > 0) ? [ "--config", expandTildePath(configFile, os) ] : [];
373+
return configFileArgs;
374+
}
375+
367376
// Returns custom rule configuration for user/workspace
368377
function getCustomRules (configuration) {
369378
const customRulesPaths = configuration.get(sectionCustomRules);
@@ -429,6 +438,7 @@ async function markdownlintWrapper (document) {
429438
const argv = independentDocument ?
430439
[] :
431440
[ `:${name}` ];
441+
argv.push(...getConfigFileArgs(configuration));
432442
const contents = independentDocument ?
433443
"nonFileContents" :
434444
"fileContents";

package.json

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@
172172
"title": "markdownlint",
173173
"type": "object",
174174
"properties": {
175+
"markdownlint.config": {
176+
"description": "markdownlint configuration object",
177+
"scope": "resource",
178+
"type": "object",
179+
"$ref": "https://raw.githubusercontent.com/DavidAnson/markdownlint/v0.37.4/schema/markdownlint-config-schema.json",
180+
"default": {}
181+
},
182+
"markdownlint.configFile": {
183+
"description": "Path to a configuration file that defines the base configuration",
184+
"scope": "resource",
185+
"type": "string",
186+
"default": null
187+
},
175188
"markdownlint.customRules": {
176189
"description": "Array of paths for custom rules to include when linting",
177190
"scope": "resource",
@@ -215,13 +228,6 @@
215228
"onType"
216229
],
217230
"default": "onType"
218-
},
219-
"markdownlint.config": {
220-
"description": "markdownlint config object",
221-
"scope": "resource",
222-
"type": "object",
223-
"$ref": "https://raw.githubusercontent.com/DavidAnson/markdownlint/v0.37.4/schema/markdownlint-config-schema.json",
224-
"default": {}
225231
}
226232
}
227233
}

0 commit comments

Comments
 (0)