diff --git a/doc-build/md033.md b/doc-build/md033.md index 98dc8a73..09544fb9 100644 --- a/doc-build/md033.md +++ b/doc-build/md033.md @@ -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 `
` 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 diff --git a/doc/Rules.md b/doc/Rules.md index 5c4b7988..babfe225 100644 --- a/doc/Rules.md +++ b/doc/Rules.md @@ -1321,6 +1321,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: @@ -1334,7 +1336,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 `
` 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 diff --git a/doc/md033.md b/doc/md033.md index d2f5ddec..bc0ca7ad 100644 --- a/doc/md033.md +++ b/doc/md033.md @@ -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 `
` 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 diff --git a/lib/configuration-strict.d.ts b/lib/configuration-strict.d.ts index 09cd8bd7..187ffabe 100644 --- a/lib/configuration-strict.d.ts +++ b/lib/configuration-strict.d.ts @@ -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 diff --git a/lib/md033.mjs b/lib/md033.mjs index d353c0d6..8d64a942 100644 --- a/lib/md033.mjs +++ b/lib/md033.mjs @@ -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, diff --git a/schema/.markdownlint.jsonc b/schema/.markdownlint.jsonc index fa91b697..75e4e0a0 100644 --- a/schema/.markdownlint.jsonc +++ b/schema/.markdownlint.jsonc @@ -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 diff --git a/schema/.markdownlint.yaml b/schema/.markdownlint.yaml index 3eb8d9be..6e56205d 100644 --- a/schema/.markdownlint.yaml +++ b/schema/.markdownlint.yaml @@ -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 diff --git a/schema/build-config-schema.mjs b/schema/build-config-schema.mjs index 90980553..b9469f58 100644 --- a/schema/build-config-schema.mjs +++ b/schema/build-config-schema.mjs @@ -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; diff --git a/schema/markdownlint-config-schema-strict.json b/schema/markdownlint-config-schema-strict.json index 2dea240e..0fe1484e 100644 --- a/schema/markdownlint-config-schema-strict.json +++ b/schema/markdownlint-config-schema-strict.json @@ -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 diff --git a/schema/markdownlint-config-schema.json b/schema/markdownlint-config-schema.json index f9f25809..a21c220f 100644 --- a/schema/markdownlint-config-schema.json +++ b/schema/markdownlint-config-schema.json @@ -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 diff --git a/test/inline_html-allowed_elements.md b/test/inline_html-allowed_elements.md index dc2d1dfe..f1f5f782 100644 --- a/test/inline_html-allowed_elements.md +++ b/test/inline_html-allowed_elements.md @@ -54,6 +54,11 @@ This is not allowed.
{MD033}

+| Allowed | Not Allowed | +| ------- | ------------ | +|

| | +| |
{MD033} | + diff --git a/test/inline_html-table_allowed_elements.md b/test/inline_html-table_allowed_elements.md new file mode 100644 index 00000000..45129674 --- /dev/null +++ b/test/inline_html-table_allowed_elements.md @@ -0,0 +1,33 @@ +# inline_html-table_allowed_elements.md + +

This is allowed

+ +

This is not allowed {MD033}

+ +
This is not allowed {MD033} + +
This is not allowed {MD033} + +| Allowed | Not Allowed | +| ------------------- | ------------------ | +|
| | +|
| | +|
| | +| |

{MD033}

| + + +
{MD033} +
+ + diff --git a/test/markdownlint-test.mjs b/test/markdownlint-test.mjs index 19cc6a85..5a000de7 100644 --- a/test/markdownlint-test.mjs +++ b/test/markdownlint-test.mjs @@ -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); diff --git a/test/snapshots/markdownlint-test-scenarios.mjs.md b/test/snapshots/markdownlint-test-scenarios.mjs.md index 74c8b973..142ed52d 100644 --- a/test/snapshots/markdownlint-test-scenarios.mjs.md +++ b/test/snapshots/markdownlint-test-scenarios.mjs.md @@ -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).
{MD033}␊

␊ ␊ + | Allowed | Not Allowed |␊ + | ------- | ------------ |␊ + |

| |␊ + | |
{MD033} |␊ + ␊ ␊ + `, + } + +## 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␊ + ␊ +

This is allowed

␊ + ␊ +

This is not allowed {MD033}

␊ + ␊ +
This is not allowed {MD033}␊ + ␊ +
This is not allowed {MD033}␊ + ␊ + | Allowed | Not Allowed |␊ + | ------------------- | ------------------ |␊ + |
| |␊ + |
| |␊ + |
| |␊ + | |

{MD033}

|␊ + ␊ + ␊ +
{MD033}␊ +
␊ + ␊ + ␊ + `, + } + ## inline_html.md > Snapshot 1 diff --git a/test/snapshots/markdownlint-test-scenarios.mjs.snap b/test/snapshots/markdownlint-test-scenarios.mjs.snap index 63289c2f..b5463bf5 100644 Binary files a/test/snapshots/markdownlint-test-scenarios.mjs.snap and b/test/snapshots/markdownlint-test-scenarios.mjs.snap differ