コンテンツにスキップ

buildContextGraph

解析済みドキュメントの Map から、ドキュメント間の依存関係を表すグラフを構築します。各ドキュメントの相対リンクと画像参照を辺(edge)として収集し、ノード(node)には入次数と出次数を付与します。

Graph API の他関数(getImpactSettopologicalSortclassifyImpact など)はすべて、この関数が返す ContextGraph を入力として受け取ります。グラフ操作の起点となる関数です。

辺はソースファイルの相対リンクから解決され、解決先が documents Map に存在する場合のみ追加されます。アンカーフラグメント(#section)は除去され、自己参照は無視されます。出力は決定論的で、ノード・辺ともファイルパスでソートされます。

function buildContextGraph(
documents: Map<string, ParsedDocument>,
): ContextGraph;

関連する型は次のとおりです。

interface GraphNode {
filePath: string;
inDegree: number;
outDegree: number;
}
interface GraphEdge {
source: string;
target: string;
type: "link" | "image";
line: number;
}
interface ContextGraph {
nodes: GraphNode[];
edges: GraphEdge[];
}
パラメータ必須説明
documentsMap<string, ParsedDocument>ファイルパスをキー、parseDocument の結果を値とする Map。キーは相対パスでも絶対パスでも構いませんが、lintFiles と同様に相対パスを推奨します

ContextGraph を返します。

  • nodes — 各ファイルパスとその入次数・出次数。ファイルパスでソート済み
  • edgessource から target への有向辺。type はリンクか画像参照かを示します。ソース・ターゲット・行番号でソート済み
import { loadDocuments } from "@contextlint/core";
import { buildContextGraph } from "@contextlint/core";
const documents = loadDocuments(["docs/**/*.md"]);
const graph = buildContextGraph(documents);
console.log(`${graph.nodes.length} ファイル、${graph.edges.length}`);
for (const edge of graph.edges) {
console.log(`${edge.source}${edge.target} (${edge.type}, line ${edge.line})`);
}