|
| 1 | +// @ts-ignore - markdownlint-rule-helpers doesn't have TypeScript declarations |
| 2 | +import { addError } from 'markdownlint-rule-helpers' |
| 3 | + |
| 4 | +import { getFrontmatter } from '../helpers/utils' |
| 5 | +import { liquid } from '@/content-render/index' |
| 6 | +import type { RuleParams, RuleErrorCallback } from '@/content-linter/types' |
| 7 | + |
| 8 | +export const journeyTracksLiquid = { |
| 9 | + names: ['GHD058', 'journey-tracks-liquid'], |
| 10 | + description: 'Journey track properties must use valid Liquid syntax', |
| 11 | + tags: ['frontmatter', 'journey-tracks', 'liquid'], |
| 12 | + function: (params: RuleParams, onError: RuleErrorCallback) => { |
| 13 | + // Using any for frontmatter as it's a dynamic YAML object with varying properties |
| 14 | + const fm: any = getFrontmatter(params.lines) |
| 15 | + if (!fm || !fm.journeyTracks || !Array.isArray(fm.journeyTracks)) return |
| 16 | + if (!fm.layout || fm.layout !== 'journey-landing') return |
| 17 | + |
| 18 | + // Find the base journeyTracks line |
| 19 | + const journeyTracksLine: string | undefined = params.lines.find((line: string) => |
| 20 | + line.trim().startsWith('journeyTracks:'), |
| 21 | + ) |
| 22 | + const baseLineNumber: number = journeyTracksLine |
| 23 | + ? params.lines.indexOf(journeyTracksLine) + 1 |
| 24 | + : 1 |
| 25 | + |
| 26 | + fm.journeyTracks.forEach((track: any, trackIndex: number) => { |
| 27 | + // Try to find the line number for this specific journey track so we can use that for the error |
| 28 | + // line number. Getting the exact line number is probably more work than it's worth for this |
| 29 | + // particular rule. |
| 30 | + |
| 31 | + // Look for the track by finding the nth occurrence of track-like patterns after journeyTracks |
| 32 | + let trackLineNumber: number = baseLineNumber |
| 33 | + if (journeyTracksLine) { |
| 34 | + let trackCount: number = 0 |
| 35 | + for (let i = params.lines.indexOf(journeyTracksLine) + 1; i < params.lines.length; i++) { |
| 36 | + const line: string = params.lines[i].trim() |
| 37 | + // Look for track indicators (array item with id, title, or description) |
| 38 | + if ( |
| 39 | + line.startsWith('- id:') || |
| 40 | + line.startsWith('- title:') || |
| 41 | + (line === '-' && |
| 42 | + i + 1 < params.lines.length && |
| 43 | + (params.lines[i + 1].trim().startsWith('id:') || |
| 44 | + params.lines[i + 1].trim().startsWith('title:'))) |
| 45 | + ) { |
| 46 | + if (trackCount === trackIndex) { |
| 47 | + trackLineNumber = i + 1 |
| 48 | + break |
| 49 | + } |
| 50 | + trackCount++ |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Simple validation - just check if liquid can parse each string property |
| 56 | + const properties = [ |
| 57 | + { name: 'title', value: track.title }, |
| 58 | + { name: 'description', value: track.description }, |
| 59 | + ] |
| 60 | + |
| 61 | + properties.forEach((prop) => { |
| 62 | + if (prop.value && typeof prop.value === 'string') { |
| 63 | + try { |
| 64 | + liquid.parse(prop.value) |
| 65 | + } catch (error: any) { |
| 66 | + addError( |
| 67 | + onError, |
| 68 | + trackLineNumber, |
| 69 | + `Invalid Liquid syntax in journey track ${prop.name} (track ${trackIndex + 1}): ${error.message}`, |
| 70 | + prop.value, |
| 71 | + ) |
| 72 | + } |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + if (track.guides && Array.isArray(track.guides)) { |
| 77 | + track.guides.forEach((guide: string, guideIndex: number) => { |
| 78 | + if (typeof guide === 'string') { |
| 79 | + try { |
| 80 | + liquid.parse(guide) |
| 81 | + } catch (error: any) { |
| 82 | + addError( |
| 83 | + onError, |
| 84 | + trackLineNumber, |
| 85 | + `Invalid Liquid syntax in journey track guide (track ${trackIndex + 1}, guide ${guideIndex + 1}): ${error.message}`, |
| 86 | + guide, |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | + }) |
| 93 | + }, |
| 94 | +} |
0 commit comments