getComponents
Overview
Section titled “Overview”Treats the graph as undirected and extracts its connected components. BFS groups nodes that are reachable from one another, returning “clusters” of documents.
Why it exists
Section titled “Why it exists”Even in a large repository, documents often split into several independent clusters in practice. getComponents lets you see which document groups form a coherent context together and whether any isolated clusters exist.
It is also useful for finding orphan documents — components of size 1, where no document references them and they reference nothing (the GRP-003 rule detects this automatically).
Signature
Section titled “Signature”function getComponents(graph: ContextGraph): string[][];Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
graph | ContextGraph | ✓ | The graph built by buildContextGraph. |
Return value
Section titled “Return value”Returns string[][]. Each component is an array of file paths sorted alphabetically. The components themselves are also sorted deterministically by their first file path.
Example
Section titled “Example”import { buildContextGraph, getComponents, loadDocuments } from "@contextlint/core";
const documents = loadDocuments(["docs/**/*.md"]);const graph = buildContextGraph(documents);
const components = getComponents(graph);
console.log(`Detected ${components.length} clusters`);
for (const [index, component] of components.entries()) { console.log(`\nCluster ${index + 1}: ${component.length} files`); for (const file of component) { console.log(` - ${file}`); }}
// Extract orphan documentsconst orphans = components.filter((c) => c.length === 1).flat();console.log(`Orphan documents: ${orphans.length}`);Related functions
Section titled “Related functions”buildContextGraph— Build the input graphtopologicalSort— The directed counterpart that orders by dependencygetContextSlice— Get related documents starting from a query