Skip to content

Commit

Permalink
feat(core): add option to exclude files from collection
Browse files Browse the repository at this point in the history
  • Loading branch information
sdorra committed Apr 12, 2024
1 parent 725a71b commit 58e659b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/silver-planets-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@content-collections/core": minor
---

Add option to exclude files from collection
41 changes: 41 additions & 0 deletions packages/core/src/collector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,45 @@ describe("collector", () => {

expect(collections).toHaveLength(2);
});

it("should exclude file", async () => {
const [collection] = await collect([
{
directory: "./__tests__/sources/test/",
include: "*.md",
exclude: "002.md",
parser: "frontmatter",
},
]);

expect(collection?.files).toHaveLength(1);
});

it("should exclude multiple files", async () => {
const [collection] = await collect([
{
directory: "./__tests__/sources/test/",
include: "*.md",
exclude: ["001.md", "002.md"],
parser: "frontmatter",
},
]);

expect(collection?.files).toHaveLength(0);
});

it("should exclude files which matches glob", async () => {
const [collection] = await collect([
{
directory: "./__tests__/sources/test/",
include: "*.md",
exclude: "00?.md",
parser: "frontmatter",
},
]);

expect(collection?.files).toHaveLength(0);
});
});

it("should treat dates as string", async () => {
Expand All @@ -197,4 +236,6 @@ describe("collector", () => {

expect(typeof file.data.published).toBe("string");
});


});
13 changes: 13 additions & 0 deletions packages/core/src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,25 @@ export function createCollector(emitter: Emitter, baseDirectory: string = ".") {
}
}

function createIgnorePattern<T extends FileCollection>(collection: T): Array<string> | undefined {
if (collection.exclude) {
if (Array.isArray(collection.exclude)) {
return collection.exclude;
} else {
return [collection.exclude];
}
}
return undefined;
}

async function resolveCollection<T extends FileCollection>(collection: T) {
const collectionDirectory = path.join(baseDirectory, collection.directory);

const filePaths = await fg(collection.include, {
cwd: collectionDirectory,
onlyFiles: true,
absolute: false,
ignore: createIgnorePattern(collection),
});
const promises = filePaths.map((filePath) =>
collectFile(collection, filePath)
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type CollectionRequest<
transform?: (data: TSchema, context: Context) => TTransformResult;
directory: string;
include: string | string[];
exclude?: string | string[];
onSuccess?: (documents: Array<TDocument>) => void | Promise<void>;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type CollectionFile = {

export type FileCollection = Pick<
AnyCollection,
"directory" | "include" | "parser"
"directory" | "include" | "exclude" | "parser"
>;

export type ResolvedCollection<T extends FileCollection> = T & {
Expand Down

0 comments on commit 58e659b

Please sign in to comment.