mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Update MD040/fenced-code-language to add language_only parameter to reject extra data in info string.
This commit is contained in:
parent
718de432f3
commit
72439f42c6
13 changed files with 176 additions and 1 deletions
|
@ -4176,6 +4176,7 @@ module.exports = {
|
|||
"function": function MD040(params, onError) {
|
||||
let allowed = params.config.allowed_languages;
|
||||
allowed = Array.isArray(allowed) ? allowed : [];
|
||||
const languageOnly = !!params.config.language_only;
|
||||
filterTokens(params, "fence", function forToken(token) {
|
||||
const lang = token.info.trim().split(/\s+/u).shift();
|
||||
if (lang === "") {
|
||||
|
@ -4184,6 +4185,9 @@ module.exports = {
|
|||
else if ((allowed.length > 0) && !allowed.includes(lang)) {
|
||||
addError(onError, token.lineNumber, `"${lang}" is not allowed`);
|
||||
}
|
||||
if (languageOnly && (token.info !== lang)) {
|
||||
addError(onError, token.lineNumber, `Info string contains more than language: "${token.info}"`);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
@ -29,6 +29,13 @@ You can configure the `allowed_languages` parameter to specify a list of
|
|||
languages code blocks could use. Languages are case sensitive. The default value
|
||||
is `[]` which means any language specifier is valid.
|
||||
|
||||
You can prevent extra data from being present in the info string of fenced code
|
||||
blocks. To do so, set the `language_only` parameter to `true`.
|
||||
|
||||
<!-- markdownlint-disable-next-line no-space-in-code -->
|
||||
Info strings with leading/trailing whitespace (ex: `js `) or other content (ex:
|
||||
`ruby startline=3`) will trigger this rule.
|
||||
|
||||
Rationale: Specifying a language improves content rendering by using the
|
||||
correct syntax highlighting for code. More information:
|
||||
<https://cirosantilli.com/markdown-style-guide#option-code-fenced>.
|
||||
|
|
|
@ -1623,6 +1623,7 @@ Aliases: `fenced-code-language`
|
|||
Parameters:
|
||||
|
||||
* `allowed_languages`: List of languages (`string[]`, default `[]`)
|
||||
* `language_only`: Require language only (`boolean`, default `false`)
|
||||
|
||||
This rule is triggered when fenced code blocks are used, but a language isn't
|
||||
specified:
|
||||
|
@ -1655,6 +1656,13 @@ You can configure the `allowed_languages` parameter to specify a list of
|
|||
languages code blocks could use. Languages are case sensitive. The default value
|
||||
is `[]` which means any language specifier is valid.
|
||||
|
||||
You can prevent extra data from being present in the info string of fenced code
|
||||
blocks. To do so, set the `language_only` parameter to `true`.
|
||||
|
||||
<!-- markdownlint-disable-next-line no-space-in-code -->
|
||||
Info strings with leading/trailing whitespace (ex: `js `) or other content (ex:
|
||||
`ruby startline=3`) will trigger this rule.
|
||||
|
||||
Rationale: Specifying a language improves content rendering by using the
|
||||
correct syntax highlighting for code. More information:
|
||||
<https://cirosantilli.com/markdown-style-guide#option-code-fenced>.
|
||||
|
|
|
@ -7,6 +7,7 @@ Aliases: `fenced-code-language`
|
|||
Parameters:
|
||||
|
||||
* `allowed_languages`: List of languages (`string[]`, default `[]`)
|
||||
* `language_only`: Require language only (`boolean`, default `false`)
|
||||
|
||||
This rule is triggered when fenced code blocks are used, but a language isn't
|
||||
specified:
|
||||
|
@ -39,6 +40,13 @@ You can configure the `allowed_languages` parameter to specify a list of
|
|||
languages code blocks could use. Languages are case sensitive. The default value
|
||||
is `[]` which means any language specifier is valid.
|
||||
|
||||
You can prevent extra data from being present in the info string of fenced code
|
||||
blocks. To do so, set the `language_only` parameter to `true`.
|
||||
|
||||
<!-- markdownlint-disable-next-line no-space-in-code -->
|
||||
Info strings with leading/trailing whitespace (ex: `js `) or other content (ex:
|
||||
`ruby startline=3`) will trigger this rule.
|
||||
|
||||
Rationale: Specifying a language improves content rendering by using the
|
||||
correct syntax highlighting for code. More information:
|
||||
<https://cirosantilli.com/markdown-style-guide#option-code-fenced>.
|
||||
|
|
10
lib/md040.js
10
lib/md040.js
|
@ -11,6 +11,8 @@ module.exports = {
|
|||
"function": function MD040(params, onError) {
|
||||
let allowed = params.config.allowed_languages;
|
||||
allowed = Array.isArray(allowed) ? allowed : [];
|
||||
const languageOnly = !!params.config.language_only;
|
||||
|
||||
filterTokens(params, "fence", function forToken(token) {
|
||||
const lang = token.info.trim().split(/\s+/u).shift();
|
||||
if (lang === "") {
|
||||
|
@ -22,6 +24,14 @@ module.exports = {
|
|||
`"${lang}" is not allowed`
|
||||
);
|
||||
}
|
||||
|
||||
if (languageOnly && (token.info !== lang)) {
|
||||
addError(
|
||||
onError,
|
||||
token.lineNumber,
|
||||
`Info string contains more than language: "${token.info}"`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
@ -209,7 +209,9 @@
|
|||
// MD040/fenced-code-language - Fenced code blocks should have a language specified
|
||||
"MD040": {
|
||||
// List of languages
|
||||
"allowed_languages": []
|
||||
"allowed_languages": [],
|
||||
// Require language only
|
||||
"language_only": false
|
||||
},
|
||||
|
||||
// MD041/first-line-heading/first-line-h1 - First line in a file should be a top-level heading
|
||||
|
|
|
@ -191,6 +191,8 @@ MD039: true
|
|||
MD040:
|
||||
# List of languages
|
||||
allowed_languages: []
|
||||
# Require language only
|
||||
language_only: false
|
||||
|
||||
# MD041/first-line-heading/first-line-h1 - First line in a file should be a top-level heading
|
||||
MD041:
|
||||
|
|
|
@ -356,6 +356,11 @@ for (const rule of rules) {
|
|||
"type": "string"
|
||||
},
|
||||
"default": []
|
||||
},
|
||||
"language_only": {
|
||||
"description": "Require language only",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
}
|
||||
};
|
||||
break;
|
||||
|
|
|
@ -682,6 +682,11 @@
|
|||
"type": "string"
|
||||
},
|
||||
"default": []
|
||||
},
|
||||
"language_only": {
|
||||
"description": "Require language only",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
|
|
6
test/md040-language_only.json
Normal file
6
test/md040-language_only.json
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"MD040": {
|
||||
"allowed_languages": ["html", "css"],
|
||||
"language_only": true
|
||||
}
|
||||
}
|
25
test/md040-language_only.md
Normal file
25
test/md040-language_only.md
Normal file
|
@ -0,0 +1,25 @@
|
|||
# md040-language_only.md
|
||||
|
||||
Fence code block information with leading whitespace:
|
||||
|
||||
``` html
|
||||
<h1>markdownlint</h1> {MD040:5}
|
||||
```
|
||||
|
||||
Fence code block information with trailing whitespace:
|
||||
|
||||
```css
|
||||
body {} {MD040:11} {MD009:11}
|
||||
```
|
||||
|
||||
Fence code block information with extra data:
|
||||
|
||||
```html version=5
|
||||
<title>MarkdownLint</title> {MD040:17}
|
||||
```
|
||||
|
||||
Fence code block information without whitespaces and extra data:
|
||||
|
||||
```css
|
||||
a {}
|
||||
```
|
|
@ -29553,6 +29553,99 @@ Generated by [AVA](https://avajs.dev).
|
|||
`,
|
||||
}
|
||||
|
||||
## md040-language_only.md
|
||||
|
||||
> Snapshot 1
|
||||
|
||||
{
|
||||
errors: [
|
||||
{
|
||||
errorContext: null,
|
||||
errorDetail: 'Expected: 0 or 2; Actual: 1',
|
||||
errorRange: [
|
||||
7,
|
||||
1,
|
||||
],
|
||||
fixInfo: {
|
||||
deleteCount: 1,
|
||||
editColumn: 7,
|
||||
},
|
||||
lineNumber: 11,
|
||||
ruleDescription: 'Trailing spaces',
|
||||
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md009.md',
|
||||
ruleNames: [
|
||||
'MD009',
|
||||
'no-trailing-spaces',
|
||||
],
|
||||
},
|
||||
{
|
||||
errorContext: null,
|
||||
errorDetail: 'Info string contains more than language: " html"',
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
lineNumber: 5,
|
||||
ruleDescription: 'Fenced code blocks should have a language specified',
|
||||
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md040.md',
|
||||
ruleNames: [
|
||||
'MD040',
|
||||
'fenced-code-language',
|
||||
],
|
||||
},
|
||||
{
|
||||
errorContext: null,
|
||||
errorDetail: 'Info string contains more than language: "css "',
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
lineNumber: 11,
|
||||
ruleDescription: 'Fenced code blocks should have a language specified',
|
||||
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md040.md',
|
||||
ruleNames: [
|
||||
'MD040',
|
||||
'fenced-code-language',
|
||||
],
|
||||
},
|
||||
{
|
||||
errorContext: null,
|
||||
errorDetail: 'Info string contains more than language: "html version=5"',
|
||||
errorRange: null,
|
||||
fixInfo: null,
|
||||
lineNumber: 17,
|
||||
ruleDescription: 'Fenced code blocks should have a language specified',
|
||||
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md040.md',
|
||||
ruleNames: [
|
||||
'MD040',
|
||||
'fenced-code-language',
|
||||
],
|
||||
},
|
||||
],
|
||||
fixed: `# md040-language_only.md␊
|
||||
␊
|
||||
Fence code block information with leading whitespace:␊
|
||||
␊
|
||||
\`\`\` html␊
|
||||
<h1>markdownlint</h1> {MD040:5}␊
|
||||
\`\`\`␊
|
||||
␊
|
||||
Fence code block information with trailing whitespace:␊
|
||||
␊
|
||||
\`\`\`css␊
|
||||
body {} {MD040:11} {MD009:11}␊
|
||||
\`\`\`␊
|
||||
␊
|
||||
Fence code block information with extra data:␊
|
||||
␊
|
||||
\`\`\`html version=5␊
|
||||
<title>MarkdownLint</title> {MD040:17}␊
|
||||
\`\`\`␊
|
||||
␊
|
||||
Fence code block information without whitespaces and extra data:␊
|
||||
␊
|
||||
\`\`\`css␊
|
||||
a {}␊
|
||||
\`\`\`␊
|
||||
`,
|
||||
}
|
||||
|
||||
## md041-ignore-leading-comments-combined.md
|
||||
|
||||
> Snapshot 1
|
||||
|
|
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue