Skip to content

Commit ac6d310

Browse files
committed
wip
1 parent 937390e commit ac6d310

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
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: 28 additions & 15 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,15 +176,14 @@ 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 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. Rules can also be configured using VS Code's support for [user and workspace settings](https://code.visualstudio.com/docs/customization/userandworkspace).
182180

183181
> 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).
184182
185183
A custom configuration is often defined by a `.markdownlint.json` file in the root of the project:
186184

187185
```json
188186
{
189-
"default": true,
190187
"MD003": { "style": "atx_closed" },
191188
"MD007": { "indent": 4 },
192189
"no-hard-tabs": false
@@ -204,15 +201,29 @@ To extend another configuration file, such a file can use the `extends` property
204201

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

207-
Rules can also be configured using VS Code's support for [user and workspace settings](https://code.visualstudio.com/docs/customization/userandworkspace).
204+
Configuration sources have the following precedence (in decreasing order):
205+
206+
* `.markdownlint-cli2.{jsonc,yaml,cjs}` file in the same or parent directory
207+
* `.markdownlint.{jsonc,json,yaml,yml,cjs}` file in the same or parent directory
208+
* Visual Studio Code user/workspace settings (see [markdownlint.config](#markdownlintconfig) and [markdownlint.configFile](#markdownlintconfigfile) below)
209+
* Default configuration (see above)
210+
211+
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.
212+
213+
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.
214+
215+
> **Note**: Because JavaScript is cached by VS Code after being loaded, edits to `.markdownlint.cjs`/`.markdownlint-cli2.cjs` require a restart of VS Code.
216+
217+
### markdownlint.config
218+
219+
> **Note**: When possible, using a project-local configuration file is preferred because it works with command-line tools and is easier for collaboration.
208220
209-
The above configuration might look like the following in VS Code's user settings file:
221+
The configuration above might look like the following in VS Code's user settings file:
210222

211223
```json
212224
{
213225
"editor.someSetting": true,
214226
"markdownlint.config": {
215-
"default": true,
216227
"MD003": { "style": "atx_closed" },
217228
"MD007": { "indent": 4 },
218229
"no-hard-tabs": false
@@ -228,18 +239,20 @@ When using `extends`:
228239
* File paths referenced by `extends` from workspace settings are resolved relative to the workspace folder.
229240
* 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.
230241

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

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)
244+
The default behavior of locating configuration files in the root of a project works well most of the time. However, projects that store configuration files in a different location can set `configFile` to the relative path of that file. All the [`markdownlint-cli2` configuration files used with `--config`](https://github.com/DavidAnson/markdownlint-cli2?tab=readme-ov-file#command-line) are supported.
237245

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.
246+
This looks like the following in VS Code's user settings:
239247

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.
248+
```json
249+
{
250+
"editor.someSetting": true,
251+
"markdownlint.configFile": "./config/.markdownlint.jsonc"
252+
}
253+
```
241254

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.
255+
If [markdownlint.config](#markdownlintconfig) is also set, the settings from `configFile` take precedence.
243256

244257
### markdownlint.focusMode
245258

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 ? [ "--config", configFile ] : [];
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)