markdownlint 0.33.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/CONTRIBUTING.md +4 -3
  3. package/README.md +25 -10
  4. package/demo/markdownlint-browser.js +3286 -2119
  5. package/doc/CustomRules.md +25 -13
  6. package/doc/Rules.md +106 -7
  7. package/doc/md013.md +8 -7
  8. package/doc/md034.md +5 -0
  9. package/doc/md044.md +10 -0
  10. package/doc/md049.md +3 -0
  11. package/doc/md050.md +3 -0
  12. package/doc/md051.md +26 -0
  13. package/doc/md058.md +48 -0
  14. package/helpers/README.md +3 -27
  15. package/helpers/helpers.js +60 -292
  16. package/helpers/micromark.cjs +153 -36
  17. package/helpers/package.json +2 -2
  18. package/lib/cache.js +62 -13
  19. package/lib/configuration-strict.d.ts +1185 -0
  20. package/lib/configuration.d.ts +3 -1173
  21. package/lib/constants.js +3 -2
  22. package/lib/markdownlint.d.ts +143 -61
  23. package/lib/markdownlint.js +151 -54
  24. package/lib/md001.js +17 -8
  25. package/lib/md003.js +16 -8
  26. package/lib/md004.js +57 -47
  27. package/lib/md005.js +7 -8
  28. package/lib/md007.js +13 -12
  29. package/lib/md009.js +52 -43
  30. package/lib/md010.js +50 -32
  31. package/lib/md011.js +18 -7
  32. package/lib/md012.js +19 -8
  33. package/lib/md013.js +46 -37
  34. package/lib/md014.js +37 -36
  35. package/lib/md018.js +20 -8
  36. package/lib/md019-md021.js +80 -0
  37. package/lib/md020.js +15 -6
  38. package/lib/md022.js +10 -12
  39. package/lib/md023.js +24 -22
  40. package/lib/md024.js +14 -8
  41. package/lib/md025.js +17 -9
  42. package/lib/md026.js +5 -6
  43. package/lib/md027.js +24 -43
  44. package/lib/md028.js +26 -14
  45. package/lib/md029.js +42 -33
  46. package/lib/md030.js +7 -8
  47. package/lib/md031.js +45 -22
  48. package/lib/md032.js +18 -30
  49. package/lib/md033.js +6 -4
  50. package/lib/md034.js +30 -9
  51. package/lib/md035.js +5 -3
  52. package/lib/md036.js +34 -39
  53. package/lib/md037.js +3 -0
  54. package/lib/md038.js +6 -5
  55. package/lib/md039.js +68 -52
  56. package/lib/md040.js +21 -21
  57. package/lib/md041.js +21 -26
  58. package/lib/md042.js +50 -36
  59. package/lib/md043.js +22 -13
  60. package/lib/md044.js +5 -2
  61. package/lib/md045.js +6 -4
  62. package/lib/md046.js +9 -8
  63. package/lib/md047.js +5 -2
  64. package/lib/md048.js +22 -13
  65. package/lib/md049-md050.js +19 -5
  66. package/lib/md051.js +18 -20
  67. package/lib/md052.js +5 -2
  68. package/lib/md053.js +5 -2
  69. package/lib/md054.js +12 -12
  70. package/lib/md055.js +10 -3
  71. package/lib/md056.js +18 -6
  72. package/lib/md058.js +43 -0
  73. package/lib/rules.js +11 -4
  74. package/package.json +22 -25
  75. package/schema/.markdownlint.jsonc +54 -51
  76. package/schema/.markdownlint.yaml +53 -50
  77. package/schema/ValidatingConfiguration.md +6 -8
  78. package/schema/markdownlint-config-schema-strict.json +1819 -0
  79. package/schema/markdownlint-config-schema.json +115 -105
  80. package/style/prettier.json +1 -0
  81. package/lib/md019.js +0 -39
  82. package/lib/md021.js +0 -62
package/lib/md013.js CHANGED
@@ -2,30 +2,23 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorDetailIf, filterTokens, forEachHeading, forEachLine,
6
- includesSorted } = require("../helpers");
7
- const { lineMetadata, referenceLinkImageData } = require("./cache");
5
+ const { addErrorDetailIf } = require("../helpers");
6
+ const { getReferenceLinkImageData } = require("./cache");
7
+ const { addRangeToSet, getDescendantsByType } = require("../helpers/micromark.cjs");
8
+ const { filterByTypesCached } = require("./cache");
8
9
 
9
10
  const longLineRePrefix = "^.{";
10
11
  const longLineRePostfixRelaxed = "}.*\\s.*$";
11
12
  const longLineRePostfixStrict = "}.+$";
12
- const linkOrImageOnlyLineRe = /^[es]*(?:lT?L|I)[ES]*$/;
13
13
  const sternModeRe = /^(?:[#>\s]*\s)?\S*$/;
14
- const tokenTypeMap = {
15
- "em_open": "e",
16
- "em_close": "E",
17
- "image": "I",
18
- "link_open": "l",
19
- "link_close": "L",
20
- "strong_open": "s",
21
- "strong_close": "S",
22
- "text": "T"
23
- };
24
14
 
15
+ // eslint-disable-next-line jsdoc/valid-types
16
+ /** @type import("./markdownlint").Rule */
25
17
  module.exports = {
26
18
  "names": [ "MD013", "line-length" ],
27
19
  "description": "Line length",
28
20
  "tags": [ "line_length" ],
21
+ "parser": "micromark",
29
22
  "function": function MD013(params, onError) {
30
23
  const lineLength = Number(params.config.line_length || 80);
31
24
  const headingLineLength =
@@ -48,26 +41,41 @@ module.exports = {
48
41
  const includeTables = (tables === undefined) ? true : !!tables;
49
42
  const headings = params.config.headings;
50
43
  const includeHeadings = (headings === undefined) ? true : !!headings;
51
- const headingLineNumbers = [];
52
- forEachHeading(params, (heading) => {
53
- headingLineNumbers.push(heading.lineNumber);
54
- });
55
- const linkOnlyLineNumbers = [];
56
- filterTokens(params, "inline", (token) => {
57
- let childTokenTypes = "";
58
- for (const child of token.children) {
59
- if (child.type !== "text" || child.content !== "") {
60
- childTokenTypes += tokenTypeMap[child.type] || "x";
61
- }
44
+ const headingLineNumbers = new Set();
45
+ for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
46
+ addRangeToSet(headingLineNumbers, heading.startLine, heading.endLine);
47
+ }
48
+ const codeBlockLineNumbers = new Set();
49
+ for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
50
+ addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
51
+ }
52
+ const tableLineNumbers = new Set();
53
+ for (const table of filterByTypesCached([ "table" ])) {
54
+ addRangeToSet(tableLineNumbers, table.startLine, table.endLine);
55
+ }
56
+ const linkLineNumbers = new Set();
57
+ for (const link of filterByTypesCached([ "autolink", "image", "link", "literalAutolink" ])) {
58
+ addRangeToSet(linkLineNumbers, link.startLine, link.endLine);
59
+ }
60
+ const paragraphDataLineNumbers = new Set();
61
+ for (const paragraph of filterByTypesCached([ "paragraph" ])) {
62
+ for (const data of getDescendantsByType(paragraph, [ "data" ])) {
63
+ addRangeToSet(paragraphDataLineNumbers, data.startLine, data.endLine);
62
64
  }
63
- if (linkOrImageOnlyLineRe.test(childTokenTypes)) {
64
- linkOnlyLineNumbers.push(token.lineNumber);
65
+ }
66
+ const linkOnlyLineNumbers = new Set();
67
+ for (const lineNumber of linkLineNumbers) {
68
+ if (!paragraphDataLineNumbers.has(lineNumber)) {
69
+ linkOnlyLineNumbers.add(lineNumber);
65
70
  }
66
- });
67
- const { definitionLineIndices } = referenceLinkImageData();
68
- forEachLine(lineMetadata(), (line, lineIndex, inCode, onFence, inTable) => {
71
+ }
72
+ const definitionLineIndices = new Set(getReferenceLinkImageData().definitionLineIndices);
73
+ for (let lineIndex = 0; lineIndex < params.lines.length; lineIndex++) {
74
+ const line = params.lines[lineIndex];
69
75
  const lineNumber = lineIndex + 1;
70
- const isHeading = includesSorted(headingLineNumbers, lineNumber);
76
+ const isHeading = headingLineNumbers.has(lineNumber);
77
+ const inCode = codeBlockLineNumbers.has(lineNumber);
78
+ const inTable = tableLineNumbers.has(lineNumber);
71
79
  const length = inCode ?
72
80
  codeLineLength :
73
81
  (isHeading ? headingLineLength : lineLength);
@@ -77,20 +85,21 @@ module.exports = {
77
85
  if ((includeCodeBlocks || !inCode) &&
78
86
  (includeTables || !inTable) &&
79
87
  (includeHeadings || !isHeading) &&
80
- !includesSorted(definitionLineIndices, lineIndex) &&
88
+ !definitionLineIndices.has(lineIndex) &&
81
89
  (strict ||
82
90
  (!(stern && sternModeRe.test(line)) &&
83
- !includesSorted(linkOnlyLineNumbers, lineNumber))) &&
91
+ !linkOnlyLineNumbers.has(lineNumber))) &&
84
92
  lengthRe.test(line)) {
85
93
  addErrorDetailIf(
86
94
  onError,
87
95
  lineNumber,
88
96
  length,
89
97
  line.length,
90
- null,
91
- null,
92
- [ length + 1, line.length - length ]);
98
+ undefined,
99
+ undefined,
100
+ [ length + 1, line.length - length ]
101
+ );
93
102
  }
94
- });
103
+ }
95
104
  }
96
105
  };
package/lib/md014.js CHANGED
@@ -2,52 +2,53 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, filterTokens } = require("../helpers");
5
+ const { addErrorContext } = require("../helpers");
6
+ const { filterByTypes } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
6
8
 
7
9
  const dollarCommandRe = /^(\s*)(\$\s+)/;
8
10
 
11
+ // eslint-disable-next-line jsdoc/valid-types
12
+ /** @type import("./markdownlint").Rule */
9
13
  module.exports = {
10
14
  "names": [ "MD014", "commands-show-output" ],
11
15
  "description": "Dollar signs used before commands without showing output",
12
16
  "tags": [ "code" ],
17
+ "parser": "micromark",
13
18
  "function": function MD014(params, onError) {
14
- for (const type of [ "code_block", "fence" ]) {
15
- filterTokens(params, type, (token) => {
16
- const margin = (token.type === "fence") ? 1 : 0;
17
- const dollarInstances = [];
18
- let allDollars = true;
19
- for (let i = token.map[0] + margin; i < token.map[1] - margin; i++) {
20
- const line = params.lines[i];
21
- const lineTrim = line.trim();
22
- if (lineTrim) {
23
- const match = dollarCommandRe.exec(line);
24
- if (match) {
25
- const column = match[1].length + 1;
26
- const length = match[2].length;
27
- dollarInstances.push([ i, lineTrim, column, length ]);
28
- } else {
29
- allDollars = false;
19
+ for (const codeBlock of filterByTypesCached([ "codeFenced", "codeIndented" ])) {
20
+ const codeFlowValues = filterByTypes(
21
+ codeBlock.children,
22
+ [ "codeFlowValue" ]
23
+ );
24
+ const dollarMatches = codeFlowValues.
25
+ map((codeFlowValue) => ({
26
+ "result": codeFlowValue.text.match(dollarCommandRe),
27
+ "startColumn": codeFlowValue.startColumn,
28
+ "startLine": codeFlowValue.startLine,
29
+ "text": codeFlowValue.text
30
+ })).
31
+ filter((dollarMatch) => dollarMatch.result);
32
+ if (dollarMatches.length === codeFlowValues.length) {
33
+ for (const dollarMatch of dollarMatches) {
34
+ // @ts-ignore
35
+ const column = dollarMatch.startColumn + dollarMatch.result[1].length;
36
+ // @ts-ignore
37
+ const length = dollarMatch.result[2].length;
38
+ addErrorContext(
39
+ onError,
40
+ dollarMatch.startLine,
41
+ dollarMatch.text,
42
+ undefined,
43
+ undefined,
44
+ [ column, length ],
45
+ {
46
+ "editColumn": column,
47
+ "deleteCount": length
30
48
  }
31
- }
49
+ );
32
50
  }
33
- if (allDollars) {
34
- for (const instance of dollarInstances) {
35
- const [ i, lineTrim, column, length ] = instance;
36
- addErrorContext(
37
- onError,
38
- i + 1,
39
- lineTrim,
40
- null,
41
- null,
42
- [ column, length ],
43
- {
44
- "editColumn": column,
45
- "deleteCount": length
46
- }
47
- );
48
- }
49
- }
50
- });
51
+ }
51
52
  }
52
53
  }
53
54
  };
package/lib/md018.js CHANGED
@@ -2,26 +2,38 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, forEachLine } = require("../helpers");
6
- const { lineMetadata } = require("./cache");
5
+ const { addErrorContext } = require("../helpers");
6
+ const { addRangeToSet } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
7
8
 
9
+ // eslint-disable-next-line jsdoc/valid-types
10
+ /** @type import("./markdownlint").Rule */
8
11
  module.exports = {
9
12
  "names": [ "MD018", "no-missing-space-atx" ],
10
13
  "description": "No space after hash on atx style heading",
11
14
  "tags": [ "headings", "atx", "spaces" ],
15
+ "parser": "micromark",
12
16
  "function": function MD018(params, onError) {
13
- forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
14
- if (!inCode &&
17
+ const { lines } = params;
18
+ const ignoreBlockLineNumbers = new Set();
19
+ for (const ignoreBlock of filterByTypesCached([ "codeFenced", "codeIndented", "htmlFlow" ])) {
20
+ addRangeToSet(ignoreBlockLineNumbers, ignoreBlock.startLine, ignoreBlock.endLine);
21
+ }
22
+ for (const [ lineIndex, line ] of lines.entries()) {
23
+ if (
24
+ !ignoreBlockLineNumbers.has(lineIndex + 1) &&
15
25
  /^#+[^# \t]/.test(line) &&
16
26
  !/#\s*$/.test(line) &&
17
- !line.startsWith("#️⃣")) {
27
+ !line.startsWith("#️⃣")
28
+ ) {
29
+ // @ts-ignore
18
30
  const hashCount = /^#+/.exec(line)[0].length;
19
31
  addErrorContext(
20
32
  onError,
21
33
  lineIndex + 1,
22
34
  line.trim(),
23
- null,
24
- null,
35
+ undefined,
36
+ undefined,
25
37
  [ 1, hashCount + 1 ],
26
38
  {
27
39
  "editColumn": hashCount + 1,
@@ -29,6 +41,6 @@ module.exports = {
29
41
  }
30
42
  );
31
43
  }
32
- });
44
+ }
33
45
  }
34
46
  };
@@ -0,0 +1,80 @@
1
+ // @ts-check
2
+
3
+ "use strict";
4
+
5
+ const { addErrorContext } = require("../helpers/helpers");
6
+ const { getHeadingStyle } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
8
+
9
+ /**
10
+ * Validate heading sequence and whitespace length at start or end.
11
+ *
12
+ * @param {import("./markdownlint").RuleOnError} onError Error-reporting callback.
13
+ * @param {import("./markdownlint").MicromarkToken} heading ATX heading token.
14
+ * @param {number} delta Direction to scan.
15
+ * @returns {void}
16
+ */
17
+ function validateHeadingSpaces(onError, heading, delta) {
18
+ const { children, startLine, text } = heading;
19
+ let index = (delta > 0) ? 0 : (children.length - 1);
20
+ while (
21
+ children[index] &&
22
+ (children[index].type !== "atxHeadingSequence")
23
+ ) {
24
+ index += delta;
25
+ }
26
+ const headingSequence = children[index];
27
+ const whitespace = children[index + delta];
28
+ if (
29
+ (headingSequence?.type === "atxHeadingSequence") &&
30
+ (whitespace?.type === "whitespace") &&
31
+ (whitespace.text.length > 1)
32
+ ) {
33
+ const column = whitespace.startColumn + 1;
34
+ const length = whitespace.endColumn - column;
35
+ addErrorContext(
36
+ onError,
37
+ startLine,
38
+ text.trim(),
39
+ delta > 0,
40
+ delta < 0,
41
+ [ column, length ],
42
+ {
43
+ "editColumn": column,
44
+ "deleteCount": length
45
+ }
46
+ );
47
+ }
48
+ }
49
+
50
+ // eslint-disable-next-line jsdoc/valid-types
51
+ /** @type import("./markdownlint").Rule[] */
52
+ module.exports = [
53
+ {
54
+ "names": [ "MD019", "no-multiple-space-atx" ],
55
+ "description": "Multiple spaces after hash on atx style heading",
56
+ "tags": [ "headings", "atx", "spaces" ],
57
+ "parser": "micromark",
58
+ "function": function MD019(params, onError) {
59
+ const atxHeadings = filterByTypesCached([ "atxHeading" ])
60
+ .filter((heading) => getHeadingStyle(heading) === "atx");
61
+ for (const atxHeading of atxHeadings) {
62
+ validateHeadingSpaces(onError, atxHeading, 1);
63
+ }
64
+ }
65
+ },
66
+ {
67
+ "names": [ "MD021", "no-multiple-space-closed-atx" ],
68
+ "description": "Multiple spaces inside hashes on closed atx style heading",
69
+ "tags": [ "headings", "atx_closed", "spaces" ],
70
+ "parser": "micromark",
71
+ "function": function MD021(params, onError) {
72
+ const atxClosedHeadings = filterByTypesCached([ "atxHeading" ])
73
+ .filter((heading) => getHeadingStyle(heading) === "atx_closed");
74
+ for (const atxClosedHeading of atxClosedHeadings) {
75
+ validateHeadingSpaces(onError, atxClosedHeading, 1);
76
+ validateHeadingSpaces(onError, atxClosedHeading, -1);
77
+ }
78
+ }
79
+ }
80
+ ];
package/lib/md020.js CHANGED
@@ -2,16 +2,25 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, forEachLine } = require("../helpers");
6
- const { lineMetadata } = require("./cache");
5
+ const { addErrorContext } = require("../helpers");
6
+ const { addRangeToSet } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
7
8
 
9
+ // eslint-disable-next-line jsdoc/valid-types
10
+ /** @type import("./markdownlint").Rule */
8
11
  module.exports = {
9
12
  "names": [ "MD020", "no-missing-space-closed-atx" ],
10
13
  "description": "No space inside hashes on closed atx style heading",
11
14
  "tags": [ "headings", "atx_closed", "spaces" ],
15
+ "parser": "micromark",
12
16
  "function": function MD020(params, onError) {
13
- forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
14
- if (!inCode) {
17
+ const { lines } = params;
18
+ const ignoreBlockLineNumbers = new Set();
19
+ for (const ignoreBlock of filterByTypesCached([ "codeFenced", "codeIndented", "htmlFlow" ])) {
20
+ addRangeToSet(ignoreBlockLineNumbers, ignoreBlock.startLine, ignoreBlock.endLine);
21
+ }
22
+ for (const [ lineIndex, line ] of lines.entries()) {
23
+ if (!ignoreBlockLineNumbers.has(lineIndex + 1)) {
15
24
  const match =
16
25
  /^(#+)([ \t]*)([^#]*?[^#\\])([ \t]*)((?:\\#)?)(#+)(\s*)$/.exec(line);
17
26
  if (match) {
@@ -28,7 +37,7 @@ module.exports = {
28
37
  const leftHashLength = leftHash.length;
29
38
  const rightHashLength = rightHash.length;
30
39
  const left = !leftSpaceLength;
31
- const right = !rightSpaceLength || rightEscape;
40
+ const right = !rightSpaceLength || !!rightEscape;
32
41
  const rightEscapeReplacement = rightEscape ? `${rightEscape} ` : "";
33
42
  if (left || right) {
34
43
  const range = left ?
@@ -57,6 +66,6 @@ module.exports = {
57
66
  }
58
67
  }
59
68
  }
60
- });
69
+ }
61
70
  }
62
71
  };
package/lib/md022.js CHANGED
@@ -2,10 +2,9 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorDetailIf, blockquotePrefixRe, isBlankLine } =
6
- require("../helpers");
7
- const { filterByTypes, getHeadingLevel, inHtmlFlow } =
8
- require("../helpers/micromark.cjs");
5
+ const { addErrorDetailIf, blockquotePrefixRe, isBlankLine } = require("../helpers");
6
+ const { getHeadingLevel } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
9
8
 
10
9
  const defaultLines = 1;
11
10
 
@@ -31,19 +30,18 @@ const getBlockQuote = (str, count) => (
31
30
  .repeat(count)
32
31
  );
33
32
 
33
+ // eslint-disable-next-line jsdoc/valid-types
34
+ /** @type import("./markdownlint").Rule */
34
35
  module.exports = {
35
36
  "names": [ "MD022", "blanks-around-headings" ],
36
37
  "description": "Headings should be surrounded by blank lines",
37
38
  "tags": [ "headings", "blank_lines" ],
39
+ "parser": "micromark",
38
40
  "function": function MD022(params, onError) {
39
41
  const getLinesAbove = getLinesFunction(params.config.lines_above);
40
42
  const getLinesBelow = getLinesFunction(params.config.lines_below);
41
- const { lines, parsers } = params;
42
- const headings = filterByTypes(
43
- parsers.micromark.tokens,
44
- [ "atxHeading", "setextHeading" ]
45
- ).filter((heading) => !inHtmlFlow(heading));
46
- for (const heading of headings) {
43
+ const { lines } = params;
44
+ for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
47
45
  const { startLine, endLine } = heading;
48
46
  const line = lines[startLine - 1].trim();
49
47
 
@@ -65,7 +63,7 @@ module.exports = {
65
63
  actualAbove,
66
64
  "Above",
67
65
  line,
68
- null,
66
+ undefined,
69
67
  {
70
68
  "insertText": getBlockQuote(
71
69
  lines[startLine - 2],
@@ -93,7 +91,7 @@ module.exports = {
93
91
  actualBelow,
94
92
  "Below",
95
93
  line,
96
- null,
94
+ undefined,
97
95
  {
98
96
  "lineNumber": endLine + 1,
99
97
  "insertText": getBlockQuote(
package/lib/md023.js CHANGED
@@ -2,37 +2,39 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, filterTokens } = require("../helpers");
6
-
7
- const spaceBeforeHeadingRe = /^(\s+|[>\s]+\s\s)[^>\s]/;
5
+ const { addErrorContext } = require("../helpers");
6
+ const { filterByTypesCached } = require("./cache");
8
7
 
8
+ // eslint-disable-next-line jsdoc/valid-types
9
+ /** @type import("./markdownlint").Rule */
9
10
  module.exports = {
10
11
  "names": [ "MD023", "heading-start-left" ],
11
12
  "description": "Headings must start at the beginning of the line",
12
13
  "tags": [ "headings", "spaces" ],
14
+ "parser": "micromark",
13
15
  "function": function MD023(params, onError) {
14
- filterTokens(params, "heading_open", function forToken(token) {
15
- const { lineNumber, line } = token;
16
- const match = line.match(spaceBeforeHeadingRe);
17
- if (match) {
18
- const [ prefixAndFirstChar, prefix ] = match;
19
- let deleteCount = prefix.length;
20
- const prefixLengthNoSpace = prefix.trimEnd().length;
21
- if (prefixLengthNoSpace) {
22
- deleteCount -= prefixLengthNoSpace - 1;
23
- }
16
+ const headings = filterByTypesCached([ "atxHeading", "linePrefix", "setextHeading" ]);
17
+ for (let i = 0; i < headings.length - 1; i++) {
18
+ if (
19
+ (headings[i].type === "linePrefix") &&
20
+ (headings[i + 1].type !== "linePrefix") &&
21
+ (headings[i].startLine === headings[i + 1].startLine)
22
+ ) {
23
+ const { endColumn, startColumn, startLine } = headings[i];
24
+ const length = endColumn - startColumn;
24
25
  addErrorContext(
25
26
  onError,
26
- lineNumber,
27
- line,
28
- null,
29
- null,
30
- [ 1, prefixAndFirstChar.length ],
27
+ startLine,
28
+ params.lines[startLine - 1],
29
+ true,
30
+ false,
31
+ [ startColumn, length ],
31
32
  {
32
- "editColumn": prefixLengthNoSpace + 1,
33
- "deleteCount": deleteCount
34
- });
33
+ "editColumn": startColumn,
34
+ "deleteCount": length
35
+ }
36
+ );
35
37
  }
36
- });
38
+ }
37
39
  }
38
40
  };
package/lib/md024.js CHANGED
@@ -2,20 +2,26 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, forEachHeading } = require("../helpers");
5
+ const { addErrorContext } = require("../helpers");
6
+ const { getHeadingLevel, getHeadingText } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
6
8
 
9
+ // eslint-disable-next-line jsdoc/valid-types
10
+ /** @type import("./markdownlint").Rule */
7
11
  module.exports = {
8
12
  "names": [ "MD024", "no-duplicate-heading" ],
9
13
  "description": "Multiple headings with the same content",
10
14
  "tags": [ "headings" ],
15
+ "parser": "micromark",
11
16
  "function": function MD024(params, onError) {
12
17
  const siblingsOnly = !!params.config.siblings_only || false;
13
18
  const knownContents = [ null, [] ];
14
19
  let lastLevel = 1;
15
20
  let knownContent = knownContents[lastLevel];
16
- forEachHeading(params, (heading, content) => {
21
+ for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
22
+ const headingText = getHeadingText(heading);
17
23
  if (siblingsOnly) {
18
- const newLevel = heading.tag.slice(1);
24
+ const newLevel = getHeadingLevel(heading);
19
25
  while (lastLevel < newLevel) {
20
26
  lastLevel++;
21
27
  knownContents[lastLevel] = [];
@@ -27,16 +33,16 @@ module.exports = {
27
33
  knownContent = knownContents[newLevel];
28
34
  }
29
35
  // @ts-ignore
30
- if (knownContent.includes(content)) {
36
+ if (knownContent.includes(headingText)) {
31
37
  addErrorContext(
32
38
  onError,
33
- heading.lineNumber,
34
- heading.line.trim()
39
+ heading.startLine,
40
+ headingText.trim()
35
41
  );
36
42
  } else {
37
43
  // @ts-ignore
38
- knownContent.push(content);
44
+ knownContent.push(headingText);
39
45
  }
40
- });
46
+ }
41
47
  }
42
48
  };
package/lib/md025.js CHANGED
@@ -2,31 +2,39 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, filterTokens, frontMatterHasTitle } =
6
- require("../helpers");
5
+ const { addErrorContext, frontMatterHasTitle } = require("../helpers");
6
+ const { getHeadingLevel, getHeadingText } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
7
8
 
9
+ // eslint-disable-next-line jsdoc/valid-types
10
+ /** @type import("./markdownlint").Rule */
8
11
  module.exports = {
9
12
  "names": [ "MD025", "single-title", "single-h1" ],
10
13
  "description": "Multiple top-level headings in the same document",
11
14
  "tags": [ "headings" ],
15
+ "parser": "micromark",
12
16
  "function": function MD025(params, onError) {
13
17
  const level = Number(params.config.level || 1);
14
- const tag = "h" + level;
15
18
  const foundFrontMatterTitle =
16
19
  frontMatterHasTitle(
17
20
  params.frontMatterLines,
18
21
  params.config.front_matter_title
19
22
  );
20
23
  let hasTopLevelHeading = false;
21
- filterTokens(params, "heading_open", function forToken(token) {
22
- if (token.tag === tag) {
24
+ for (const heading of filterByTypesCached([ "atxHeading", "setextHeading" ])) {
25
+ const headingLevel = getHeadingLevel(heading);
26
+ if (headingLevel === level) {
23
27
  if (hasTopLevelHeading || foundFrontMatterTitle) {
24
- addErrorContext(onError, token.lineNumber,
25
- token.line.trim());
26
- } else if (token.lineNumber === 1) {
28
+ const headingText = getHeadingText(heading);
29
+ addErrorContext(
30
+ onError,
31
+ heading.startLine,
32
+ headingText
33
+ );
34
+ } else if (heading.startLine === 1) {
27
35
  hasTopLevelHeading = true;
28
36
  }
29
37
  }
30
- });
38
+ }
31
39
  }
32
40
  };
package/lib/md026.js CHANGED
@@ -4,13 +4,15 @@
4
4
 
5
5
  const { addError, allPunctuationNoQuestion, endOfLineGemojiCodeRe,
6
6
  endOfLineHtmlEntityRe, escapeForRegExp } = require("../helpers");
7
- const { filterByTypes } = require("../helpers/micromark.cjs");
8
-
7
+ const { filterByTypesCached } = require("./cache");
9
8
 
9
+ // eslint-disable-next-line jsdoc/valid-types
10
+ /** @type import("./markdownlint").Rule */
10
11
  module.exports = {
11
12
  "names": [ "MD026", "no-trailing-punctuation" ],
12
13
  "description": "Trailing punctuation in heading",
13
14
  "tags": [ "headings" ],
15
+ "parser": "micromark",
14
16
  "function": function MD026(params, onError) {
15
17
  let punctuation = params.config.punctuation;
16
18
  punctuation = String(
@@ -18,10 +20,7 @@ module.exports = {
18
20
  );
19
21
  const trailingPunctuationRe =
20
22
  new RegExp("\\s*[" + escapeForRegExp(punctuation) + "]+$");
21
- const headings = filterByTypes(
22
- params.parsers.micromark.tokens,
23
- [ "atxHeadingText", "setextHeadingText" ]
24
- );
23
+ const headings = filterByTypesCached([ "atxHeadingText", "setextHeadingText" ]);
25
24
  for (const heading of headings) {
26
25
  const { endColumn, endLine, text } = heading;
27
26
  const match = trailingPunctuationRe.exec(text);