|
| 1 | +import { addError } from 'markdownlint-rule-helpers' |
| 2 | +import { getFrontmatter } from '@/content-linter/lib/helpers/utils' |
| 3 | + |
| 4 | +export const frontmatterVersionsWhitespace = { |
| 5 | + names: ['GHD051', 'frontmatter-versions-whitespace'], |
| 6 | + description: 'Versions frontmatter should not contain unnecessary whitespace', |
| 7 | + tags: ['frontmatter', 'versions'], |
| 8 | + function: (params, onError) => { |
| 9 | + const fm = getFrontmatter(params.lines) |
| 10 | + if (!fm || !fm.versions) return |
| 11 | + |
| 12 | + const versionsObj = fm.versions |
| 13 | + if (typeof versionsObj !== 'object') return |
| 14 | + |
| 15 | + // Find the frontmatter section in the file |
| 16 | + const fmStartIndex = params.lines.findIndex((line) => line.trim() === '---') |
| 17 | + if (fmStartIndex === -1) return |
| 18 | + |
| 19 | + // Check each version entry for whitespace issues |
| 20 | + Object.entries(versionsObj).forEach(([key, value]) => { |
| 21 | + if (typeof value !== 'string') return |
| 22 | + |
| 23 | + const hasUnwantedWhitespace = checkForUnwantedWhitespace(value) |
| 24 | + if (hasUnwantedWhitespace) { |
| 25 | + // Find the line containing this version key |
| 26 | + const versionLineIndex = params.lines.findIndex((line, index) => { |
| 27 | + return index > fmStartIndex && line.trim().startsWith(`${key}:`) && line.includes(value) |
| 28 | + }) |
| 29 | + |
| 30 | + if (versionLineIndex !== -1) { |
| 31 | + const line = params.lines[versionLineIndex] |
| 32 | + const lineNumber = versionLineIndex + 1 |
| 33 | + const cleanedValue = getCleanedValue(value) |
| 34 | + |
| 35 | + // Create fix info to remove unwanted whitespace |
| 36 | + const fixInfo = { |
| 37 | + editColumn: line.indexOf(value) + 1, |
| 38 | + deleteCount: value.length, |
| 39 | + insertText: cleanedValue, |
| 40 | + } |
| 41 | + |
| 42 | + addError( |
| 43 | + onError, |
| 44 | + lineNumber, |
| 45 | + `Versions frontmatter should not contain leading or trailing whitespace. Found: '${value}', expected: '${cleanedValue}'`, |
| 46 | + line, |
| 47 | + [line.indexOf(value) + 1, value.length], |
| 48 | + fixInfo, |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | + }) |
| 53 | + }, |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Check if a version string has unwanted whitespace |
| 58 | + * Allows whitespace in complex expressions like '<3.6 >3.8' |
| 59 | + * but disallows leading/trailing whitespace |
| 60 | + */ |
| 61 | +function checkForUnwantedWhitespace(value) { |
| 62 | + // Don't flag if the value is just whitespace or empty |
| 63 | + if (!value || value.trim() === '') return false |
| 64 | + |
| 65 | + // Check for leading or trailing whitespace |
| 66 | + if (value !== value.trim()) return true |
| 67 | + |
| 68 | + // Allow whitespace around operators in complex expressions |
| 69 | + // This regex matches patterns like '<3.6 >3.8', '>=2.19', etc. |
| 70 | + const hasOperators = /[<>=]/.test(value) |
| 71 | + if (hasOperators) { |
| 72 | + // For operator expressions, we're more lenient about internal whitespace |
| 73 | + // Only flag if there's leading/trailing whitespace (already checked above) |
| 74 | + return false |
| 75 | + } |
| 76 | + |
| 77 | + // For simple version strings (like 'fpt', 'ghec'), no internal whitespace should be allowed |
| 78 | + // This catches cases like 'f pt' where there's whitespace in the middle |
| 79 | + return /\s/.test(value) |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Get the cleaned version of a value by removing appropriate whitespace |
| 84 | + */ |
| 85 | +function getCleanedValue(value) { |
| 86 | + // For values with operators, just trim leading/trailing whitespace |
| 87 | + const hasOperators = /[<>=]/.test(value) |
| 88 | + if (hasOperators) { |
| 89 | + return value.trim() |
| 90 | + } |
| 91 | + |
| 92 | + // For simple version strings, remove all whitespace |
| 93 | + return value.replace(/\s/g, '') |
| 94 | +} |
0 commit comments