deno.land / x / lume@v2.1.4 / core / formats.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104import type { Engine } from "./renderer.ts";import type { Loader } from "./fs.ts";
export interface Format { /** The file extension for this format */ ext: string;
/** The type of page */ pageType?: "page" | "asset";
/** The file loader used for this format (used by pages, includes, components, etc) */ loader?: Loader;
/** The loader used as asset */ assetLoader?: Loader;
/** Loader for _data files in this format */ dataLoader?: Loader;
/** * The template engines used to render this format * Used to render the page and components */ engines?: Engine[];
/** Whether this file must be copied instead loaded */ copy?: boolean | ((path: string) => string);}
/** Class to store loaders, engines and other stuff related with different formats */export default class Formats { entries = new Map<string, Format>();
get size(): number { return this.entries.size; }
/** Assign a value to a extension */ set(format: Format, override = true): void { const ext = format.ext.toLowerCase(); const existing = this.entries.get(ext);
if (existing) { if (override) { this.entries.set(ext, { ...existing, ...format }); } else { this.entries.set(ext, { ...format, ...existing }); } return; }
// Simple extension (.ts, .js, .json) if (ext.match(/^\.\w+$/)) { this.entries.set(ext, format); return; }
// Chained extension (.tmpl.js) goes first if (ext.match(/^\.\w+\.\w+$/)) { const entries = Array.from(this.entries.entries()); entries.unshift([ext, format]); this.entries = new Map(entries); return; }
throw new Error( `Invalid file extension: "${ext}". It must start with '.'`, ); }
/** Returns a format by extension */ get(extension: string): Format | undefined { return this.entries.get(extension.toLowerCase()); }
/** Delete a format */ delete(extension: string): void { this.entries.delete(extension.toLowerCase()); }
/** Returns if a format exists */ has(extension: string): boolean { return this.entries.has(extension.toLowerCase()); }
/** Search and return the associated format for a path */ search(path: string): Format | undefined { path = path.toLowerCase();
for (const format of this.entries.values()) { if (path.endsWith(format.ext)) { return format; } } }
/** Delete a cached template */ deleteCache(file: string): void { for (const format of this.entries.values()) { format.engines?.forEach((engine) => engine.deleteCache(file)); } }}
Version Info