markdownlint 0.34.0 → 0.36.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 (90) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +97 -45
  3. package/demo/markdownlint-browser.js +2186 -2164
  4. package/doc/CustomRules.md +66 -255
  5. package/doc/Rules.md +102 -29
  6. package/doc/md003.md +2 -2
  7. package/doc/md004.md +4 -3
  8. package/doc/md034.md +5 -0
  9. package/doc/md035.md +3 -8
  10. package/doc/md044.md +10 -0
  11. package/doc/md046.md +2 -2
  12. package/doc/md048.md +3 -2
  13. package/doc/md049.md +4 -3
  14. package/doc/md050.md +4 -4
  15. package/doc/md051.md +11 -3
  16. package/doc/md055.md +3 -2
  17. package/doc/md058.md +48 -0
  18. package/helpers/README.md +2 -45
  19. package/helpers/helpers.js +115 -607
  20. package/helpers/micromark-helpers.cjs +296 -0
  21. package/helpers/micromark-parse.cjs +313 -0
  22. package/helpers/package.json +5 -2
  23. package/helpers/shared.js +6 -0
  24. package/helpers/test.cjs +28 -0
  25. package/lib/cache.js +62 -13
  26. package/lib/configuration-strict.d.ts +1199 -0
  27. package/lib/configuration.d.ts +3 -1173
  28. package/lib/constants.js +3 -2
  29. package/lib/markdownit.cjs +172 -0
  30. package/lib/markdownlint.d.ts +103 -56
  31. package/lib/markdownlint.js +180 -120
  32. package/lib/md001.js +15 -9
  33. package/lib/md003.js +14 -9
  34. package/lib/md004.js +55 -48
  35. package/lib/md005.js +4 -13
  36. package/lib/md007.js +7 -11
  37. package/lib/md009.js +50 -44
  38. package/lib/md010.js +52 -33
  39. package/lib/md011.js +32 -23
  40. package/lib/md012.js +17 -9
  41. package/lib/md013.js +44 -38
  42. package/lib/md014.js +31 -38
  43. package/lib/md018.js +17 -9
  44. package/lib/md019-md021.js +80 -0
  45. package/lib/md020.js +14 -8
  46. package/lib/md022.js +13 -29
  47. package/lib/md023.js +22 -23
  48. package/lib/md024.js +12 -9
  49. package/lib/md025.js +15 -10
  50. package/lib/md026.js +2 -7
  51. package/lib/md027.js +7 -12
  52. package/lib/md028.js +3 -8
  53. package/lib/md029.js +40 -34
  54. package/lib/md030.js +4 -10
  55. package/lib/md031.js +45 -23
  56. package/lib/md032.js +33 -31
  57. package/lib/md033.js +3 -8
  58. package/lib/md034.js +21 -34
  59. package/lib/md035.js +2 -7
  60. package/lib/md036.js +19 -24
  61. package/lib/md037.js +3 -8
  62. package/lib/md038.js +11 -22
  63. package/lib/md039.js +64 -54
  64. package/lib/md040.js +13 -20
  65. package/lib/md041.js +5 -5
  66. package/lib/md042.js +44 -38
  67. package/lib/md043.js +20 -14
  68. package/lib/md044.js +34 -36
  69. package/lib/md045.js +5 -10
  70. package/lib/md046.js +2 -8
  71. package/lib/md048.js +30 -23
  72. package/lib/md049-md050.js +22 -12
  73. package/lib/md051.js +20 -28
  74. package/lib/md052.js +2 -2
  75. package/lib/md053.js +7 -6
  76. package/lib/md054.js +16 -23
  77. package/lib/md055.js +41 -49
  78. package/lib/md056.js +32 -33
  79. package/lib/md058.js +58 -0
  80. package/lib/rules.js +11 -4
  81. package/package.json +23 -20
  82. package/schema/.markdownlint.jsonc +58 -52
  83. package/schema/.markdownlint.yaml +56 -51
  84. package/schema/ValidatingConfiguration.md +6 -8
  85. package/schema/markdownlint-config-schema-strict.json +1841 -0
  86. package/schema/markdownlint-config-schema.json +141 -109
  87. package/style/prettier.json +1 -0
  88. package/helpers/micromark.cjs +0 -445
  89. package/lib/md019.js +0 -42
  90. package/lib/md021.js +0 -65
package/lib/md029.js CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorDetailIf, listItemMarkerRe, orderedListItemMarkerRe,
6
- rangeFromRegExp } = require("../helpers");
7
- const { flattenedLists } = require("./cache");
5
+ const { addErrorDetailIf } = require("../helpers");
6
+ const { getDescendantsByType } = require("../helpers/micromark-helpers.cjs");
7
+ const { filterByTypesCached } = require("./cache");
8
8
 
9
9
  const listStyleExamples = {
10
10
  "one": "1/1/1",
@@ -12,32 +12,37 @@ const listStyleExamples = {
12
12
  "zero": "0/0/0"
13
13
  };
14
14
 
15
+ /**
16
+ * Gets the value of an ordered list item prefix token.
17
+ *
18
+ * @param {import("../helpers/micromark-helpers.cjs").Token} listItemPrefix List item prefix token.
19
+ * @returns {number} List item value.
20
+ */
21
+ function getOrderedListItemValue(listItemPrefix) {
22
+ return Number(getDescendantsByType(listItemPrefix, [ "listItemValue" ])[0].text);
23
+ }
24
+
15
25
  // eslint-disable-next-line jsdoc/valid-types
16
26
  /** @type import("./markdownlint").Rule */
17
27
  module.exports = {
18
28
  "names": [ "MD029", "ol-prefix" ],
19
29
  "description": "Ordered list item prefix",
20
30
  "tags": [ "ol" ],
21
- "parser": "none",
31
+ "parser": "micromark",
22
32
  "function": function MD029(params, onError) {
23
33
  const style = String(params.config.style || "one_or_ordered");
24
- const filteredLists = flattenedLists().filter((list) => !list.unordered);
25
- for (const list of filteredLists) {
26
- const { items } = list;
27
- let current = 1;
34
+ for (const listOrdered of filterByTypesCached([ "listOrdered" ])) {
35
+ const listItemPrefixes = getDescendantsByType(listOrdered, [ "listItemPrefix" ]);
36
+ let expected = 1;
28
37
  let incrementing = false;
29
38
  // Check for incrementing number pattern 1/2/3 or 0/1/2
30
- if (items.length >= 2) {
31
- const first = orderedListItemMarkerRe.exec(items[0].line);
32
- const second = orderedListItemMarkerRe.exec(items[1].line);
33
- if (first && second) {
34
- const [ , firstNumber ] = first;
35
- const [ , secondNumber ] = second;
36
- if ((secondNumber !== "1") || (firstNumber === "0")) {
37
- incrementing = true;
38
- if (firstNumber === "0") {
39
- current = 0;
40
- }
39
+ if (listItemPrefixes.length >= 2) {
40
+ const firstValue = getOrderedListItemValue(listItemPrefixes[0]);
41
+ const secondValue = getOrderedListItemValue(listItemPrefixes[1]);
42
+ if ((secondValue !== 1) || (firstValue === 0)) {
43
+ incrementing = true;
44
+ if (firstValue === 0) {
45
+ expected = 0;
41
46
  }
42
47
  }
43
48
  }
@@ -45,24 +50,25 @@ module.exports = {
45
50
  let listStyle = style;
46
51
  if (listStyle === "one_or_ordered") {
47
52
  listStyle = incrementing ? "ordered" : "one";
48
- }
49
- // Force expected value for 0/0/0 and 1/1/1 patterns
50
- if (listStyle === "zero") {
51
- current = 0;
53
+ } else if (listStyle === "zero") {
54
+ expected = 0;
52
55
  } else if (listStyle === "one") {
53
- current = 1;
56
+ expected = 1;
54
57
  }
55
58
  // Validate each list item marker
56
- for (const item of items) {
57
- const match = orderedListItemMarkerRe.exec(item.line);
58
- if (match) {
59
- addErrorDetailIf(onError, item.lineNumber,
60
- String(current), match[1],
61
- "Style: " + listStyleExamples[listStyle], null,
62
- rangeFromRegExp(item.line, listItemMarkerRe));
63
- if (listStyle === "ordered") {
64
- current++;
65
- }
59
+ for (const listItemPrefix of listItemPrefixes) {
60
+ const actual = getOrderedListItemValue(listItemPrefix);
61
+ addErrorDetailIf(
62
+ onError,
63
+ listItemPrefix.startLine,
64
+ expected,
65
+ actual,
66
+ "Style: " + listStyleExamples[listStyle],
67
+ undefined,
68
+ [ listItemPrefix.startColumn, listItemPrefix.endColumn - listItemPrefix.startColumn ]
69
+ );
70
+ if (listStyle === "ordered") {
71
+ expected++;
66
72
  }
67
73
  }
68
74
  }
package/lib/md030.js CHANGED
@@ -3,7 +3,7 @@
3
3
  "use strict";
4
4
 
5
5
  const { addErrorDetailIf } = require("../helpers");
6
- const { filterByTypes } = require("../helpers/micromark.cjs");
6
+ const { filterByTypesCached } = require("./cache");
7
7
 
8
8
  // eslint-disable-next-line jsdoc/valid-types
9
9
  /** @type import("./markdownlint").Rule */
@@ -17,13 +17,7 @@ module.exports = {
17
17
  const olSingle = Number(params.config.ol_single || 1);
18
18
  const ulMulti = Number(params.config.ul_multi || 1);
19
19
  const olMulti = Number(params.config.ol_multi || 1);
20
- // eslint-disable-next-line jsdoc/valid-types
21
- /** @type import("../helpers/micromark.cjs").Token[] */
22
- const micromarkTokens =
23
- // @ts-ignore
24
- params.parsers.micromark.tokens;
25
- const lists = filterByTypes(micromarkTokens, [ "listOrdered", "listUnordered" ]);
26
- for (const list of lists) {
20
+ for (const list of filterByTypesCached([ "listOrdered", "listUnordered" ])) {
27
21
  const ordered = (list.type === "listOrdered");
28
22
  const listItemPrefixes =
29
23
  list.children.filter((token) => (token.type === "listItemPrefix"));
@@ -54,8 +48,8 @@ module.exports = {
54
48
  startLine,
55
49
  expectedSpaces,
56
50
  actualSpaces,
57
- null,
58
- null,
51
+ undefined,
52
+ undefined,
59
53
  range,
60
54
  fixInfo
61
55
  );
package/lib/md031.js CHANGED
@@ -2,42 +2,64 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, forEachLine, isBlankLine } = require("../helpers");
6
- const { lineMetadata } = require("./cache");
5
+ const { addErrorContext, isBlankLine } = require("../helpers");
6
+ const { getParentOfType } = require("../helpers/micromark-helpers.cjs");
7
+ const { filterByTypesCached } = require("./cache");
7
8
 
8
9
  const codeFencePrefixRe = /^(.*?)[`~]/;
9
10
 
11
+ // eslint-disable-next-line jsdoc/valid-types
12
+ /** @typedef {readonly string[]} ReadonlyStringArray */
13
+
14
+ /**
15
+ * Adds an error for the top or bottom of a code fence.
16
+ *
17
+ * @param {import("./markdownlint").RuleOnError} onError Error-reporting callback.
18
+ * @param {ReadonlyStringArray} lines Lines of Markdown content.
19
+ * @param {number} lineNumber Line number.
20
+ * @param {boolean} top True iff top fence.
21
+ * @returns {void}
22
+ */
23
+ function addError(onError, lines, lineNumber, top) {
24
+ const line = lines[lineNumber - 1];
25
+ const [ , prefix ] = line.match(codeFencePrefixRe) || [];
26
+ const fixInfo = (prefix === undefined) ?
27
+ null :
28
+ {
29
+ "lineNumber": lineNumber + (top ? 0 : 1),
30
+ "insertText": `${prefix.replace(/[^>]/g, " ").trim()}\n`
31
+ };
32
+ addErrorContext(
33
+ onError,
34
+ lineNumber,
35
+ line.trim(),
36
+ undefined,
37
+ undefined,
38
+ undefined,
39
+ fixInfo
40
+ );
41
+ }
42
+
10
43
  // eslint-disable-next-line jsdoc/valid-types
11
44
  /** @type import("./markdownlint").Rule */
12
45
  module.exports = {
13
46
  "names": [ "MD031", "blanks-around-fences" ],
14
47
  "description": "Fenced code blocks should be surrounded by blank lines",
15
48
  "tags": [ "code", "blank_lines" ],
16
- "parser": "none",
49
+ "parser": "micromark",
17
50
  "function": function MD031(params, onError) {
18
51
  const listItems = params.config.list_items;
19
52
  const includeListItems = (listItems === undefined) ? true : !!listItems;
20
53
  const { lines } = params;
21
- forEachLine(lineMetadata(), (line, i, inCode, onFence, inTable, inItem) => {
22
- const onTopFence = (onFence > 0);
23
- const onBottomFence = (onFence < 0);
24
- if ((includeListItems || !inItem) &&
25
- ((onTopFence && !isBlankLine(lines[i - 1])) ||
26
- (onBottomFence && !isBlankLine(lines[i + 1])))) {
27
- const [ , prefix ] = line.match(codeFencePrefixRe) || [];
28
- const fixInfo = (prefix === undefined) ? null : {
29
- "lineNumber": i + (onTopFence ? 1 : 2),
30
- "insertText": `${prefix.replace(/[^>]/g, " ").trim()}\n`
31
- };
32
- addErrorContext(
33
- onError,
34
- i + 1,
35
- lines[i].trim(),
36
- null,
37
- null,
38
- null,
39
- fixInfo);
54
+ for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
55
+ if (includeListItems || !(getParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) {
56
+ if (!isBlankLine(lines[codeBlock.startLine - 2])) {
57
+ addError(onError, lines, codeBlock.startLine, true);
58
+ }
59
+ if (!isBlankLine(lines[codeBlock.endLine]) && !isBlankLine(lines[codeBlock.endLine - 1])) {
60
+ addError(onError, lines, codeBlock.endLine, false);
61
+ }
40
62
  }
41
- });
63
+ }
42
64
  }
43
65
  };
package/lib/md032.js CHANGED
@@ -2,28 +2,13 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, blockquotePrefixRe, isBlankLine } = require("../helpers");
6
- const { filterByPredicate, nonContentTokens } = require("../helpers/micromark.cjs");
5
+ const { addErrorContext, isBlankLine } = require("../helpers");
6
+ const { filterByPredicate, getBlockQuotePrefixText, nonContentTokens } = require("../helpers/micromark-helpers.cjs");
7
+ const { filterByTypesCached } = require("./cache");
7
8
 
8
9
  const isList = (token) => (
9
10
  (token.type === "listOrdered") || (token.type === "listUnordered")
10
11
  );
11
- const addBlankLineError = (onError, lines, lineIndex, lineNumber) => {
12
- const line = lines[lineIndex];
13
- const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
14
- addErrorContext(
15
- onError,
16
- lineIndex + 1,
17
- line.trim(),
18
- null,
19
- null,
20
- null,
21
- {
22
- lineNumber,
23
- "insertText": `${quotePrefix}\n`
24
- }
25
- );
26
- };
27
12
 
28
13
  // eslint-disable-next-line jsdoc/valid-types
29
14
  /** @type import("./markdownlint").Rule */
@@ -33,16 +18,12 @@ module.exports = {
33
18
  "tags": [ "bullet", "ul", "ol", "blank_lines" ],
34
19
  "parser": "micromark",
35
20
  "function": function MD032(params, onError) {
36
- // eslint-disable-next-line jsdoc/valid-types
37
- /** @type import("../helpers/micromark.cjs").Token[] */
38
- const micromarkTokens =
39
- // @ts-ignore
40
- params.parsers.micromark.tokens;
41
- const { lines } = params;
21
+ const { lines, parsers } = params;
22
+ const blockQuotePrefixes = filterByTypesCached([ "blockQuotePrefix", "linePrefix" ]);
42
23
 
43
24
  // For every top-level list...
44
25
  const topLevelLists = filterByPredicate(
45
- micromarkTokens,
26
+ parsers.micromark.tokens,
46
27
  isList,
47
28
  (token) => (
48
29
  (isList(token) || (token.type === "htmlFlow")) ? [] : token.children
@@ -51,9 +32,19 @@ module.exports = {
51
32
  for (const list of topLevelLists) {
52
33
 
53
34
  // Look for a blank line above the list
54
- const firstIndex = list.startLine - 1;
55
- if (!isBlankLine(lines[firstIndex - 1])) {
56
- addBlankLineError(onError, lines, firstIndex);
35
+ const firstLineNumber = list.startLine;
36
+ if (!isBlankLine(lines[firstLineNumber - 2])) {
37
+ addErrorContext(
38
+ onError,
39
+ firstLineNumber,
40
+ lines[firstLineNumber - 1].trim(),
41
+ undefined,
42
+ undefined,
43
+ undefined,
44
+ {
45
+ "insertText": getBlockQuotePrefixText(blockQuotePrefixes, firstLineNumber)
46
+ }
47
+ );
57
48
  }
58
49
 
59
50
  // Find the "visual" end of the list
@@ -67,9 +58,20 @@ module.exports = {
67
58
  }
68
59
 
69
60
  // Look for a blank line below the list
70
- const lastIndex = endLine - 1;
71
- if (!isBlankLine(lines[lastIndex + 1])) {
72
- addBlankLineError(onError, lines, lastIndex, lastIndex + 2);
61
+ const lastLineNumber = endLine;
62
+ if (!isBlankLine(lines[lastLineNumber])) {
63
+ addErrorContext(
64
+ onError,
65
+ lastLineNumber,
66
+ lines[lastLineNumber - 1].trim(),
67
+ undefined,
68
+ undefined,
69
+ undefined,
70
+ {
71
+ "lineNumber": lastLineNumber + 1,
72
+ "insertText": getBlockQuotePrefixText(blockQuotePrefixes, lastLineNumber)
73
+ }
74
+ );
73
75
  }
74
76
  }
75
77
  }
package/lib/md033.js CHANGED
@@ -3,8 +3,8 @@
3
3
  "use strict";
4
4
 
5
5
  const { addError, nextLinesRe } = require("../helpers");
6
- const { filterByTypes, getHtmlTagInfo } =
7
- require("../helpers/micromark.cjs");
6
+ const { getHtmlTagInfo } = require("../helpers/micromark-helpers.cjs");
7
+ const { filterByTypesCached } = require("./cache");
8
8
 
9
9
  // eslint-disable-next-line jsdoc/valid-types
10
10
  /** @type import("./markdownlint").Rule */
@@ -17,12 +17,7 @@ module.exports = {
17
17
  let allowedElements = params.config.allowed_elements;
18
18
  allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
19
19
  allowedElements = allowedElements.map((element) => element.toLowerCase());
20
- // eslint-disable-next-line jsdoc/valid-types
21
- /** @type import("../helpers/micromark.cjs").Token[] */
22
- const micromarkTokens =
23
- // @ts-ignore
24
- params.parsers.micromark.tokens;
25
- for (const token of filterByTypes(micromarkTokens, [ "htmlText" ])) {
20
+ for (const token of filterByTypesCached([ "htmlText" ], true)) {
26
21
  const htmlTagInfo = getHtmlTagInfo(token);
27
22
  if (
28
23
  htmlTagInfo &&
package/lib/md034.js CHANGED
@@ -3,8 +3,7 @@
3
3
  "use strict";
4
4
 
5
5
  const { addErrorContext } = require("../helpers");
6
- const { filterByPredicate, filterByTypes, getHtmlTagInfo, inHtmlFlow, parse } =
7
- require("../helpers/micromark.cjs");
6
+ const { filterByPredicate, getHtmlTagInfo, inHtmlFlow } = require("../helpers/micromark-helpers.cjs");
8
7
 
9
8
  // eslint-disable-next-line jsdoc/valid-types
10
9
  /** @type import("./markdownlint").Rule */
@@ -14,11 +13,6 @@ module.exports = {
14
13
  "tags": [ "links", "url" ],
15
14
  "parser": "micromark",
16
15
  "function": function MD034(params, onError) {
17
- // eslint-disable-next-line jsdoc/valid-types
18
- /** @type import("../helpers/micromark.cjs").Token[] */
19
- const micromarkTokens =
20
- // @ts-ignore
21
- params.parsers.micromark.tokens;
22
16
  const literalAutolinks = (tokens) => (
23
17
  filterByPredicate(
24
18
  tokens,
@@ -26,8 +20,6 @@ module.exports = {
26
20
  if ((token.type === "literalAutolink") && !inHtmlFlow(token)) {
27
21
  // Detect and ignore https://github.com/micromark/micromark/issues/164
28
22
  const siblings = token.parent?.children;
29
- // Commented-out due to not being able to exercise in test/code coverage
30
- // || micromarkTokens;
31
23
  const index = siblings?.indexOf(token);
32
24
  // @ts-ignore
33
25
  const prev = siblings?.at(index - 1);
@@ -45,6 +37,7 @@ module.exports = {
45
37
  return false;
46
38
  },
47
39
  (token) => {
40
+ // Ignore content of inline HTML tags
48
41
  const { children } = token;
49
42
  const result = [];
50
43
  for (let i = 0; i < children.length; i++) {
@@ -75,31 +68,25 @@ module.exports = {
75
68
  }
76
69
  )
77
70
  );
78
- const autoLinks = filterByTypes(micromarkTokens, [ "literalAutolink" ]);
79
- if (autoLinks.length > 0) {
80
- // Re-parse with correct link/image reference definition handling
81
- const document = params.lines.join("\n");
82
- const tokens = parse(document, undefined, false);
83
- for (const token of literalAutolinks(tokens)) {
84
- const range = [
85
- token.startColumn,
86
- token.endColumn - token.startColumn
87
- ];
88
- const fixInfo = {
89
- "editColumn": range[0],
90
- "deleteCount": range[1],
91
- "insertText": `<${token.text}>`
92
- };
93
- addErrorContext(
94
- onError,
95
- token.startLine,
96
- token.text,
97
- null,
98
- null,
99
- range,
100
- fixInfo
101
- );
102
- }
71
+ for (const token of literalAutolinks(params.parsers.micromark.tokens)) {
72
+ const range = [
73
+ token.startColumn,
74
+ token.endColumn - token.startColumn
75
+ ];
76
+ const fixInfo = {
77
+ "editColumn": range[0],
78
+ "deleteCount": range[1],
79
+ "insertText": `<${token.text}>`
80
+ };
81
+ addErrorContext(
82
+ onError,
83
+ token.startLine,
84
+ token.text,
85
+ undefined,
86
+ undefined,
87
+ range,
88
+ fixInfo
89
+ );
103
90
  }
104
91
  }
105
92
  };
package/lib/md035.js CHANGED
@@ -3,7 +3,7 @@
3
3
  "use strict";
4
4
 
5
5
  const { addErrorDetailIf } = require("../helpers");
6
- const { filterByTypes } = require("../helpers/micromark.cjs");
6
+ const { filterByTypesCached } = require("./cache");
7
7
 
8
8
  // eslint-disable-next-line jsdoc/valid-types
9
9
  /** @type import("./markdownlint").Rule */
@@ -14,12 +14,7 @@ module.exports = {
14
14
  "parser": "micromark",
15
15
  "function": function MD035(params, onError) {
16
16
  let style = String(params.config.style || "consistent").trim();
17
- // eslint-disable-next-line jsdoc/valid-types
18
- /** @type import("../helpers/micromark.cjs").Token[] */
19
- const micromarkTokens =
20
- // @ts-ignore
21
- params.parsers.micromark.tokens;
22
- const thematicBreaks = filterByTypes(micromarkTokens, [ "thematicBreak" ]);
17
+ const thematicBreaks = filterByTypesCached([ "thematicBreak" ]);
23
18
  for (const token of thematicBreaks) {
24
19
  const { startLine, text } = token;
25
20
  if (style === "consistent") {
package/lib/md036.js CHANGED
@@ -3,14 +3,15 @@
3
3
  "use strict";
4
4
 
5
5
  const { addErrorContext, allPunctuation } = require("../helpers");
6
- const { filterByTypes, matchAndGetTokensByType } = require("../helpers/micromark.cjs");
6
+ const { getDescendantsByType } = require("../helpers/micromark-helpers.cjs");
7
+ const { filterByTypesCached } = require("./cache");
7
8
 
8
- /** @typedef {import("../helpers/micromark.cjs").TokenType} TokenType */
9
- /** @type {Map<TokenType, TokenType[]>} */
10
- const emphasisAndChildrenTypes = new Map([
11
- [ "emphasis", [ "emphasisSequence", "emphasisText", "emphasisSequence" ] ],
12
- [ "strong", [ "strongSequence", "strongText", "strongSequence" ] ]
13
- ]);
9
+ /** @typedef {import("../helpers/micromark-helpers.cjs").TokenType} TokenType */
10
+ /** @type {TokenType[][]} */
11
+ const emphasisTypes = [
12
+ [ "emphasis", "emphasisText" ],
13
+ [ "strong", "strongText" ]
14
+ ];
14
15
 
15
16
  // eslint-disable-next-line jsdoc/valid-types
16
17
  /** @type import("./markdownlint").Rule */
@@ -24,25 +25,19 @@ module.exports = {
24
25
  punctuation = String((punctuation === undefined) ? allPunctuation : punctuation);
25
26
  const punctuationRe = new RegExp("[" + punctuation + "]$");
26
27
  const paragraphTokens =
27
- filterByTypes(params.parsers.micromark.tokens, [ "paragraph" ]).
28
- filter((token) =>
28
+ filterByTypesCached([ "paragraph" ])
29
+ .filter((token) =>
29
30
  (token.parent?.type === "content") && !token.parent?.parent && (token.children.length === 1)
30
31
  );
31
- for (const paragraphToken of paragraphTokens) {
32
- const childToken = paragraphToken.children[0];
33
- for (const [ emphasisType, emphasisChildrenTypes ] of emphasisAndChildrenTypes) {
34
- if (childToken.type === emphasisType) {
35
- const matchingTokens = matchAndGetTokensByType(childToken.children, emphasisChildrenTypes);
36
- if (matchingTokens) {
37
- const textToken = matchingTokens[1];
38
- if (
39
- (textToken.children.length === 1) &&
40
- (textToken.children[0].type === "data") &&
41
- !punctuationRe.test(textToken.text)
42
- ) {
43
- addErrorContext(onError, textToken.startLine, textToken.text);
44
- }
45
- }
32
+ for (const emphasisType of emphasisTypes) {
33
+ const textTokens = getDescendantsByType(paragraphTokens, emphasisType);
34
+ for (const textToken of textTokens) {
35
+ if (
36
+ (textToken.children.length === 1) &&
37
+ (textToken.children[0].type === "data") &&
38
+ !punctuationRe.test(textToken.text)
39
+ ) {
40
+ addErrorContext(onError, textToken.startLine, textToken.text);
46
41
  }
47
42
  }
48
43
  }
package/lib/md037.js CHANGED
@@ -3,7 +3,7 @@
3
3
  "use strict";
4
4
 
5
5
  const { addError } = require("../helpers");
6
- const { filterByPredicate, inHtmlFlow } = require("../helpers/micromark.cjs");
6
+ const { filterByPredicate, inHtmlFlow } = require("../helpers/micromark-helpers.cjs");
7
7
 
8
8
  // eslint-disable-next-line jsdoc/valid-types
9
9
  /** @type import("./markdownlint").Rule */
@@ -15,18 +15,13 @@ module.exports = {
15
15
  "function": function MD037(params, onError) {
16
16
 
17
17
  // Initialize variables
18
- // eslint-disable-next-line jsdoc/valid-types
19
- /** @type import("../helpers/micromark.cjs").Token[] */
20
- const micromarkTokens =
21
- // @ts-ignore
22
- params.parsers.micromark.tokens;
23
- const { lines } = params;
18
+ const { lines, parsers } = params;
24
19
  const emphasisTokensByMarker = new Map();
25
20
  for (const marker of [ "_", "__", "___", "*", "**", "***" ]) {
26
21
  emphasisTokensByMarker.set(marker, []);
27
22
  }
28
23
  const tokens = filterByPredicate(
29
- micromarkTokens,
24
+ parsers.micromark.tokens,
30
25
  (token) => token.children.some((child) => child.type === "data")
31
26
  );
32
27
  for (const token of tokens) {
package/lib/md038.js CHANGED
@@ -3,8 +3,8 @@
3
3
  "use strict";
4
4
 
5
5
  const { addErrorContext } = require("../helpers");
6
- const { filterByTypes, inHtmlFlow, tokenIfType } =
7
- require("../helpers/micromark.cjs");
6
+ const { getDescendantsByType } = require("../helpers/micromark-helpers.cjs");
7
+ const { filterByTypesCached } = require("./cache");
8
8
 
9
9
  const leftSpaceRe = /^\s(?:[^`]|$)/;
10
10
  const rightSpaceRe = /[^`]\s$/;
@@ -27,32 +27,21 @@ module.exports = {
27
27
  "tags": [ "whitespace", "code" ],
28
28
  "parser": "micromark",
29
29
  "function": function MD038(params, onError) {
30
- // eslint-disable-next-line jsdoc/valid-types
31
- /** @type import("../helpers/micromark.cjs").Token[] */
32
- const micromarkTokens =
33
- // @ts-ignore
34
- params.parsers.micromark.tokens;
35
- const codeTexts = filterByTypes(micromarkTokens, [ "codeText" ])
36
- .filter((codeText) => !inHtmlFlow(codeText));
30
+ const codeTexts = filterByTypesCached([ "codeText" ]);
37
31
  for (const codeText of codeTexts) {
38
- const { children } = codeText;
39
- const first = 0;
40
- const last = children.length - 1;
41
- const startSequence = tokenIfType(children[first], "codeTextSequence");
42
- const endSequence = tokenIfType(children[last], "codeTextSequence");
43
- const startData =
44
- tokenIfType(children[first + 1], "codeTextData") ||
45
- tokenIfType(children[first + 2], "codeTextData");
46
- const endData =
47
- tokenIfType(children[last - 1], "codeTextData") ||
48
- tokenIfType(children[last - 2], "codeTextData");
32
+ const sequences = getDescendantsByType(codeText, [ "codeTextSequence" ]);
33
+ const startSequence = sequences[0];
34
+ const endSequence = sequences[sequences.length - 1];
35
+ const datas = getDescendantsByType(codeText, [ "codeTextData" ]);
36
+ const startData = datas[0];
37
+ const endData = datas[datas.length - 1];
49
38
  if (startSequence && endSequence && startData && endData) {
50
39
  const spaceLeft = leftSpaceRe.test(startData.text);
51
40
  const spaceRight = rightSpaceRe.test(endData.text);
52
41
  if (spaceLeft || spaceRight) {
53
42
  let lineNumber = startSequence.startLine;
54
- let range = null;
55
- let fixInfo = null;
43
+ let range = undefined;
44
+ let fixInfo = undefined;
56
45
  if (startSequence.startLine === endSequence.endLine) {
57
46
  range = [
58
47
  startSequence.startColumn,