Skip to content

Commit c3eb6ef

Browse files
committed
added search.file()
1 parent 5b3054e commit c3eb6ef

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ Go to the `v2` branch to see the changelog of Lume 2.
77
Go to the `v1` branch to see the changelog of Lume 1.
88

99
## [3.1.2] - Unreleased
10+
### Added
11+
- `search.file()` to search for a single file.
12+
13+
### Changed
14+
- Cache results of `search.files()`.
15+
1016
### Fixed
11-
- Ensure console output for validate_html and seo plugins are after the build process.
17+
- Ensure console output for validate_html and seo plugins is shown after the build process.
1218
- `run` command.
1319
- Added missing tests for SEO plugin.
1420
- Renamed `Config` interface of `seo` and `validate_html` plugins to `Options`.
1521
- Added export-ignore paths to `.gitattributes`.
16-
- `site.remote()`: support commit SHA
22+
- `site.remote()`: support commit SHA for GitHub versions.
1723
- Updated dependencies: `deno-loader`, `tailwindcss`, `html-validate`, `magic-string`, `minify-html` and some icons.
1824

1925
## [3.1.1] - 2025-10-20

core/searcher.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default class Searcher {
2626
#files: StaticFile[];
2727
#sourceData: Map<string, Partial<Data>>;
2828
#cache = new Map<string, Data[]>();
29+
#cacheFiles = new Map<string, string[]>();
2930
#filters: Filter[];
3031

3132
constructor(options: Options) {
@@ -38,6 +39,7 @@ export default class Searcher {
3839
/** Clear the cache (used after a change in watch mode) */
3940
deleteCache() {
4041
this.#cache.clear();
42+
this.#cacheFiles.clear();
4143
}
4244

4345
/**
@@ -78,19 +80,12 @@ export default class Searcher {
7880

7981
/** Search files using a glob */
8082
files(globOrRegexp?: RegExp | string): string[] {
81-
const files = this.#files.map((file) => file.outputPath);
82-
const pages = this.#pages.map((page) => page.outputPath);
83-
const allFiles = [...files, ...pages];
84-
85-
if (!globOrRegexp) {
86-
return allFiles;
87-
}
88-
89-
const regexp = typeof globOrRegexp === "string"
90-
? globToRegExp(globOrRegexp)
91-
: globOrRegexp;
83+
return this.#searchFiles(globOrRegexp);
84+
}
9285

93-
return allFiles.filter((file) => regexp.test(file));
86+
/** Search a single file using a glob */
87+
file(globOrRegexp?: RegExp | string): string | undefined {
88+
return this.#searchFiles(globOrRegexp)[0];
9489
}
9590

9691
/** Returns all values from the same key of a search */
@@ -154,6 +149,30 @@ export default class Searcher {
154149
this.#cache.set(id, result);
155150
return [...result] as (Data & T)[];
156151
}
152+
153+
#searchFiles(globOrRegexp?: RegExp | string): string[] {
154+
const id = typeof globOrRegexp === "string"
155+
? globOrRegexp
156+
: globOrRegexp?.source || "";
157+
158+
if (this.#cacheFiles.has(id)) {
159+
return [...this.#cacheFiles.get(id)!];
160+
}
161+
162+
const files = this.#files.map((file) => file.outputPath);
163+
const pages = this.#pages.map((page) => page.outputPath);
164+
let result: string[] = [...files, ...pages];
165+
166+
if (typeof globOrRegexp === "string") {
167+
const regexp = globToRegExp(globOrRegexp);
168+
result = result.filter((file) => regexp.test(file));
169+
} else if (globOrRegexp instanceof RegExp) {
170+
result = result.filter((file) => globOrRegexp.test(file));
171+
}
172+
173+
this.#cacheFiles.set(id, result);
174+
return [...result];
175+
}
157176
}
158177

159178
/**

0 commit comments

Comments
 (0)