|
| 1 | +import { addError, filterTokens } from 'markdownlint-rule-helpers' |
| 2 | + |
| 3 | +export const headerContentRequirement = { |
| 4 | + names: ['GHD053', 'header-content-requirement'], |
| 5 | + description: 'Headers must have content between them, such as an introduction', |
| 6 | + tags: ['headers', 'structure', 'content'], |
| 7 | + function: (params, onError) => { |
| 8 | + const headings = [] |
| 9 | + |
| 10 | + // Collect all heading tokens with their line numbers and levels |
| 11 | + filterTokens(params, 'heading_open', (token) => { |
| 12 | + headings.push({ |
| 13 | + token, |
| 14 | + lineNumber: token.lineNumber, |
| 15 | + level: parseInt(token.tag.slice(1)), // Extract number from h1, h2, etc. |
| 16 | + line: params.lines[token.lineNumber - 1], |
| 17 | + }) |
| 18 | + }) |
| 19 | + |
| 20 | + // Check each pair of consecutive headings |
| 21 | + for (let i = 0; i < headings.length - 1; i++) { |
| 22 | + const currentHeading = headings[i] |
| 23 | + const nextHeading = headings[i + 1] |
| 24 | + |
| 25 | + // Only check if next heading is a subheading (higher level number) |
| 26 | + if (nextHeading.level > currentHeading.level) { |
| 27 | + const hasContent = checkForContentBetweenHeadings( |
| 28 | + params.lines, |
| 29 | + currentHeading.lineNumber, |
| 30 | + nextHeading.lineNumber, |
| 31 | + ) |
| 32 | + |
| 33 | + if (!hasContent) { |
| 34 | + addError( |
| 35 | + onError, |
| 36 | + nextHeading.lineNumber, |
| 37 | + `Header must have introductory content before subheader. Add content between "${currentHeading.line.trim()}" and "${nextHeading.line.trim()}".`, |
| 38 | + nextHeading.line, |
| 39 | + null, // No specific range within the line |
| 40 | + null, // No fix possible - requires manual content addition |
| 41 | + ) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Check if there is meaningful content between two headings |
| 50 | + * Returns true if content exists, false if only whitespace/empty lines |
| 51 | + */ |
| 52 | +function checkForContentBetweenHeadings(lines, startLineNumber, endLineNumber) { |
| 53 | + // Convert to 0-based indexes and skip the heading lines themselves |
| 54 | + const startIndex = startLineNumber // Skip the current heading line |
| 55 | + const endIndex = endLineNumber - 2 // Stop before the next heading line |
| 56 | + |
| 57 | + // Check each line between the headings |
| 58 | + for (let i = startIndex; i <= endIndex; i++) { |
| 59 | + if (i >= lines.length) break |
| 60 | + |
| 61 | + const line = lines[i].trim() |
| 62 | + |
| 63 | + // Skip empty lines |
| 64 | + if (line === '') continue |
| 65 | + |
| 66 | + // Skip frontmatter delimiters |
| 67 | + if (line === '---') continue |
| 68 | + |
| 69 | + // Skip Liquid tags that don't produce visible content |
| 70 | + if (isNonContentLiquidTag(line)) continue |
| 71 | + |
| 72 | + // If we find any other content, consider it valid |
| 73 | + if (line.length > 0) { |
| 74 | + return true |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return false |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Check if a line contains only Liquid tags that don't produce visible content |
| 83 | + * This helps avoid false positives for conditional blocks |
| 84 | + */ |
| 85 | +function isNonContentLiquidTag(line) { |
| 86 | + // Match common non-content Liquid tags |
| 87 | + const nonContentTags = [ |
| 88 | + /^{%\s*ifversion\s+.*%}$/, |
| 89 | + /^{%\s*elsif\s+.*%}$/, |
| 90 | + /^{%\s*else\s*%}$/, |
| 91 | + /^{%\s*endif\s*%}$/, |
| 92 | + /^{%\s*if\s+.*%}$/, |
| 93 | + /^{%\s*unless\s+.*%}$/, |
| 94 | + /^{%\s*endunless\s*%}$/, |
| 95 | + /^{%\s*comment\s*%}$/, |
| 96 | + /^{%\s*endcomment\s*%}$/, |
| 97 | + ] |
| 98 | + |
| 99 | + return nonContentTags.some((pattern) => pattern.test(line)) |
| 100 | +} |
0 commit comments