MD033/no-inline-html: Add table_allowed_elements parameter.

This commit is contained in:
Alexandre Feblot 2025-05-27 01:32:15 +02:00 committed by David Anson
parent c6e2ee1488
commit 1ac0b22804
16 changed files with 432 additions and 8 deletions

View file

@ -10,7 +10,13 @@ To fix this, use 'pure' Markdown instead of including raw HTML:
# Markdown heading
```
Note: To allow specific HTML elements, use the `allowed_elements` parameter.
Discussions of parameters are usually in paragraph form. What about:
To allow specific HTML elements anywhere in Markdown content, set the
`allowed_elements` parameter to a list of HTML element names. To allow
a specific set of HTML elements within Markdown tables, set the
`table_allowed_elements` to a list of HTML element names. This can be
used to permit the use of `<br>` line breaks only within Markdown tables.
Rationale: Raw HTML is allowed in Markdown, but this rule is included for
those who want their documents to only include "pure" Markdown, or for those

View file

@ -1327,6 +1327,8 @@ Aliases: `no-inline-html`
Parameters:
- `allowed_elements`: Allowed elements (`string[]`, default `[]`)
- `table_allowed_elements`: Allowed elements in tables (`string[]`, default
`[]`)
This rule is triggered whenever raw HTML is used in a Markdown document:
@ -1340,7 +1342,13 @@ To fix this, use 'pure' Markdown instead of including raw HTML:
# Markdown heading
```
Note: To allow specific HTML elements, use the `allowed_elements` parameter.
Discussions of parameters are usually in paragraph form. What about:
To allow specific HTML elements anywhere in Markdown content, set the
`allowed_elements` parameter to a list of HTML element names. To allow
a specific set of HTML elements within Markdown tables, set the
`table_allowed_elements` to a list of HTML element names. This can be
used to permit the use of `<br>` line breaks only within Markdown tables.
Rationale: Raw HTML is allowed in Markdown, but this rule is included for
those who want their documents to only include "pure" Markdown, or for those

View file

@ -7,6 +7,8 @@ Aliases: `no-inline-html`
Parameters:
- `allowed_elements`: Allowed elements (`string[]`, default `[]`)
- `table_allowed_elements`: Allowed elements in tables (`string[]`, default
`[]`)
This rule is triggered whenever raw HTML is used in a Markdown document:
@ -20,7 +22,13 @@ To fix this, use 'pure' Markdown instead of including raw HTML:
# Markdown heading
```
Note: To allow specific HTML elements, use the `allowed_elements` parameter.
Discussions of parameters are usually in paragraph form. What about:
To allow specific HTML elements anywhere in Markdown content, set the
`allowed_elements` parameter to a list of HTML element names. To allow
a specific set of HTML elements within Markdown tables, set the
`table_allowed_elements` to a list of HTML element names. This can be
used to permit the use of `<br>` line breaks only within Markdown tables.
Rationale: Raw HTML is allowed in Markdown, but this rule is included for
those who want their documents to only include "pure" Markdown, or for those

View file

@ -605,6 +605,10 @@ export interface ConfigurationStrict {
* Allowed elements
*/
allowed_elements?: string[];
/**
* Allowed elements in tables
*/
table_allowed_elements?: string[];
};
/**
* MD033/no-inline-html : Inline HTML : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md033.md
@ -616,6 +620,10 @@ export interface ConfigurationStrict {
* Allowed elements
*/
allowed_elements?: string[];
/**
* Allowed elements in tables
*/
table_allowed_elements?: string[];
};
/**
* MD034/no-bare-urls : Bare URL used : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md034.md

View file

@ -1,7 +1,7 @@
// @ts-check
import { addError, nextLinesRe } from "../helpers/helpers.cjs";
import { getHtmlTagInfo } from "../helpers/micromark-helpers.cjs";
import { getHtmlTagInfo, getParentOfType } from "../helpers/micromark-helpers.cjs";
import { filterByTypesCached } from "./cache.mjs";
/** @type {import("markdownlint").Rule} */
@ -14,12 +14,20 @@ export default {
let allowedElements = params.config.allowed_elements;
allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
allowedElements = allowedElements.map((element) => element.toLowerCase());
let tableAllowedElements = params.config.table_allowed_elements;
// if not defined, use allowedElements for backward compatibility
tableAllowedElements = Array.isArray(tableAllowedElements) ? tableAllowedElements : allowedElements;
tableAllowedElements = tableAllowedElements.map((element) => element.toLowerCase());
for (const token of filterByTypesCached([ "htmlText" ], true)) {
const htmlTagInfo = getHtmlTagInfo(token);
const elementName = htmlTagInfo?.name.toLowerCase();
const inTable = !!getParentOfType(token, [ "table" ]);
if (
htmlTagInfo &&
!htmlTagInfo.close &&
!allowedElements.includes(htmlTagInfo.name.toLowerCase())
!htmlTagInfo.close && !(
(!inTable && allowedElements.includes(elementName)) ||
(inTable && tableAllowedElements.includes(elementName))
)
) {
const range = [
token.startColumn,

View file

@ -169,7 +169,9 @@
// MD033/no-inline-html : Inline HTML : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md033.md
"MD033": {
// Allowed elements
"allowed_elements": []
"allowed_elements": [],
// Allowed elements in tables
"table_allowed_elements": []
},
// MD034/no-bare-urls : Bare URL used : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md034.md

View file

@ -154,6 +154,8 @@ MD032: true
MD033:
# Allowed elements
allowed_elements: []
# Allowed elements in tables
table_allowed_elements: []
# MD034/no-bare-urls : Bare URL used : https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md034.md
MD034: true

View file

@ -336,6 +336,14 @@ for (const rule of rules) {
"type": "string"
},
"default": []
},
"table_allowed_elements": {
"description": "Allowed elements in tables",
"type": "array",
"items": {
"type": "string"
},
"default": []
}
};
break;

View file

@ -934,6 +934,14 @@
"type": "string"
},
"default": []
},
"table_allowed_elements": {
"description": "Allowed elements in tables",
"type": "array",
"items": {
"type": "string"
},
"default": []
}
},
"additionalProperties": false
@ -953,6 +961,14 @@
"type": "string"
},
"default": []
},
"table_allowed_elements": {
"description": "Allowed elements in tables",
"type": "array",
"items": {
"type": "string"
},
"default": []
}
},
"additionalProperties": false

View file

@ -934,6 +934,14 @@
"type": "string"
},
"default": []
},
"table_allowed_elements": {
"description": "Allowed elements in tables",
"type": "array",
"items": {
"type": "string"
},
"default": []
}
},
"additionalProperties": false
@ -953,6 +961,14 @@
"type": "string"
},
"default": []
},
"table_allowed_elements": {
"description": "Allowed elements in tables",
"type": "array",
"items": {
"type": "string"
},
"default": []
}
},
"additionalProperties": false

View file

@ -54,6 +54,11 @@ This is not allowed.
<Br/> {MD033}
</P>
| Allowed | Not Allowed |
| ------- | ------------ |
| <h1> | |
| | <br> {MD033} |
<!-- markdownlint-configure-file {
"no-inline-html": {
"allowed_elements": [

View file

@ -0,0 +1,27 @@
# inline_html-only_table_allowed_elements.md
<h1>This is not allowed {MD033}</h1>
<br> This is not allowed {MD033}
<br/> This is not allowed {MD033}
| Allowed | Not Allowed |
| ------------------- | ------------------ |
| <br> | |
| <br/> | |
| <table><br></table> | |
| | <h1> {MD033} </h1> |
<table> {MD033}
<br/> {MD033}
</table>
<!-- markdownlint-configure-file {
"no-inline-html": {
"table_allowed_elements": [
"br",
"tAbLE"
]
}
} -->

View file

@ -0,0 +1,33 @@
# inline_html-table_allowed_elements.md
<h1>This is allowed</h1>
<h2>This is not allowed {MD033}</h2>
<br> This is not allowed {MD033}
<br/> This is not allowed {MD033}
| Allowed | Not Allowed |
| ------------------- | ------------------ |
| <br> | |
| <br/> | |
| <table><br></table> | |
| | <h1> {MD033} </h1> |
<table>
<br/> {MD033}
</table>
<!-- markdownlint-configure-file {
"no-inline-html": {
"allowed_elements": [
"h1",
"tAbLE"
],
"table_allowed_elements": [
"br",
"tAbLE"
]
}
} -->

View file

@ -908,7 +908,7 @@ test("readme", async(t) => {
});
test("validateJsonUsingConfigSchemaStrict", async(t) => {
t.plan(201);
t.plan(203);
// @ts-ignore
const ajv = new Ajv(ajvOptions);
const validateSchemaStrict = ajv.compile(configSchemaStrict);

View file

@ -24422,6 +24422,22 @@ Generated by [AVA](https://avajs.dev).
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: br',
errorRange: [
13,
4,
],
fixInfo: null,
lineNumber: 60,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
],
fixed: `# inline_html-allowed_elements␊
@ -24479,6 +24495,11 @@ Generated by [AVA](https://avajs.dev).
<Br/> {MD033}␊
</P>
| Allowed | Not Allowed |␊
| ------- | ------------ |␊
| <h1> | |␊
| | <br> {MD033} |␊
<!-- markdownlint-configure-file {␊
"no-inline-html": {␊
"allowed_elements": [␊
@ -24492,6 +24513,262 @@ Generated by [AVA](https://avajs.dev).
`,
}
## inline_html-only_table_allowed_elements.md
> Snapshot 1
{
errors: [
{
errorContext: null,
errorDetail: 'Element: h1',
errorRange: [
1,
4,
],
fixInfo: null,
lineNumber: 3,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: br',
errorRange: [
1,
4,
],
fixInfo: null,
lineNumber: 5,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: br',
errorRange: [
1,
5,
],
fixInfo: null,
lineNumber: 7,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: h1',
errorRange: [
25,
4,
],
fixInfo: null,
lineNumber: 14,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: table',
errorRange: [
1,
7,
],
fixInfo: null,
lineNumber: 16,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: br',
errorRange: [
3,
5,
],
fixInfo: null,
lineNumber: 17,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
],
fixed: `# inline_html-only_table_allowed_elements.md␊
<h1>This is not allowed {MD033}</h1>
<br> This is not allowed {MD033}␊
<br/> This is not allowed {MD033}␊
| Allowed | Not Allowed |␊
| ------------------- | ------------------ |␊
| <br> | |␊
| <br/> | |␊
| <table><br></table> | |␊
| | <h1> {MD033} </h1> |␊
<table> {MD033}␊
<br/> {MD033}␊
</table>
<!-- markdownlint-configure-file {␊
"no-inline-html": {␊
"table_allowed_elements": [␊
"br",␊
"tAbLE"␊
]␊
}␊
} -->␊
`,
}
## inline_html-table_allowed_elements.md
> Snapshot 1
{
errors: [
{
errorContext: null,
errorDetail: 'Element: h2',
errorRange: [
1,
4,
],
fixInfo: null,
lineNumber: 5,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: br',
errorRange: [
1,
4,
],
fixInfo: null,
lineNumber: 7,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: br',
errorRange: [
1,
5,
],
fixInfo: null,
lineNumber: 9,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: h1',
errorRange: [
25,
4,
],
fixInfo: null,
lineNumber: 16,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
{
errorContext: null,
errorDetail: 'Element: br',
errorRange: [
3,
5,
],
fixInfo: null,
lineNumber: 19,
ruleDescription: 'Inline HTML',
ruleInformation: 'https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md033.md',
ruleNames: [
'MD033',
'no-inline-html',
],
},
],
fixed: `# inline_html-table_allowed_elements.md␊
<h1>This is allowed</h1>
<h2>This is not allowed {MD033}</h2>
<br> This is not allowed {MD033}␊
<br/> This is not allowed {MD033}␊
| Allowed | Not Allowed |␊
| ------------------- | ------------------ |␊
| <br> | |␊
| <br/> | |␊
| <table><br></table> | |␊
| | <h1> {MD033} </h1> |␊
<table>
<br/> {MD033}␊
</table>
<!-- markdownlint-configure-file {␊
"no-inline-html": {␊
"allowed_elements": [␊
"h1",␊
"tAbLE"␊
],␊
"table_allowed_elements": [␊
"br",␊
"tAbLE"␊
]␊
}␊
} -->␊
`,
}
## inline_html.md
> Snapshot 1