import { escapeHtml, getActionButtonsHtml, getGitHubUrl, getInstallDropdownHtml, getLastUpdatedHtml, } from '../utils'; export interface RenderableInstruction { title: string; description?: string; path: string; applyTo?: string | string[] | null; extensions?: string[]; lastUpdated?: string | null; } export type InstructionSortOption = 'title' | 'lastUpdated'; export function sortInstructions( items: T[], sort: InstructionSortOption ): T[] { return [...items].sort((a, b) => { if (sort === 'lastUpdated') { const dateA = a.lastUpdated ? new Date(a.lastUpdated).getTime() : 0; const dateB = b.lastUpdated ? new Date(b.lastUpdated).getTime() : 0; return dateB - dateA; } return a.title.localeCompare(b.title); }); } export function renderInstructionsHtml( items: RenderableInstruction[], options: { query?: string; highlightTitle?: (title: string, query: string) => string; } = {} ): string { const { query = '', highlightTitle } = options; if (items.length === 0) { return `

No instructions found

Try a different search term or adjust filters

`; } return items .map((item) => { const applyToText = Array.isArray(item.applyTo) ? item.applyTo.join(', ') : item.applyTo; const titleHtml = query && highlightTitle ? highlightTitle(item.title, query) : escapeHtml(item.title); return `
${titleHtml}
${escapeHtml(item.description || 'No description')}
${applyToText ? `applies to: ${escapeHtml(applyToText)}` : ''} ${item.extensions?.slice(0, 4).map((extension) => `${escapeHtml(extension)}`).join('') || ''} ${item.extensions && item.extensions.length > 4 ? `+${item.extensions.length - 4} more` : ''} ${getLastUpdatedHtml(item.lastUpdated)}
${getInstallDropdownHtml('instructions', item.path, true)} ${getActionButtonsHtml(item.path, true)} GitHub
`; }) .join(''); }