paths.ts (1359B)
1 export function normalizePath(value: string): string { 2 const input = String(value || '').replace(/\\/g, '/'); 3 if (!input || input.includes('\0') || input.startsWith('/')) { 4 throw new Error(`Invalid template path: ${value}`); 5 } 6 7 const parts: string[] = []; 8 for (const part of input.split('/')) { 9 if (!part || part === '.') continue; 10 if (part === '..') throw new Error(`Invalid template path: ${value}`); 11 parts.push(part); 12 } 13 14 if (parts.length === 0) throw new Error(`Invalid template path: ${value}`); 15 return parts.join('/'); 16 } 17 18 export function resolvePath(baseFile: string, reference: string): string { 19 const value = String(reference || '').trim(); 20 if (!value) throw new Error('Empty template reference'); 21 if (value.startsWith('/')) throw new Error(`Invalid template reference: ${reference}`); 22 23 const base = baseFile.split('/'); 24 base.pop(); 25 return normalizePath([...base, value].join('/')); 26 } 27 28 export function templateReference(value: string): string | null { 29 const prefix = 'template://'; 30 if (!value.startsWith(prefix)) return null; 31 return normalizePath(decodeURIComponent(value.slice(prefix.length))); 32 } 33 34 export function extensionOf(path: string): string { 35 const name = path.slice(path.lastIndexOf('/') + 1); 36 const dot = name.lastIndexOf('.'); 37 return dot === -1 ? '' : name.slice(dot).toLowerCase(); 38 }