Update MD040/fenced-code-language to use case-sensitive comparison for allowed languages (fixes #627).

This commit is contained in:
Sébastien Règne 2022-11-06 01:41:27 +01:00 committed by GitHub
parent 30353cc733
commit b73ab7fd91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 106 additions and 32 deletions

View file

@ -4167,16 +4167,13 @@ module.exports = {
"tags": ["code", "language"], "tags": ["code", "language"],
"function": function MD040(params, onError) { "function": function MD040(params, onError) {
let allowed = params.config.allowed_languages; let allowed = params.config.allowed_languages;
allowed = Array.isArray(allowed) ? allowed = Array.isArray(allowed) ? allowed : [];
allowed.map((lang) => lang.toLowerCase()) :
[];
filterTokens(params, "fence", function forToken(token) { filterTokens(params, "fence", function forToken(token) {
const lang = token.info.trim(); const lang = token.info.trim().split(/\s+/u).shift();
if (lang === "") { if (lang === "") {
addErrorContext(onError, token.lineNumber, token.line); addErrorContext(onError, token.lineNumber, token.line);
} }
else if (allowed.length > 0 && else if ((allowed.length > 0) && !allowed.includes(lang)) {
!allowed.includes(lang.toLowerCase())) {
addError(onError, token.lineNumber, `"${lang}" is not allowed`); addError(onError, token.lineNumber, `"${lang}" is not allowed`);
} }
}); });

View file

@ -26,8 +26,8 @@ Plain text in a code block
```` ````
You can configure the `allowed_languages` parameter to specify a list of You can configure the `allowed_languages` parameter to specify a list of
languages code blocks could use. The default value is `[]` which means any languages code blocks could use. Languages are case sensitive. The default value
language specifier is valid. is `[]` which means any language specifier is valid.
Rationale: Specifying a language improves content rendering by using the Rationale: Specifying a language improves content rendering by using the
correct syntax highlighting for code. More information: correct syntax highlighting for code. More information:

View file

@ -1648,8 +1648,8 @@ Plain text in a code block
```` ````
You can configure the `allowed_languages` parameter to specify a list of You can configure the `allowed_languages` parameter to specify a list of
languages code blocks could use. The default value is `[]` which means any languages code blocks could use. Languages are case sensitive. The default value
language specifier is valid. is `[]` which means any language specifier is valid.
Rationale: Specifying a language improves content rendering by using the Rationale: Specifying a language improves content rendering by using the
correct syntax highlighting for code. More information: correct syntax highlighting for code. More information:

View file

@ -36,8 +36,8 @@ Plain text in a code block
```` ````
You can configure the `allowed_languages` parameter to specify a list of You can configure the `allowed_languages` parameter to specify a list of
languages code blocks could use. The default value is `[]` which means any languages code blocks could use. Languages are case sensitive. The default value
language specifier is valid. is `[]` which means any language specifier is valid.
Rationale: Specifying a language improves content rendering by using the Rationale: Specifying a language improves content rendering by using the
correct syntax highlighting for code. More information: correct syntax highlighting for code. More information:

View file

@ -10,17 +10,12 @@ module.exports = {
"tags": [ "code", "language" ], "tags": [ "code", "language" ],
"function": function MD040(params, onError) { "function": function MD040(params, onError) {
let allowed = params.config.allowed_languages; let allowed = params.config.allowed_languages;
allowed = Array.isArray(allowed) ? allowed = Array.isArray(allowed) ? allowed : [];
allowed.map((lang) => lang.toLowerCase()) :
[];
filterTokens(params, "fence", function forToken(token) { filterTokens(params, "fence", function forToken(token) {
const lang = token.info.trim(); const lang = token.info.trim().split(/\s+/u).shift();
if (lang === "") { if (lang === "") {
addErrorContext(onError, token.lineNumber, token.line); addErrorContext(onError, token.lineNumber, token.line);
} else if ( } else if ((allowed.length > 0) && !allowed.includes(lang)) {
allowed.length > 0 &&
!allowed.includes(lang.toLowerCase())
) {
addError( addError(
onError, onError,
token.lineNumber, token.lineNumber,

View file

@ -12,15 +12,33 @@ Code block with `css` not in allowed_languages:
body {} {MD040:11} body {} {MD040:11}
``` ```
Code block with `MD` (uppercase) not in allowed_languages:
```MD
hello md {MD040:17}
```
Code block with `ts` (lowercase) not in allowed_languages:
```ts
let foo = "bar"; {MD040:23}
```
Code block with `js` in allowed_languages: Code block with `js` in allowed_languages:
```js ```js
console.log('markdownlint') console.log('markdownlint')
``` ```
Code block with `scss` in allowed_languages: Code block with `js foo` allowed_languages:
```scss ```js foo
console.log('bar')
```
Code block with ` scss` (prefixed by a space) in allowed_languages: {MD038}
``` scss
body { body {
h1 { h1 {
color: red; color: red;
@ -28,15 +46,15 @@ body {
} }
``` ```
Code block with `md` in allowed_languages: Code block with `md` (lowercase) in allowed_languages:
```MD ```md
hello md hello md
``` ```
Code block with `TS` in allowed_languages: Code block with `TS` (uppercase) in allowed_languages:
```ts ```TS
body { body {
h1 { h1 {
color: red; color: red;

View file

@ -29324,6 +29324,26 @@ Generated by [AVA](https://avajs.dev).
{ {
errors: [ errors: [
{
errorContext: '` scss`',
errorDetail: null,
errorRange: [
17,
7,
],
fixInfo: {
deleteCount: 5,
editColumn: 18,
insertText: 'scss',
},
lineNumber: 39,
ruleDescription: 'Spaces inside code span elements',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md038.md',
ruleNames: [
'MD038',
'no-space-in-code',
],
},
{ {
errorContext: null, errorContext: null,
errorDetail: '"html" is not allowed', errorDetail: '"html" is not allowed',
@ -29350,6 +29370,32 @@ Generated by [AVA](https://avajs.dev).
'fenced-code-language', 'fenced-code-language',
], ],
}, },
{
errorContext: null,
errorDetail: '"MD" is not allowed',
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',
],
},
{
errorContext: null,
errorDetail: '"ts" is not allowed',
errorRange: null,
fixInfo: null,
lineNumber: 23,
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-allowed_languages.md␊ fixed: `# md040-allowed_languages.md␊
@ -29365,15 +29411,33 @@ Generated by [AVA](https://avajs.dev).
body {} {MD040:11}␊ body {} {MD040:11}␊
\`\`\`␊ \`\`\`␊
Code block with \`MD\` (uppercase) not in allowed_languages:␊
\`\`\`MD␊
hello md {MD040:17}␊
\`\`\`␊
Code block with \`ts\` (lowercase) not in allowed_languages:␊
\`\`\`ts␊
let foo = "bar"; {MD040:23}␊
\`\`\`␊
Code block with \`js\` in allowed_languages:␊ Code block with \`js\` in allowed_languages:␊
\`\`\`js␊ \`\`\`js␊
console.log('markdownlint')␊ console.log('markdownlint')␊
\`\`\`␊ \`\`\`␊
Code block with \`scss\` in allowed_languages:␊ Code block with \`js foo\` allowed_languages:␊
\`\`\`scss␊ \`\`\`js foo␊
console.log('bar')␊
\`\`\`␊
Code block with \`scss\` (prefixed by a space) in allowed_languages: {MD038}␊
\`\`\` scss␊
body {␊ body {␊
h1 {␊ h1 {␊
color: red;␊ color: red;␊
@ -29381,15 +29445,15 @@ Generated by [AVA](https://avajs.dev).
}␊ }␊
\`\`\`␊ \`\`\`␊
Code block with \`md\` in allowed_languages:␊ Code block with \`md\` (lowercase) in allowed_languages:␊
\`\`\`MD \`\`\`md
hello md␊ hello md␊
\`\`\`␊ \`\`\`␊
Code block with \`TS\` in allowed_languages:␊ Code block with \`TS\` (uppercase) in allowed_languages:␊
\`\`\`ts \`\`\`TS
body {␊ body {␊
h1 {␊ h1 {␊
color: red;␊ color: red;␊