|
| 1 | +/** |
| 2 | + * @fileoverview Rule to ensure there is a space after hash on ATX style headings in Markdown. |
| 3 | + * @author Sweta Tanwar (@SwetaTanwar) |
| 4 | + */ |
| 5 | + |
| 6 | +//----------------------------------------------------------------------------- |
| 7 | +// Type Definitions |
| 8 | +//----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +/** |
| 11 | + * @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: []; }>} |
| 12 | + * NoMissingAtxHeadingSpaceRuleDefinition |
| 13 | + */ |
| 14 | + |
| 15 | +//----------------------------------------------------------------------------- |
| 16 | +// Helpers |
| 17 | +//----------------------------------------------------------------------------- |
| 18 | + |
| 19 | +const headingPattern = /^(#{1,6})(?:[^#\s]|$)/u; |
| 20 | +const newLinePattern = /\r?\n/u; |
| 21 | + |
| 22 | +//----------------------------------------------------------------------------- |
| 23 | +// Rule Definition |
| 24 | +//----------------------------------------------------------------------------- |
| 25 | + |
| 26 | +/** @type {NoMissingAtxHeadingSpaceRuleDefinition} */ |
| 27 | +export default { |
| 28 | + meta: { |
| 29 | + type: "problem", |
| 30 | + |
| 31 | + docs: { |
| 32 | + recommended: true, |
| 33 | + description: |
| 34 | + "Disallow headings without a space after the hash characters", |
| 35 | + url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-missing-atx-heading-space.md", |
| 36 | + }, |
| 37 | + |
| 38 | + fixable: "whitespace", |
| 39 | + |
| 40 | + messages: { |
| 41 | + missingSpace: "Missing space after hash(es) on ATX style heading.", |
| 42 | + }, |
| 43 | + }, |
| 44 | + |
| 45 | + create(context) { |
| 46 | + return { |
| 47 | + paragraph(node) { |
| 48 | + if (node.children && node.children.length > 0) { |
| 49 | + const firstTextChild = node.children.find( |
| 50 | + child => child.type === "text", |
| 51 | + ); |
| 52 | + if (!firstTextChild) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + const text = context.sourceCode.getText(firstTextChild); |
| 57 | + const lines = text.split(newLinePattern); |
| 58 | + |
| 59 | + lines.forEach((line, idx) => { |
| 60 | + const lineNum = |
| 61 | + firstTextChild.position.start.line + idx; |
| 62 | + |
| 63 | + const match = headingPattern.exec(line); |
| 64 | + if (!match) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const hashes = match[1]; |
| 69 | + |
| 70 | + const startColumn = |
| 71 | + firstTextChild.position.start.column; |
| 72 | + |
| 73 | + context.report({ |
| 74 | + loc: { |
| 75 | + start: { line: lineNum, column: startColumn }, |
| 76 | + end: { |
| 77 | + line: lineNum, |
| 78 | + column: startColumn + hashes.length + 1, |
| 79 | + }, |
| 80 | + }, |
| 81 | + messageId: "missingSpace", |
| 82 | + fix(fixer) { |
| 83 | + const offset = |
| 84 | + firstTextChild.position.start.offset + |
| 85 | + lines.slice(0, idx).join("\n").length + |
| 86 | + (idx > 0 ? 1 : 0); |
| 87 | + |
| 88 | + return fixer.insertTextAfterRange( |
| 89 | + [ |
| 90 | + offset + hashes.length - 1, |
| 91 | + offset + hashes.length, |
| 92 | + ], |
| 93 | + " ", |
| 94 | + ); |
| 95 | + }, |
| 96 | + }); |
| 97 | + }); |
| 98 | + } |
| 99 | + }, |
| 100 | + }; |
| 101 | + }, |
| 102 | +}; |
0 commit comments