Skip to content

Commit d54312a

Browse files
committed
Update MD055/table-pipe-style to add "per_table" style allowing tables in a document to have different (internally consistent) styles (closes #1818).
1 parent 14f3ea1 commit d54312a

11 files changed

Lines changed: 200 additions & 16 deletions

‎doc-build/md055.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
This rule is triggered when a [GitHub Flavored Markdown table][gfm-table-055]
2-
is inconsistent about its use of leading and trailing pipe characters (`|`).
2+
is inconsistent with its use of leading and trailing pipe characters (`|`).
33

44
By default (`consistent` style), the header row of the first table in a document
55
is used to determine the style that is enforced for every table in the document.
66
A specific style can be used instead (`leading_and_trailing`, `leading_only`,
7-
`no_leading_or_trailing`, `trailing_only`).
7+
`no_leading_or_trailing`, `trailing_only`) to require all tables in a document
8+
have the same approach. The style `per_table` can be used to require the rows of
9+
each table use the same approach (based on its header row) while allowing tables
10+
in a document to differ from each other.
811

912
This table's header row has leading and trailing pipes, but its delimiter row is
1013
missing the trailing pipe and its first row of cells is missing the leading

‎doc/Rules.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,15 +2552,18 @@ Parameters:
25522552

25532553
- `style`: Table pipe style (`string`, default `consistent`, values
25542554
`consistent` / `leading_and_trailing` / `leading_only` /
2555-
`no_leading_or_trailing` / `trailing_only`)
2555+
`no_leading_or_trailing` / `per_table` / `trailing_only`)
25562556

25572557
This rule is triggered when a [GitHub Flavored Markdown table][gfm-table-055]
2558-
is inconsistent about its use of leading and trailing pipe characters (`|`).
2558+
is inconsistent with its use of leading and trailing pipe characters (`|`).
25592559

25602560
By default (`consistent` style), the header row of the first table in a document
25612561
is used to determine the style that is enforced for every table in the document.
25622562
A specific style can be used instead (`leading_and_trailing`, `leading_only`,
2563-
`no_leading_or_trailing`, `trailing_only`).
2563+
`no_leading_or_trailing`, `trailing_only`) to require all tables in a document
2564+
have the same approach. The style `per_table` can be used to require the rows of
2565+
each table use the same approach (based on its header row) while allowing tables
2566+
in a document to differ from each other.
25642567

25652568
This table's header row has leading and trailing pipes, but its delimiter row is
25662569
missing the trailing pipe and its first row of cells is missing the leading

‎doc/md055.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ Parameters:
88

99
- `style`: Table pipe style (`string`, default `consistent`, values
1010
`consistent` / `leading_and_trailing` / `leading_only` /
11-
`no_leading_or_trailing` / `trailing_only`)
11+
`no_leading_or_trailing` / `per_table` / `trailing_only`)
1212

1313
This rule is triggered when a [GitHub Flavored Markdown table][gfm-table-055]
14-
is inconsistent about its use of leading and trailing pipe characters (`|`).
14+
is inconsistent with its use of leading and trailing pipe characters (`|`).
1515

1616
By default (`consistent` style), the header row of the first table in a document
1717
is used to determine the style that is enforced for every table in the document.
1818
A specific style can be used instead (`leading_and_trailing`, `leading_only`,
19-
`no_leading_or_trailing`, `trailing_only`).
19+
`no_leading_or_trailing`, `trailing_only`) to require all tables in a document
20+
have the same approach. The style `per_table` can be used to require the rows of
21+
each table use the same approach (based on its header row) while allowing tables
22+
in a document to differ from each other.
2023

2124
This table's header row has leading and trailing pipes, but its delimiter row is
2225
missing the trailing pipe and its first row of cells is missing the leading

‎lib/configuration-strict.d.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,7 +2144,13 @@ export interface ConfigurationStrict {
21442144
/**
21452145
* Table pipe style
21462146
*/
2147-
style?: "consistent" | "leading_only" | "trailing_only" | "leading_and_trailing" | "no_leading_or_trailing";
2147+
style?:
2148+
| "consistent"
2149+
| "leading_only"
2150+
| "trailing_only"
2151+
| "leading_and_trailing"
2152+
| "no_leading_or_trailing"
2153+
| "per_table";
21482154
};
21492155
/**
21502156
* MD055/table-pipe-style : Table pipe style : https://github.com/DavidAnson/markdownlint/blob/v0.41.1/doc/md055.md
@@ -2164,7 +2170,13 @@ export interface ConfigurationStrict {
21642170
/**
21652171
* Table pipe style
21662172
*/
2167-
style?: "consistent" | "leading_only" | "trailing_only" | "leading_and_trailing" | "no_leading_or_trailing";
2173+
style?:
2174+
| "consistent"
2175+
| "leading_only"
2176+
| "trailing_only"
2177+
| "leading_and_trailing"
2178+
| "no_leading_or_trailing"
2179+
| "per_table";
21682180
};
21692181
/**
21702182
* MD056/table-column-count : Table column count : https://github.com/DavidAnson/markdownlint/blob/v0.41.1/doc/md056.md

‎lib/md055.mjs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22

33
import { addErrorDetailIf } from "../helpers/helpers.cjs";
4+
import { getParentOfType } from "../helpers/micromark-helpers.cjs";
45
import { filterByTypesCached } from "./cache.mjs";
56

67
/** @typedef {import("markdownlint").MicromarkToken} MicromarkToken */
@@ -27,8 +28,17 @@ export default {
2728
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "trailing_only"));
2829
let expectedTrailingPipe =
2930
((expectedStyle !== "no_leading_or_trailing") && (expectedStyle !== "leading_only"));
31+
let currentTable = null;
3032
const rows = filterByTypesCached([ "tableDelimiterRow", "tableRow" ]);
3133
for (const row of rows) {
34+
if (style === "per_table") {
35+
// Reset style for each new table
36+
const rowTable = getParentOfType(row, [ "table" ]);
37+
if (rowTable !== currentTable) {
38+
expectedStyle = "consistent";
39+
currentTable = rowTable;
40+
}
41+
}
3242
// The following uses of first/lastOrNothing lack fallback handling
3343
// because it seems not to be possible (i.e., 0% coverage)
3444
const firstCell = firstOrNothing(row.children);

‎schema/build-config-schema.mjs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ for (const rule of rules) {
617617
"leading_only",
618618
"trailing_only",
619619
"leading_and_trailing",
620-
"no_leading_or_trailing"
620+
"no_leading_or_trailing",
621+
"per_table"
621622
],
622623
"default": "consistent"
623624
};

‎schema/markdownlint-config-schema-strict.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4379,7 +4379,8 @@
43794379
"leading_only",
43804380
"trailing_only",
43814381
"leading_and_trailing",
4382-
"no_leading_or_trailing"
4382+
"no_leading_or_trailing",
4383+
"per_table"
43834384
],
43844385
"default": "consistent"
43854386
}
@@ -4426,7 +4427,8 @@
44264427
"leading_only",
44274428
"trailing_only",
44284429
"leading_and_trailing",
4429-
"no_leading_or_trailing"
4430+
"no_leading_or_trailing",
4431+
"per_table"
44304432
],
44314433
"default": "consistent"
44324434
}

‎schema/markdownlint-config-schema.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4379,7 +4379,8 @@
43794379
"leading_only",
43804380
"trailing_only",
43814381
"leading_and_trailing",
4382-
"no_leading_or_trailing"
4382+
"no_leading_or_trailing",
4383+
"per_table"
43834384
],
43844385
"default": "consistent"
43854386
}
@@ -4426,7 +4427,8 @@
44264427
"leading_only",
44274428
"trailing_only",
44284429
"leading_and_trailing",
4429-
"no_leading_or_trailing"
4430+
"no_leading_or_trailing",
4431+
"per_table"
44304432
],
44314433
"default": "consistent"
44324434
}

‎test/markdownlint-test-scenarios.mjs.snapshot‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78217,6 +78217,119 @@ exports[`markdownlint-test-scenarios.mjs > table-pipe-style-explicit-none.md 1`]
7821778217
}
7821878218
`;
7821978219

78220+
exports[`markdownlint-test-scenarios.mjs > table-pipe-style-explicit-per-table.md 1`] = `
78221+
{
78222+
"errors": [
78223+
{
78224+
"lineNumber": 8,
78225+
"ruleNames": [
78226+
"MD055",
78227+
"table-pipe-style"
78228+
],
78229+
"ruleDescription": "Table pipe style",
78230+
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md055.md",
78231+
"errorDetail": "Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe",
78232+
"errorContext": null,
78233+
"errorRange": [
78234+
14,
78235+
1
78236+
],
78237+
"fixInfo": null,
78238+
"severity": "error"
78239+
},
78240+
{
78241+
"lineNumber": 15,
78242+
"ruleNames": [
78243+
"MD055",
78244+
"table-pipe-style"
78245+
],
78246+
"ruleDescription": "Table pipe style",
78247+
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md055.md",
78248+
"errorDetail": "Expected: no_leading_or_trailing; Actual: trailing_only; Unexpected trailing pipe",
78249+
"errorContext": null,
78250+
"errorRange": [
78251+
14,
78252+
1
78253+
],
78254+
"fixInfo": null,
78255+
"severity": "error"
78256+
},
78257+
{
78258+
"lineNumber": 22,
78259+
"ruleNames": [
78260+
"MD055",
78261+
"table-pipe-style"
78262+
],
78263+
"ruleDescription": "Table pipe style",
78264+
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md055.md",
78265+
"errorDetail": "Expected: leading_only; Actual: no_leading_or_trailing; Missing leading pipe",
78266+
"errorContext": null,
78267+
"errorRange": [
78268+
3,
78269+
1
78270+
],
78271+
"fixInfo": null,
78272+
"severity": "error"
78273+
},
78274+
{
78275+
"lineNumber": 29,
78276+
"ruleNames": [
78277+
"MD055",
78278+
"table-pipe-style"
78279+
],
78280+
"ruleDescription": "Table pipe style",
78281+
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/md055.md",
78282+
"errorDetail": "Expected: trailing_only; Actual: no_leading_or_trailing; Missing trailing pipe",
78283+
"errorContext": null,
78284+
"errorRange": [
78285+
12,
78286+
1
78287+
],
78288+
"fixInfo": null,
78289+
"severity": "error"
78290+
}
78291+
],
78292+
"fixed": [
78293+
"# Table Pipe Style Per Table",
78294+
"",
78295+
"## Style: both {MD055:+5}",
78296+
"",
78297+
"| Table | Heading |",
78298+
"| ----- | ------- |",
78299+
"| Cell | Cell |",
78300+
"| Cell | Cell",
78301+
"",
78302+
"## Style: none {MD055:+5}",
78303+
"",
78304+
"Table | Heading",
78305+
"----- | -------",
78306+
"Cell | Cell",
78307+
"Cell | Cell |",
78308+
"",
78309+
"## Style: leading {MD055:+5}",
78310+
"",
78311+
"| Table | Heading",
78312+
"| ----- | -------",
78313+
"| Cell | Cell",
78314+
" Cell | Cell",
78315+
"",
78316+
"## Style: trailing {MD055:+5}",
78317+
"",
78318+
"Table | Heading |",
78319+
"----- | ------- |",
78320+
"Cell | Cell |",
78321+
"Cell | Cell",
78322+
"",
78323+
"<!-- markdownlint-configure-file {",
78324+
" \\"table-pipe-style\\": {",
78325+
" \\"style\\": \\"per_table\\"",
78326+
" }",
78327+
"} -->",
78328+
""
78329+
]
78330+
}
78331+
`;
78332+
7822078333
exports[`markdownlint-test-scenarios.mjs > table-pipe-style-explicit-trailing.md 1`] = `
7822178334
{
7822278335
"errors": [

‎test/markdownlint-test.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ test.suite(import.meta.url.replace(/^.*?\/(?<name>[^/]*)$/u, "$<name>"), () => {
10941094
});
10951095

10961096
test("validateJsonUsingConfigSchemaStrict", async(t) => {
1097-
t.plan(236);
1097+
t.plan(237);
10981098
// @ts-ignore
10991099
const ajv = new Ajv(ajvOptions);
11001100
const validateSchemaStrict = ajv.compile(configSchemaStrict);

0 commit comments

Comments
 (0)