deno.land / x / lume@v2.1.4 / core / searcher.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437import { globToRegExp } from "../deps/path.ts";import { normalizePath } from "./utils/path.ts";
import type { Data, Page, StaticFile } from "./file.ts";
export interface Options { /** The pages array */ pages: Page[];
/** The static files array */ files: StaticFile[];
/** Context data */ sourceData: Map<string, Partial<Data>>;
/** Filters to apply to all page searches */ filters?: Filter[];}
type Filter = (data: Data) => boolean;type Condition = [string, string, unknown];
/** Search helper */export default class Searcher { #pages: Page[]; #files: StaticFile[]; #sourceData: Map<string, Partial<Data>>; #cache = new Map<string, Data[]>(); #filters: Filter[];
constructor(options: Options) { this.#pages = options.pages; this.#files = options.files; this.#sourceData = options.sourceData; this.#filters = options.filters || []; }
/** Clear the cache (used after a change in watch mode) */ deleteCache() { this.#cache.clear(); }
/** * Return the data in the scope of a path (file or folder) */ data<T>(path = "/"): T & Partial<Data> | undefined { const normalized = normalizePath(path); const dirData = this.#sourceData.get(normalized);
if (dirData) { return dirData as T & Partial<Data>; }
const result = this.#pages.find((page) => page.data.url === normalized);
if (result) { return result.data as T & Partial<Data>; } }
/** Search pages */ pages<T>(query?: string, sort?: string, limit?: number): (Data & T)[] { const result = this.#searchPages<T>(query, sort);
if (!limit) { return result; }
return (limit < 0) ? result.slice(limit) : result.slice(0, limit); }
/** Search and return the first page */ page<T>(query?: string, sort?: string): Data & T | undefined { return this.pages<T>(query, sort)[0]; }
/** Search files using a glob */ files(globOrRegexp?: RegExp | string): string[] { const files = this.#files.map((file) => file.outputPath); const pages = this.#pages.map((page) => page.outputPath); const allFiles = [...files, ...pages];
if (!globOrRegexp) { return allFiles; }
const regexp = typeof globOrRegexp === "string" ? globToRegExp(globOrRegexp) : globOrRegexp;
return allFiles.filter((file) => regexp.test(file)); }
/** Returns all values from the same key of a search */ values<T = unknown>(key: string, query?: string): T[] { const values = new Set();
this.#searchPages(query).forEach((data) => { const value = data[key];
if (Array.isArray(value)) { value.forEach((v) => values.add(v)); } else if (value !== undefined) { values.add(value); } });
return Array.from(values) as T[]; }
/** Return the next page of a search */ nextPage<T = unknown>( url: string, query?: string, sort?: string, ): Data & T | undefined { const pages = this.#searchPages<T>(query, sort); const index = pages.findIndex((data) => data.url === url);
return (index === -1) ? undefined : pages[index + 1]; }
/** Return the previous page of a search */ previousPage<T = unknown>( url: string, query?: string, sort?: string, ): Data & T | undefined { const pages = this.#searchPages<T>(query, sort); const index = pages.findIndex((data) => data.url === url);
return (index <= 0) ? undefined : pages[index - 1]; }
#searchPages<T = unknown>(query?: string, sort = "date"): (Data & T)[] { const id = JSON.stringify([query, sort]);
if (this.#cache.has(id)) { return [...this.#cache.get(id)!] as (Data & T)[]; }
const compiledFilter = buildFilter(query); const filters = compiledFilter ? this.#filters.concat([compiledFilter]) : this.#filters; const result = filters.reduce( (pages, filter) => pages.filter(filter), this.#pages.map((page) => page.data), );
result.sort(buildSort(sort)); this.#cache.set(id, result); return [...result] as (Data & T)[]; }}
/** * Parse a query string and return a function to filter a search result * * example: "title=foo level<3" * returns: (page) => page.data.title === "foo" && page.data.level < 3 */export function buildFilter(query = ""): Filter | undefined { // (?:(not)?(fieldName)(operator))?(value|"value"|'value') const matches = query ? query.matchAll( /(?:(!)?([\w.-]+)([!^$*]?=|[<>]=?))?([^'"\s][^\s=<>]*|"[^"]+"|'[^']+')/g, ) : [];
const conditions: Condition[] = [];
for (const match of matches) { let [, not, key, operator, value] = match;
if (!key) { key = "tags"; operator = "*=";
if (value?.startsWith("!")) { not = not ? "" : "!"; value = value.slice(1); } }
if (not) { operator = "!" + operator; }
conditions.push([key, operator, compileValue(value)]); }
if (conditions.length) { return compileFilter(conditions); }}
/** * Convert a parsed query to a function * * example: [["title", "=", "foo"], ["level", "<", 3]] * returns: (data) => data.title === "foo" && data.level < 3 */function compileFilter(conditions: Condition[]) { const filters: string[] = []; const args: string[] = []; const values: unknown[] = [];
conditions.forEach((condition, index) => { const [key, operator, value] = condition; const varName = `value${index}`;
filters.push(compileCondition(key, operator, varName, value)); args.push(varName); values.push(value); });
args.push(`return (data) => ${filters.join(" && ")};`);
const factory = new Function(...args);
return factory(...values);}
/** * Convert a parsed condition to a function * * example: key = "data.title", operator = "=" name = "value0" value = "foo" * returns: data.title === value0 */function compileCondition( key: string, operator: string, name: string, value: unknown,) { key = key.replaceAll(".", "?.");
if (value instanceof Date) { switch (operator) { case "=": return `data.${key}?.getTime() === ${name}.getTime()`;
case "!=": return `data.${key}?.getTime() !== ${name}.getTime()`;
case "<": case "<=": case ">": case ">=": return `data.${key}?.getTime() ${operator} ${name}.getTime()`;
case "!<": case "!<=": case "!>": case "!>=": return `!(data.${key}?.getTime() ${ operator.substring(1) } ${name}.getTime())`;
default: throw new Error(`Operator ${operator} not valid for Date values`); } }
if (Array.isArray(value)) { switch (operator) { case "=": return `${name}.some((i) => data.${key} === i)`;
case "!=": return `${name}.some((i) => data.${key} !== i)`;
case "^=": return `${name}.some((i) => data.${key}?.startsWith(i))`;
case "!^=": return `!${name}.some((i) => data.${key}?.startsWith(i))`;
case "$=": return `${name}.some((i) => data.${key}?.endsWith(i))`;
case "!$=": return `!${name}.some((i) => data.${key}?.endsWith(i))`;
case "*=": return `${name}.some((i) => data.${key}?.includes(i))`;
case "!*=": return `${name}.some((i) => data.${key}?.includes(i))`;
case "!<": case "!<=": case "!>": case "!>=": return `!${name}.some((i) => data.${key} ${operator.substring(1)} i)`;
default: // < <= > >= return `${name}.some((i) => data.${key} ${operator} i)`; } }
switch (operator) { case "=": return `data.${key} === ${name}`;
case "!=": return `data.${key} !== ${name}`;
case "^=": return `data.${key}?.startsWith(${name})`;
case "!^=": return `!data.${key}?.startsWith(${name})`;
case "$=": return `data.${key}?.endsWith(${name})`;
case "!$=": return `!data.${key}?.endsWith(${name})`;
case "*=": return `data.${key}?.includes(${name})`;
case "!*=": return `!data.${key}?.includes(${name})`;
case "!<": case "!<=": case "!>": case "!>=": return `!(data.${key} ${operator.substring(1)} ${name})`;
default: // < <= > >= return `data.${key} ${operator} ${name}`; }}
/** * Compile a value and return the proper type * * example: "true" => true * example: "foo" => "foo" * example: "2021-06-12" => new Date(2021, 05, 12) */function compileValue(value: string): unknown { if (!value) { return value; }
// Remove quotes const quoted = !!value.match(/^('|")(.*)\1$/);
if (quoted) { value = value.slice(1, -1); }
if (value.includes("|")) { return value.split("|").map((val) => compileValue(val)); }
if (quoted) { return value; }
if (value.toLowerCase() === "true") { return true; } if (value.toLowerCase() === "false") { return false; } if (value.toLowerCase() === "undefined") { return undefined; } if (value.toLowerCase() === "null") { return null; } if (value.match(/^\d+$/)) { return Number(value); } if (typeof value === "number" && isFinite(value)) { return Number(value); } // Date or datetime values: // yyyy-mm // yyyy-mm-dd // yyyy-mm-ddThh // yyyy-mm-ddThh:ii // yyyy-mm-ddThh:ii:ss const match = value.match( /^(\d{4})-(\d\d)(?:-(\d\d))?(?:T(\d\d)(?::(\d\d))?(?::(\d\d))?)?$/i, );
if (match) { const [, year, month, day, hour, minute, second] = match;
return new Date( parseInt(year), parseInt(month) - 1, day ? parseInt(day) : 1, hour ? parseInt(hour) : 0, minute ? parseInt(minute) : 0, second ? parseInt(second) : 0, ); }
return value;}
/** * Convert a query to sort to a function * * example: "title=desc" * returns: (a, b) => a.title > b.title */export function buildSort(sort: string): (a: Data, b: Data) => number { let fn = "0";
const pieces = sort.split(/\s+/).filter((arg) => arg);
pieces.reverse().forEach((arg) => { const match = arg.match(/([\w.-]+)(?:=(asc|desc))?/);
if (!match) { return; }
let [, key, direction] = match; key = key.replaceAll(".", "?."); const operator = direction === "desc" ? ">" : "<"; fn = `(a.${key} == b.${key} ? ${fn} : (a.${key} ${operator} b.${key} ? -1 : 1))`; });
return new Function("a", "b", `return ${fn}`) as (a: Data, b: Data) => number;}
Version Info