|
| 1 | +/** |
| 2 | + * @fileoverview Rule to enforce at most one H1 heading in Markdown. |
| 3 | + * @author Pixel998 |
| 4 | + */ |
| 5 | + |
| 6 | +//----------------------------------------------------------------------------- |
| 7 | +// Imports |
| 8 | +//----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +import { findOffsets } from "../util.js"; |
| 11 | + |
| 12 | +//----------------------------------------------------------------------------- |
| 13 | +// Type Definitions |
| 14 | +//----------------------------------------------------------------------------- |
| 15 | + |
| 16 | +/** |
| 17 | + * @typedef {import("../types.ts").MarkdownRuleDefinition<{ RuleOptions: [{ frontmatterTitle?: string; }]; }>} |
| 18 | + * NoMultipleH1RuleDefinition |
| 19 | + */ |
| 20 | + |
| 21 | +//----------------------------------------------------------------------------- |
| 22 | +// Helpers |
| 23 | +//----------------------------------------------------------------------------- |
| 24 | + |
| 25 | +const h1TagPattern = /(?<!<!--[\s\S]*?)<h1[^>]*>[\s\S]*?<\/h1>/giu; |
| 26 | + |
| 27 | +/** |
| 28 | + * Checks if a frontmatter block contains a title matching the given pattern |
| 29 | + * @param {string} value The frontmatter content |
| 30 | + * @param {RegExp|null} pattern The pattern to match against |
| 31 | + * @returns {boolean} Whether a title was found |
| 32 | + */ |
| 33 | +function frontmatterHasTitle(value, pattern) { |
| 34 | + if (!pattern) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + const lines = value.split("\n"); |
| 38 | + for (const line of lines) { |
| 39 | + if (pattern.test(line)) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + } |
| 43 | + return false; |
| 44 | +} |
| 45 | + |
| 46 | +//----------------------------------------------------------------------------- |
| 47 | +// Rule Definition |
| 48 | +//----------------------------------------------------------------------------- |
| 49 | + |
| 50 | +/** @type {NoMultipleH1RuleDefinition} */ |
| 51 | +export default { |
| 52 | + meta: { |
| 53 | + type: "problem", |
| 54 | + |
| 55 | + docs: { |
| 56 | + recommended: true, |
| 57 | + description: "Disallow multiple H1 headings in the same document", |
| 58 | + url: "https://github.com/eslint/markdown/blob/main/docs/rules/no-multiple-h1.md", |
| 59 | + }, |
| 60 | + |
| 61 | + messages: { |
| 62 | + multipleH1: "Unexpected additional H1 heading found.", |
| 63 | + }, |
| 64 | + |
| 65 | + schema: [ |
| 66 | + { |
| 67 | + type: "object", |
| 68 | + properties: { |
| 69 | + frontmatterTitle: { |
| 70 | + type: "string", |
| 71 | + }, |
| 72 | + }, |
| 73 | + additionalProperties: false, |
| 74 | + }, |
| 75 | + ], |
| 76 | + |
| 77 | + defaultOptions: [ |
| 78 | + { frontmatterTitle: "^\\s*['\"]?title['\"]?\\s*[:=]" }, |
| 79 | + ], |
| 80 | + }, |
| 81 | + |
| 82 | + create(context) { |
| 83 | + const [{ frontmatterTitle }] = context.options; |
| 84 | + const titlePattern = |
| 85 | + frontmatterTitle === "" ? null : new RegExp(frontmatterTitle, "iu"); |
| 86 | + let h1Count = 0; |
| 87 | + |
| 88 | + return { |
| 89 | + yaml(node) { |
| 90 | + if (frontmatterHasTitle(node.value, titlePattern)) { |
| 91 | + h1Count++; |
| 92 | + } |
| 93 | + }, |
| 94 | + |
| 95 | + toml(node) { |
| 96 | + if (frontmatterHasTitle(node.value, titlePattern)) { |
| 97 | + h1Count++; |
| 98 | + } |
| 99 | + }, |
| 100 | + |
| 101 | + html(node) { |
| 102 | + let match; |
| 103 | + while ((match = h1TagPattern.exec(node.value)) !== null) { |
| 104 | + h1Count++; |
| 105 | + if (h1Count > 1) { |
| 106 | + const { |
| 107 | + lineOffset: startLineOffset, |
| 108 | + columnOffset: startColumnOffset, |
| 109 | + } = findOffsets(node.value, match.index); |
| 110 | + |
| 111 | + const { |
| 112 | + lineOffset: endLineOffset, |
| 113 | + columnOffset: endColumnOffset, |
| 114 | + } = findOffsets( |
| 115 | + node.value, |
| 116 | + match.index + match[0].length, |
| 117 | + ); |
| 118 | + |
| 119 | + const nodeStartLine = node.position.start.line; |
| 120 | + const nodeStartColumn = node.position.start.column; |
| 121 | + const startLine = nodeStartLine + startLineOffset; |
| 122 | + const endLine = nodeStartLine + endLineOffset; |
| 123 | + const startColumn = |
| 124 | + (startLine === nodeStartLine |
| 125 | + ? nodeStartColumn |
| 126 | + : 1) + startColumnOffset; |
| 127 | + const endColumn = |
| 128 | + (endLine === nodeStartLine ? nodeStartColumn : 1) + |
| 129 | + endColumnOffset; |
| 130 | + |
| 131 | + context.report({ |
| 132 | + loc: { |
| 133 | + start: { |
| 134 | + line: startLine, |
| 135 | + column: startColumn, |
| 136 | + }, |
| 137 | + end: { |
| 138 | + line: endLine, |
| 139 | + column: endColumn, |
| 140 | + }, |
| 141 | + }, |
| 142 | + messageId: "multipleH1", |
| 143 | + }); |
| 144 | + } |
| 145 | + } |
| 146 | + }, |
| 147 | + |
| 148 | + heading(node) { |
| 149 | + if (node.depth === 1) { |
| 150 | + h1Count++; |
| 151 | + if (h1Count > 1) { |
| 152 | + context.report({ |
| 153 | + loc: node.position, |
| 154 | + messageId: "multipleH1", |
| 155 | + }); |
| 156 | + } |
| 157 | + } |
| 158 | + }, |
| 159 | + }; |
| 160 | + }, |
| 161 | +}; |
0 commit comments