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/md027.js CHANGED
@@ -2,54 +2,35 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, newLineRe } = require("../helpers");
6
-
7
- const spaceAfterBlockQuoteRe = /^((?:\s*>)+)(\s{2,})\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
- "names": [ "MD027", "no-multiple-space-blockquote" ],
11
+ "names": ["MD027", "no-multiple-space-blockquote"],
11
12
  "description": "Multiple spaces after blockquote symbol",
12
- "tags": [ "blockquote", "whitespace", "indentation" ],
13
+ "tags": ["blockquote", "whitespace", "indentation"],
14
+ "parser": "micromark",
13
15
  "function": function MD027(params, onError) {
14
- let blockquoteNesting = 0;
15
- let listItemNesting = 0;
16
- for (const token of params.parsers.markdownit.tokens) {
17
- const { content, lineNumber, type } = token;
18
- if (type === "blockquote_open") {
19
- blockquoteNesting++;
20
- } else if (type === "blockquote_close") {
21
- blockquoteNesting--;
22
- } else if (type === "list_item_open") {
23
- listItemNesting++;
24
- } else if (type === "list_item_close") {
25
- listItemNesting--;
26
- } else if ((type === "inline") && blockquoteNesting) {
27
- const lineCount = content.split(newLineRe).length;
28
- for (let i = 0; i < lineCount; i++) {
29
- const line = params.lines[lineNumber + i - 1];
30
- const match = line.match(spaceAfterBlockQuoteRe);
31
- if (match) {
32
- const [
33
- fullMatch,
34
- { "length": blockquoteLength },
35
- { "length": spaceLength }
36
- ] = match;
37
- if (!listItemNesting || (fullMatch[fullMatch.length - 1] === ">")) {
38
- addErrorContext(
39
- onError,
40
- lineNumber + i,
41
- line,
42
- null,
43
- null,
44
- [ 1, fullMatch.length ],
45
- {
46
- "editColumn": blockquoteLength + 1,
47
- "deleteCount": spaceLength - 1
48
- }
49
- );
50
- }
16
+ for (const token of filterByTypesCached([ "linePrefix" ])) {
17
+ const siblings = token.parent?.children || params.parsers.micromark.tokens;
18
+ if (siblings[siblings.indexOf(token) - 1]?.type === "blockQuotePrefix") {
19
+ const { startColumn, startLine, text } = token;
20
+ const { length } = text;
21
+ const line = params.lines[startLine - 1];
22
+ addErrorContext(
23
+ onError,
24
+ startLine,
25
+ line,
26
+ undefined,
27
+ undefined,
28
+ [ startColumn, length ],
29
+ {
30
+ "editColumn": startColumn,
31
+ "deleteCount": length
51
32
  }
52
- }
33
+ );
53
34
  }
54
35
  }
55
36
  }
package/lib/md028.js CHANGED
@@ -3,28 +3,40 @@
3
3
  "use strict";
4
4
 
5
5
  const { addError } = require("../helpers");
6
+ const { filterByTypesCached } = require("./cache");
6
7
 
8
+ const ignoreTypes = new Set([ "lineEnding", "listItemIndent", "linePrefix" ]);
9
+
10
+ // eslint-disable-next-line jsdoc/valid-types
11
+ /** @type import("./markdownlint").Rule */
7
12
  module.exports = {
8
13
  "names": [ "MD028", "no-blanks-blockquote" ],
9
14
  "description": "Blank line inside blockquote",
10
15
  "tags": [ "blockquote", "whitespace" ],
16
+ "parser": "micromark",
11
17
  "function": function MD028(params, onError) {
12
- let prevToken = {};
13
- let prevLineNumber = null;
14
- for (const token of params.parsers.markdownit.tokens) {
15
- if ((token.type === "blockquote_open") &&
16
- (prevToken.type === "blockquote_close")) {
17
- for (
18
- let lineNumber = prevLineNumber;
19
- lineNumber < token.lineNumber;
20
- lineNumber++) {
21
- addError(onError, lineNumber);
18
+ for (const token of filterByTypesCached([ "blockQuote" ])) {
19
+ const errorLineNumbers = [];
20
+ const siblings = token.parent?.children || params.parsers.micromark.tokens;
21
+ for (let i = siblings.indexOf(token) + 1; i < siblings.length; i++) {
22
+ const sibling = siblings[i];
23
+ const { startLine, type } = sibling;
24
+ if (type === "lineEndingBlank") {
25
+ // Possible blank between blockquotes
26
+ errorLineNumbers.push(startLine);
27
+ } else if (ignoreTypes.has(type)) {
28
+ // Ignore invisible formatting
29
+ } else if (type === "blockQuote") {
30
+ // Blockquote followed by blockquote
31
+ for (const lineNumber of errorLineNumbers) {
32
+ addError(onError, lineNumber);
33
+ }
34
+ break;
35
+ } else {
36
+ // Blockquote not followed by blockquote
37
+ break;
22
38
  }
23
39
  }
24
- prevToken = token;
25
- if (token.type === "blockquote_open") {
26
- prevLineNumber = token.map[1] + 1;
27
- }
28
40
  }
29
41
  }
30
42
  };
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, getTokenTextByType } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
8
8
 
9
9
  const listStyleExamples = {
10
10
  "one": "1/1/1",
@@ -12,29 +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.cjs").Token} listItemPrefix List item prefix token.
19
+ * @returns {number} List item value.
20
+ */
21
+ function getOrderedListItemValue(listItemPrefix) {
22
+ return Number(getTokenTextByType(listItemPrefix.children, "listItemValue"));
23
+ }
24
+
25
+ // eslint-disable-next-line jsdoc/valid-types
26
+ /** @type import("./markdownlint").Rule */
15
27
  module.exports = {
16
28
  "names": [ "MD029", "ol-prefix" ],
17
29
  "description": "Ordered list item prefix",
18
30
  "tags": [ "ol" ],
31
+ "parser": "micromark",
19
32
  "function": function MD029(params, onError) {
20
33
  const style = String(params.config.style || "one_or_ordered");
21
- const filteredLists = flattenedLists().filter((list) => !list.unordered);
22
- for (const list of filteredLists) {
23
- const { items } = list;
24
- let current = 1;
34
+ for (const listOrdered of filterByTypesCached([ "listOrdered" ])) {
35
+ const listItemPrefixes = getDescendantsByType(listOrdered, [ "listItemPrefix" ]);
36
+ let expected = 1;
25
37
  let incrementing = false;
26
38
  // Check for incrementing number pattern 1/2/3 or 0/1/2
27
- if (items.length >= 2) {
28
- const first = orderedListItemMarkerRe.exec(items[0].line);
29
- const second = orderedListItemMarkerRe.exec(items[1].line);
30
- if (first && second) {
31
- const [ , firstNumber ] = first;
32
- const [ , secondNumber ] = second;
33
- if ((secondNumber !== "1") || (firstNumber === "0")) {
34
- incrementing = true;
35
- if (firstNumber === "0") {
36
- current = 0;
37
- }
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;
38
46
  }
39
47
  }
40
48
  }
@@ -42,24 +50,25 @@ module.exports = {
42
50
  let listStyle = style;
43
51
  if (listStyle === "one_or_ordered") {
44
52
  listStyle = incrementing ? "ordered" : "one";
45
- }
46
- // Force expected value for 0/0/0 and 1/1/1 patterns
47
- if (listStyle === "zero") {
48
- current = 0;
53
+ } else if (listStyle === "zero") {
54
+ expected = 0;
49
55
  } else if (listStyle === "one") {
50
- current = 1;
56
+ expected = 1;
51
57
  }
52
58
  // Validate each list item marker
53
- for (const item of items) {
54
- const match = orderedListItemMarkerRe.exec(item.line);
55
- if (match) {
56
- addErrorDetailIf(onError, item.lineNumber,
57
- String(current), match[1],
58
- "Style: " + listStyleExamples[listStyle], null,
59
- rangeFromRegExp(item.line, listItemMarkerRe));
60
- if (listStyle === "ordered") {
61
- current++;
62
- }
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++;
63
72
  }
64
73
  }
65
74
  }
package/lib/md030.js CHANGED
@@ -3,22 +3,21 @@
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
+ // eslint-disable-next-line jsdoc/valid-types
9
+ /** @type import("./markdownlint").Rule */
8
10
  module.exports = {
9
11
  "names": [ "MD030", "list-marker-space" ],
10
12
  "description": "Spaces after list markers",
11
13
  "tags": [ "ol", "ul", "whitespace" ],
14
+ "parser": "micromark",
12
15
  "function": function MD030(params, onError) {
13
16
  const ulSingle = Number(params.config.ul_single || 1);
14
17
  const olSingle = Number(params.config.ol_single || 1);
15
18
  const ulMulti = Number(params.config.ul_multi || 1);
16
19
  const olMulti = Number(params.config.ol_multi || 1);
17
- const lists = filterByTypes(
18
- params.parsers.micromark.tokens,
19
- [ "listOrdered", "listUnordered" ]
20
- );
21
- for (const list of lists) {
20
+ for (const list of filterByTypesCached([ "listOrdered", "listUnordered" ])) {
22
21
  const ordered = (list.type === "listOrdered");
23
22
  const listItemPrefixes =
24
23
  list.children.filter((token) => (token.type === "listItemPrefix"));
@@ -49,8 +48,8 @@ module.exports = {
49
48
  startLine,
50
49
  expectedSpaces,
51
50
  actualSpaces,
52
- null,
53
- null,
51
+ undefined,
52
+ undefined,
54
53
  range,
55
54
  fixInfo
56
55
  );
package/lib/md031.js CHANGED
@@ -2,39 +2,62 @@
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 { getTokenParentOfType } = require("../helpers/micromark.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) ? null : {
27
+ "lineNumber": lineNumber + (top ? 0 : 1),
28
+ "insertText": `${prefix.replace(/[^>]/g, " ").trim()}\n`
29
+ };
30
+ addErrorContext(
31
+ onError,
32
+ lineNumber,
33
+ line.trim(),
34
+ undefined,
35
+ undefined,
36
+ undefined,
37
+ fixInfo
38
+ );
39
+ }
40
+
41
+ // eslint-disable-next-line jsdoc/valid-types
42
+ /** @type import("./markdownlint").Rule */
10
43
  module.exports = {
11
44
  "names": [ "MD031", "blanks-around-fences" ],
12
45
  "description": "Fenced code blocks should be surrounded by blank lines",
13
46
  "tags": [ "code", "blank_lines" ],
47
+ "parser": "micromark",
14
48
  "function": function MD031(params, onError) {
15
49
  const listItems = params.config.list_items;
16
50
  const includeListItems = (listItems === undefined) ? true : !!listItems;
17
51
  const { lines } = params;
18
- forEachLine(lineMetadata(), (line, i, inCode, onFence, inTable, inItem) => {
19
- const onTopFence = (onFence > 0);
20
- const onBottomFence = (onFence < 0);
21
- if ((includeListItems || !inItem) &&
22
- ((onTopFence && !isBlankLine(lines[i - 1])) ||
23
- (onBottomFence && !isBlankLine(lines[i + 1])))) {
24
- const [ , prefix ] = line.match(codeFencePrefixRe) || [];
25
- const fixInfo = (prefix === undefined) ? null : {
26
- "lineNumber": i + (onTopFence ? 1 : 2),
27
- "insertText": `${prefix.replace(/[^>]/g, " ").trim()}\n`
28
- };
29
- addErrorContext(
30
- onError,
31
- i + 1,
32
- lines[i].trim(),
33
- null,
34
- null,
35
- null,
36
- fixInfo);
52
+ for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
53
+ if (includeListItems || !(getTokenParentOfType(codeBlock, [ "listOrdered", "listUnordered" ]))) {
54
+ if (!isBlankLine(lines[codeBlock.startLine - 2])) {
55
+ addError(onError, lines, codeBlock.startLine, true);
56
+ }
57
+ if (!isBlankLine(lines[codeBlock.endLine]) && !isBlankLine(lines[codeBlock.endLine - 1])) {
58
+ addError(onError, lines, codeBlock.endLine, false);
59
+ }
37
60
  }
38
- });
61
+ }
39
62
  }
40
63
  };
package/lib/md032.js CHANGED
@@ -2,43 +2,20 @@
2
2
 
3
3
  "use strict";
4
4
 
5
- const { addErrorContext, blockquotePrefixRe, isBlankLine } =
6
- require("../helpers");
7
- const { filterByPredicate } = require("../helpers/micromark.cjs");
5
+ const { addErrorContextForLine, isBlankLine } = require("../helpers");
6
+ const { filterByPredicate, nonContentTokens } = require("../helpers/micromark.cjs");
8
7
 
9
- const nonContentTokens = new Set([
10
- "blockQuoteMarker",
11
- "blockQuotePrefix",
12
- "blockQuotePrefixWhitespace",
13
- "lineEnding",
14
- "lineEndingBlank",
15
- "linePrefix",
16
- "listItemIndent"
17
- ]);
18
8
  const isList = (token) => (
19
9
  (token.type === "listOrdered") || (token.type === "listUnordered")
20
10
  );
21
- const addBlankLineError = (onError, lines, lineIndex, lineNumber) => {
22
- const line = lines[lineIndex];
23
- const quotePrefix = line.match(blockquotePrefixRe)[0].trimEnd();
24
- addErrorContext(
25
- onError,
26
- lineIndex + 1,
27
- line.trim(),
28
- null,
29
- null,
30
- null,
31
- {
32
- lineNumber,
33
- "insertText": `${quotePrefix}\n`
34
- }
35
- );
36
- };
37
11
 
12
+ // eslint-disable-next-line jsdoc/valid-types
13
+ /** @type import("./markdownlint").Rule */
38
14
  module.exports = {
39
15
  "names": [ "MD032", "blanks-around-lists" ],
40
16
  "description": "Lists should be surrounded by blank lines",
41
17
  "tags": [ "bullet", "ul", "ol", "blank_lines" ],
18
+ "parser": "micromark",
42
19
  "function": function MD032(params, onError) {
43
20
  const { lines, parsers } = params;
44
21
 
@@ -55,7 +32,12 @@ module.exports = {
55
32
  // Look for a blank line above the list
56
33
  const firstIndex = list.startLine - 1;
57
34
  if (!isBlankLine(lines[firstIndex - 1])) {
58
- addBlankLineError(onError, lines, firstIndex);
35
+ addErrorContextForLine(
36
+ onError,
37
+ // @ts-ignore
38
+ lines,
39
+ firstIndex
40
+ );
59
41
  }
60
42
 
61
43
  // Find the "visual" end of the list
@@ -71,7 +53,13 @@ module.exports = {
71
53
  // Look for a blank line below the list
72
54
  const lastIndex = endLine - 1;
73
55
  if (!isBlankLine(lines[lastIndex + 1])) {
74
- addBlankLineError(onError, lines, lastIndex, lastIndex + 2);
56
+ addErrorContextForLine(
57
+ onError,
58
+ // @ts-ignore
59
+ lines,
60
+ lastIndex,
61
+ lastIndex + 2
62
+ );
75
63
  }
76
64
  }
77
65
  }
package/lib/md033.js CHANGED
@@ -3,19 +3,21 @@
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.cjs");
7
+ const { filterByTypesCached } = require("./cache");
8
8
 
9
+ // eslint-disable-next-line jsdoc/valid-types
10
+ /** @type import("./markdownlint").Rule */
9
11
  module.exports = {
10
12
  "names": [ "MD033", "no-inline-html" ],
11
13
  "description": "Inline HTML",
12
14
  "tags": [ "html" ],
15
+ "parser": "micromark",
13
16
  "function": function MD033(params, onError) {
14
17
  let allowedElements = params.config.allowed_elements;
15
18
  allowedElements = Array.isArray(allowedElements) ? allowedElements : [];
16
19
  allowedElements = allowedElements.map((element) => element.toLowerCase());
17
- const { tokens } = params.parsers.micromark;
18
- for (const token of filterByTypes(tokens, [ "htmlText" ])) {
20
+ for (const token of filterByTypesCached([ "htmlText" ], true)) {
19
21
  const htmlTagInfo = getHtmlTagInfo(token);
20
22
  if (
21
23
  htmlTagInfo &&
package/lib/md034.js CHANGED
@@ -3,18 +3,42 @@
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, parse } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
8
8
 
9
+ // eslint-disable-next-line jsdoc/valid-types
10
+ /** @type import("./markdownlint").Rule */
9
11
  module.exports = {
10
12
  "names": [ "MD034", "no-bare-urls" ],
11
13
  "description": "Bare URL used",
12
14
  "tags": [ "links", "url" ],
15
+ "parser": "micromark",
13
16
  "function": function MD034(params, onError) {
14
17
  const literalAutolinks = (tokens) => (
15
18
  filterByPredicate(
16
19
  tokens,
17
- (token) => (token.type === "literalAutolink") && !inHtmlFlow(token),
20
+ (token) => {
21
+ if ((token.type === "literalAutolink") && !inHtmlFlow(token)) {
22
+ // Detect and ignore https://github.com/micromark/micromark/issues/164
23
+ const siblings = token.parent?.children;
24
+ // Commented-out due to not being able to exercise in test/code coverage
25
+ // || micromarkTokens;
26
+ const index = siblings?.indexOf(token);
27
+ // @ts-ignore
28
+ const prev = siblings?.at(index - 1);
29
+ // @ts-ignore
30
+ const next = siblings?.at(index + 1);
31
+ return !(
32
+ prev &&
33
+ next &&
34
+ (prev.type === "data") &&
35
+ (next.type === "data") &&
36
+ prev.text.endsWith("<") &&
37
+ next.text.startsWith(">")
38
+ );
39
+ }
40
+ return false;
41
+ },
18
42
  (token) => {
19
43
  const { children } = token;
20
44
  const result = [];
@@ -46,10 +70,7 @@ module.exports = {
46
70
  }
47
71
  )
48
72
  );
49
- const autoLinks = filterByTypes(
50
- params.parsers.micromark.tokens,
51
- [ "literalAutolink" ]
52
- );
73
+ const autoLinks = filterByTypesCached([ "literalAutolink" ]);
53
74
  if (autoLinks.length > 0) {
54
75
  // Re-parse with correct link/image reference definition handling
55
76
  const document = params.lines.join("\n");
@@ -68,8 +89,8 @@ module.exports = {
68
89
  onError,
69
90
  token.startLine,
70
91
  token.text,
71
- null,
72
- null,
92
+ undefined,
93
+ undefined,
73
94
  range,
74
95
  fixInfo
75
96
  );
package/lib/md035.js CHANGED
@@ -3,16 +3,18 @@
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
+ // eslint-disable-next-line jsdoc/valid-types
9
+ /** @type import("./markdownlint").Rule */
8
10
  module.exports = {
9
11
  "names": [ "MD035", "hr-style" ],
10
12
  "description": "Horizontal rule style",
11
13
  "tags": [ "hr" ],
14
+ "parser": "micromark",
12
15
  "function": function MD035(params, onError) {
13
16
  let style = String(params.config.style || "consistent").trim();
14
- const thematicBreaks =
15
- filterByTypes(params.parsers.micromark.tokens, [ "thematicBreak" ]);
17
+ const thematicBreaks = filterByTypesCached([ "thematicBreak" ]);
16
18
  for (const token of thematicBreaks) {
17
19
  const { startLine, text } = token;
18
20
  if (style === "consistent") {
package/lib/md036.js CHANGED
@@ -3,54 +3,49 @@
3
3
  "use strict";
4
4
 
5
5
  const { addErrorContext, allPunctuation } = require("../helpers");
6
+ const { matchAndGetTokensByType } = require("../helpers/micromark.cjs");
7
+ const { filterByTypesCached } = require("./cache");
6
8
 
9
+ /** @typedef {import("../helpers/micromark.cjs").TokenType} TokenType */
10
+ /** @type {Map<TokenType, TokenType[]>} */
11
+ const emphasisAndChildrenTypes = new Map([
12
+ [ "emphasis", [ "emphasisSequence", "emphasisText", "emphasisSequence" ] ],
13
+ [ "strong", [ "strongSequence", "strongText", "strongSequence" ] ]
14
+ ]);
15
+
16
+ // eslint-disable-next-line jsdoc/valid-types
17
+ /** @type import("./markdownlint").Rule */
7
18
  module.exports = {
8
19
  "names": [ "MD036", "no-emphasis-as-heading" ],
9
20
  "description": "Emphasis used instead of a heading",
10
21
  "tags": [ "headings", "emphasis" ],
22
+ "parser": "micromark",
11
23
  "function": function MD036(params, onError) {
12
24
  let punctuation = params.config.punctuation;
13
- punctuation =
14
- String((punctuation === undefined) ? allPunctuation : punctuation);
15
- const re = new RegExp("[" + punctuation + "]$");
16
- // eslint-disable-next-line jsdoc/require-jsdoc
17
- function base(token) {
18
- if (token.type === "paragraph_open") {
19
- return function inParagraph(t) {
20
- // Always paragraph_open/inline/paragraph_close,
21
- const children = t.children.filter(function notEmptyText(child) {
22
- return (child.type !== "text") || (child.content !== "");
23
- });
24
- if ((children.length === 3) &&
25
- ((children[0].type === "strong_open") ||
26
- (children[0].type === "em_open")) &&
27
- (children[1].type === "text") &&
28
- !re.test(children[1].content)) {
29
- addErrorContext(onError, t.lineNumber,
30
- children[1].content);
31
- }
32
- return base;
33
- };
34
- } else if (token.type === "blockquote_open") {
35
- return function inBlockquote(t) {
36
- if (t.type !== "blockquote_close") {
37
- return inBlockquote;
25
+ punctuation = String((punctuation === undefined) ? allPunctuation : punctuation);
26
+ const punctuationRe = new RegExp("[" + punctuation + "]$");
27
+ const paragraphTokens =
28
+ filterByTypesCached([ "paragraph" ]).
29
+ filter((token) =>
30
+ (token.parent?.type === "content") && !token.parent?.parent && (token.children.length === 1)
31
+ );
32
+ for (const paragraphToken of paragraphTokens) {
33
+ const childToken = paragraphToken.children[0];
34
+ for (const [ emphasisType, emphasisChildrenTypes ] of emphasisAndChildrenTypes) {
35
+ if (childToken.type === emphasisType) {
36
+ const matchingTokens = matchAndGetTokensByType(childToken.children, emphasisChildrenTypes);
37
+ if (matchingTokens) {
38
+ const textToken = matchingTokens[1];
39
+ if (
40
+ (textToken.children.length === 1) &&
41
+ (textToken.children[0].type === "data") &&
42
+ !punctuationRe.test(textToken.text)
43
+ ) {
44
+ addErrorContext(onError, textToken.startLine, textToken.text);
45
+ }
38
46
  }
39
- return base;
40
- };
41
- } else if (token.type === "list_item_open") {
42
- return function inListItem(t) {
43
- if (t.type !== "list_item_close") {
44
- return inListItem;
45
- }
46
- return base;
47
- };
47
+ }
48
48
  }
49
- return base;
50
- }
51
- let state = base;
52
- for (const token of params.parsers.markdownit.tokens) {
53
- state = state(token);
54
49
  }
55
50
  }
56
51
  };