mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Update MD010/no-hard-tabs to add ignore_code_languages parameter (fixes #383).
This commit is contained in:
parent
0f845e9ba1
commit
b447c809bd
9 changed files with 112 additions and 3 deletions
|
@ -2550,7 +2550,7 @@ module.exports = {
|
||||||
"use strict";
|
"use strict";
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, forEachLine = _a.forEachLine, overlapsAnyRange = _a.overlapsAnyRange;
|
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, filterTokens = _a.filterTokens, forEachLine = _a.forEachLine, overlapsAnyRange = _a.overlapsAnyRange;
|
||||||
var _b = __webpack_require__(/*! ./cache */ "../lib/cache.js"), codeBlockAndSpanRanges = _b.codeBlockAndSpanRanges, lineMetadata = _b.lineMetadata;
|
var _b = __webpack_require__(/*! ./cache */ "../lib/cache.js"), codeBlockAndSpanRanges = _b.codeBlockAndSpanRanges, lineMetadata = _b.lineMetadata;
|
||||||
var tabRe = /\t+/g;
|
var tabRe = /\t+/g;
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -2560,11 +2560,21 @@ module.exports = {
|
||||||
"function": function MD010(params, onError) {
|
"function": function MD010(params, onError) {
|
||||||
var codeBlocks = params.config.code_blocks;
|
var codeBlocks = params.config.code_blocks;
|
||||||
var includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
|
var includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||||
|
var ignoreCodeLanguages = new Set((params.config.ignore_code_languages || [])
|
||||||
|
.map(function (language) { return language.toLowerCase(); }));
|
||||||
var spacesPerTab = params.config.spaces_per_tab;
|
var spacesPerTab = params.config.spaces_per_tab;
|
||||||
var spaceMultiplier = (spacesPerTab === undefined) ?
|
var spaceMultiplier = (spacesPerTab === undefined) ?
|
||||||
1 :
|
1 :
|
||||||
Math.max(0, Number(spacesPerTab));
|
Math.max(0, Number(spacesPerTab));
|
||||||
var exclusions = includeCode ? [] : codeBlockAndSpanRanges();
|
var exclusions = includeCode ? [] : codeBlockAndSpanRanges();
|
||||||
|
filterTokens(params, "fence", function (token) {
|
||||||
|
var language = token.info.trim().toLowerCase();
|
||||||
|
if (ignoreCodeLanguages.has(language)) {
|
||||||
|
for (var i = token.map[0] + 1; i < token.map[1] - 1; i++) {
|
||||||
|
exclusions.push([i, 0, params.lines[i].length]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
forEachLine(lineMetadata(), function (line, lineIndex, inCode) {
|
forEachLine(lineMetadata(), function (line, lineIndex, inCode) {
|
||||||
if (includeCode || !inCode) {
|
if (includeCode || !inCode) {
|
||||||
var match = null;
|
var match = null;
|
||||||
|
|
|
@ -396,7 +396,7 @@ Tags: whitespace, hard_tab
|
||||||
|
|
||||||
Aliases: no-hard-tabs
|
Aliases: no-hard-tabs
|
||||||
|
|
||||||
Parameters: code_blocks, spaces_per_tab (boolean; default true, number; default 1)
|
Parameters: code_blocks, ignore_code_languages, spaces_per_tab (boolean; default true, array of string; default empty, number; default 1)
|
||||||
|
|
||||||
Fixable: Most violations can be fixed by tooling
|
Fixable: Most violations can be fixed by tooling
|
||||||
|
|
||||||
|
@ -429,6 +429,12 @@ set the `code_blocks` parameter to `false`. Code blocks and spans are included
|
||||||
by default since handling of tabs by Markdown tools can be inconsistent (e.g.,
|
by default since handling of tabs by Markdown tools can be inconsistent (e.g.,
|
||||||
using 4 vs. 8 spaces).
|
using 4 vs. 8 spaces).
|
||||||
|
|
||||||
|
When code blocks are scanned (e.g., by default or if `code_blocks` is `true`),
|
||||||
|
the `ignore_code_languages` parameter can be set to a list of languages that
|
||||||
|
should be ignored (i.e., hard tabs will be allowed, though not required). This
|
||||||
|
makes it easier for documents to include code for languages that require hard
|
||||||
|
tabs.
|
||||||
|
|
||||||
By default, violations of this rule are fixed by replacing the tab with 1 space
|
By default, violations of this rule are fixed by replacing the tab with 1 space
|
||||||
character. To use a different number of spaces, set the `spaces_per_tab`
|
character. To use a different number of spaces, set the `spaces_per_tab`
|
||||||
parameter to the desired value.
|
parameter to the desired value.
|
||||||
|
|
15
lib/md010.js
15
lib/md010.js
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const { addError, forEachLine, overlapsAnyRange } = require("../helpers");
|
const { addError, filterTokens, forEachLine, overlapsAnyRange } =
|
||||||
|
require("../helpers");
|
||||||
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
|
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
|
||||||
|
|
||||||
const tabRe = /\t+/g;
|
const tabRe = /\t+/g;
|
||||||
|
@ -14,11 +15,23 @@ module.exports = {
|
||||||
"function": function MD010(params, onError) {
|
"function": function MD010(params, onError) {
|
||||||
const codeBlocks = params.config.code_blocks;
|
const codeBlocks = params.config.code_blocks;
|
||||||
const includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
|
const includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
|
||||||
|
const ignoreCodeLanguages = new Set(
|
||||||
|
(params.config.ignore_code_languages || [])
|
||||||
|
.map((language) => language.toLowerCase())
|
||||||
|
);
|
||||||
const spacesPerTab = params.config.spaces_per_tab;
|
const spacesPerTab = params.config.spaces_per_tab;
|
||||||
const spaceMultiplier = (spacesPerTab === undefined) ?
|
const spaceMultiplier = (spacesPerTab === undefined) ?
|
||||||
1 :
|
1 :
|
||||||
Math.max(0, Number(spacesPerTab));
|
Math.max(0, Number(spacesPerTab));
|
||||||
const exclusions = includeCode ? [] : codeBlockAndSpanRanges();
|
const exclusions = includeCode ? [] : codeBlockAndSpanRanges();
|
||||||
|
filterTokens(params, "fence", (token) => {
|
||||||
|
const language = token.info.trim().toLowerCase();
|
||||||
|
if (ignoreCodeLanguages.has(language)) {
|
||||||
|
for (let i = token.map[0] + 1; i < token.map[1] - 1; i++) {
|
||||||
|
exclusions.push([ i, 0, params.lines[i].length ]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
|
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
|
||||||
if (includeCode || !inCode) {
|
if (includeCode || !inCode) {
|
||||||
let match = null;
|
let match = null;
|
||||||
|
|
|
@ -58,6 +58,8 @@
|
||||||
"MD010": {
|
"MD010": {
|
||||||
// Include code blocks
|
// Include code blocks
|
||||||
"code_blocks": true,
|
"code_blocks": true,
|
||||||
|
// Fenced code languages to ignore
|
||||||
|
"ignore_code_languages": [],
|
||||||
// Number of spaces for each hard tab
|
// Number of spaces for each hard tab
|
||||||
"spaces_per_tab": 1
|
"spaces_per_tab": 1
|
||||||
},
|
},
|
||||||
|
|
|
@ -52,6 +52,8 @@ MD009:
|
||||||
MD010:
|
MD010:
|
||||||
# Include code blocks
|
# Include code blocks
|
||||||
code_blocks: true
|
code_blocks: true
|
||||||
|
# Fenced code languages to ignore
|
||||||
|
ignore_code_languages: []
|
||||||
# Number of spaces for each hard tab
|
# Number of spaces for each hard tab
|
||||||
spaces_per_tab: 1
|
spaces_per_tab: 1
|
||||||
|
|
||||||
|
|
|
@ -147,6 +147,14 @@ rules.forEach(function forRule(rule) {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": true
|
"default": true
|
||||||
},
|
},
|
||||||
|
"ignore_code_languages": {
|
||||||
|
"description": "Fenced code languages to ignore",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"default": []
|
||||||
|
},
|
||||||
"spaces_per_tab": {
|
"spaces_per_tab": {
|
||||||
"description": "Number of spaces for each hard tab",
|
"description": "Number of spaces for each hard tab",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
|
|
|
@ -201,6 +201,14 @@
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": true
|
"default": true
|
||||||
},
|
},
|
||||||
|
"ignore_code_languages": {
|
||||||
|
"description": "Fenced code languages to ignore",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"default": []
|
||||||
|
},
|
||||||
"spaces_per_tab": {
|
"spaces_per_tab": {
|
||||||
"description": "Number of spaces for each hard tab",
|
"description": "Number of spaces for each hard tab",
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
|
|
10
test/code-block-with-language-allowed.json
Normal file
10
test/code-block-with-language-allowed.json
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"default": true,
|
||||||
|
"MD010": {
|
||||||
|
"ignore_code_languages": [
|
||||||
|
"js",
|
||||||
|
"text"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"MD046": false
|
||||||
|
}
|
50
test/code-block-with-language-allowed.md
Normal file
50
test/code-block-with-language-allowed.md
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
# Heading
|
||||||
|
|
||||||
|
```js
|
||||||
|
if (true) {
|
||||||
|
console.log("true");
|
||||||
|
if (false) {
|
||||||
|
console.log("false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
if (true) {
|
||||||
|
console.log("true");
|
||||||
|
if (false) {
|
||||||
|
console.log("false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
if (true) {
|
||||||
|
console.log("true");
|
||||||
|
if (false) {
|
||||||
|
console.log("false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```text
|
||||||
|
hello
|
||||||
|
world
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
if (true) { // {MD010}
|
||||||
|
console.log("true"); // {MD010}
|
||||||
|
if (false) { // {MD010}
|
||||||
|
console.log("false"); // {MD010}
|
||||||
|
} // {MD010}
|
||||||
|
} // {MD010}
|
||||||
|
|
||||||
|
Line with hard tab. {MD010}
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
if (true) {
|
||||||
|
console.log("true"); // {MD010}
|
||||||
|
if (false) { // {MD010}
|
||||||
|
console.log("false"); // {MD010}
|
||||||
|
} // {MD010}
|
||||||
|
}
|
||||||
|
```
|
Loading…
Add table
Add a link
Reference in a new issue