-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add markdownlint-cli2-formatter-template output formatter (fixes #426).
- Loading branch information
1 parent
a6dc24d
commit 7dccba3
Showing
18 changed files
with
280 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) David Anson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# markdownlint-cli2-formatter-template | ||
|
||
> An output formatter for [`markdownlint-cli2`][markdownlint-cli2] that displays | ||
> results using a template. | ||
[![npm version][npm-image]][npm-url] | ||
[![License][license-image]][license-url] | ||
|
||
## Install | ||
|
||
```bash | ||
npm install markdownlint-cli2-formatter-template --save-dev | ||
``` | ||
|
||
## Use | ||
|
||
This output formatter makes it easy to custom-format linting violations. To | ||
specify an output format, set the `template` parameter to a `string` with text | ||
and one or more tokens representing any of the following elements. The specified | ||
template will be applied once for each violation. | ||
|
||
These tokens are always defined: | ||
|
||
| Token | Meaning | | ||
|-------------------|-----------------------| | ||
| `fileName` | File name | | ||
| `lineNumber` | Line number (1-based) | | ||
| `ruleName` | Rule name (full) | | ||
| `ruleDescription` | Rule description | | ||
| `ruleInformation` | Informational URL | | ||
|
||
These tokens are sometimes defined (depending on the rule/violation): | ||
|
||
| Token | Meaning | | ||
|----------------|-------------------------| | ||
| `columnNumber` | Column number (1-based) | | ||
| `errorContext` | Context information | | ||
| `errorDetail` | Additional detail | | ||
|
||
In the simplest case, tokens are specified with the syntax `${token}`. This is | ||
all that's needed for tokens that are always defined. To support scenarios where | ||
a token may not be defined, the syntaxes `${token:text if present}` and | ||
`${token!text if not present}` are also supported. This allows for templates to | ||
accomodate missing data. Only one level of token nesting is supported. | ||
|
||
A few examples demonstrate the concept: | ||
|
||
<!-- markdownlint-disable line-length --> | ||
|
||
| Template | Output if defined | Output if not defined | | ||
|--------------------------------------------------------------------------|-------------------|-----------------------| | ||
| `Column=${columnNumber}` | `Column=10` | `Column=` | | ||
| `${columnNumber:Column=${columnNumber}}` | `Column=10` | | | ||
| `${columnNumber!No column number}` | | `No column number` | | ||
| `${columnNumber:Column=${columnNumber}}${columnNumber!No column number}` | `Column=10` | `No column number` | | ||
|
||
<!-- markdownlint-restore --> | ||
|
||
## Example | ||
|
||
To output in the [Azure Pipelines Task command LogIssue format][task-logissue], | ||
use something like the following `.markdownlint-cli2.jsonc`: | ||
|
||
```json | ||
{ | ||
"outputFormatters": [ | ||
[ | ||
"markdownlint-cli2-formatter-template", | ||
{ | ||
"template": "##vso[task.logissue type=error;sourcepath=${fileName};linenumber=${lineNumber};${columnNumber:columnumber=${columnNumber};}code=${ruleName}]${ruleDescription}" | ||
} | ||
] | ||
] | ||
} | ||
``` | ||
|
||
Which produces output like: | ||
|
||
```text | ||
##vso[task.logissue type=error;sourcepath=viewme.md;linenumber=3;columnumber=10;code=MD009/no-trailing-spaces]Trailing spaces | ||
##vso[task.logissue type=error;sourcepath=viewme.md;linenumber=5;code=MD012/no-multiple-blanks]Multiple consecutive blank lines | ||
##vso[task.logissue type=error;sourcepath=viewme.md;linenumber=6;code=MD025/single-title/single-h1]Multiple top-level headings in the same document | ||
##vso[task.logissue type=error;sourcepath=viewme.md;linenumber=12;columnumber=4;code=MD019/no-multiple-space-atx]Multiple spaces after hash on atx style heading | ||
##vso[task.logissue type=error;sourcepath=viewme.md;linenumber=14;columnumber=14;code=MD047/single-trailing-newline]Files should end with a single newline character | ||
``` | ||
|
||
[license-image]: https://img.shields.io/npm/l/markdownlint-cli2-formatter-template.svg | ||
[license-url]: https://opensource.org/licenses/MIT | ||
[markdownlint-cli2]: https://github.com/DavidAnson/markdownlint-cli2 | ||
[npm-image]: https://img.shields.io/npm/v/markdownlint-cli2-formatter-template.svg | ||
[npm-url]: https://www.npmjs.com/package/markdownlint-cli2-formatter-template | ||
[task-logissue]: https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash#logissue-log-an-error-or-warning |
53 changes: 53 additions & 0 deletions
53
formatter-template/markdownlint-cli2-formatter-template.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// @ts-check | ||
|
||
"use strict"; | ||
|
||
// eslint-disable-next-line prefer-named-capture-group | ||
const tokenRe = /\$\{([^:!}]+)(?:([:!])([^{}]*\{[^{}]+\}[^{}]*|[^}]+))?\}/igu; | ||
|
||
// Output markdownlint-cli2 results using a template | ||
const outputFormatter = (options, params) => { | ||
const { logError, results } = options; | ||
const template = params?.template || | ||
// eslint-disable-next-line no-template-curly-in-string | ||
"fileName=\"${fileName}\" lineNumber=${lineNumber} ${columnNumber:columnNumber=${columnNumber} }ruleName=${ruleName} ruleDescription=\"${ruleDescription}\" ruleInformation=${ruleInformation} errorContext=\"${errorContext}\" errorDetail=\"${errorDetail}\""; | ||
|
||
for (const result of results) { | ||
const tokenToResult = { | ||
"fileName": result.fileName, | ||
"lineNumber": result.lineNumber, | ||
"columnNumber": result.errorRange?.[0], | ||
"ruleName": result.ruleNames.join("/"), | ||
"ruleDescription": result.ruleDescription, | ||
"ruleInformation": result.ruleInformation, | ||
"errorContext": result.errorContext, | ||
"errorDetail": result.errorDetail | ||
}; | ||
|
||
// eslint-disable-next-line unicorn/consistent-function-scoping | ||
const replacer = (match, token, type, text) => { | ||
const value = tokenToResult[token]; | ||
switch (type) { | ||
case ":": | ||
{ | ||
return (value === undefined) ? "" : text; | ||
} | ||
case "!": | ||
{ | ||
return (value === undefined) ? text : ""; | ||
} | ||
default: | ||
{ | ||
return value ?? ""; | ||
} | ||
} | ||
}; | ||
|
||
const output = template. | ||
replaceAll(tokenRe, replacer). | ||
replaceAll(tokenRe, replacer); | ||
logError(output); | ||
} | ||
}; | ||
|
||
module.exports = outputFormatter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "markdownlint-cli2-formatter-template", | ||
"version": "0.0.1", | ||
"description": "An output formatter for markdownlint-cli2 that displays results using a template", | ||
"author": { | ||
"name": "David Anson", | ||
"url": "https://dlaa.me/" | ||
}, | ||
"license": "MIT", | ||
"main": "markdownlint-cli2-formatter-template.js", | ||
"homepage": "https://github.com/DavidAnson/markdownlint-cli2", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/DavidAnson/markdownlint-cli2.git" | ||
}, | ||
"bugs": "https://github.com/DavidAnson/markdownlint-cli2/issues", | ||
"funding": "https://github.com/sponsors/DavidAnson", | ||
"files": [ | ||
"markdownlint-cli2-formatter-template.js" | ||
], | ||
"peerDependencies": { | ||
"markdownlint-cli2": ">=0.0.4" | ||
}, | ||
"keywords": [ | ||
"markdownlint-cli2-formatter" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.