Graphs that teach > graphs that impress. Turn any code into an interactive knowledge graph you can explore, search, and ask questions about. Works with Claude Code, Codex, Cursor, Copilot, Gemini CLI, and more.
TypeScript antigravity-skillsbusiness-knowledgeclaude-codeclaude-skillscodebase-analysiscodexcodex-skillsdeveloper-tools-ai-agentgemini-cli-skillskarpathy-llm-wikiknowledge-baseknowledge-graphmemoryopencode-skillspi-agentunderstandcodevibe-coding
feat(core): deterministic Figma document parser (pages/screens/components/instances)
grumingb229231e11cc
diff --git a/understand-anything-plugin/packages/core/src/figma/__tests__/parse-document.test.ts b/understand-anything-plugin/packages/core/src/figma/__tests__/parse-document.test.tsnew file mode 100644index 00000000..9010c95b--- /dev/null+++ b/understand-anything-plugin/packages/core/src/figma/__tests__/parse-document.test.ts@@ -0,0 +1,50 @@+import { describe, it, expect } from "vitest";+import { parseDocument } from "../parse/parse-document";+import type { FigmaDocument } from "../source/types";++const doc: FigmaDocument = {+ name: "MyApp",+ document: {+ id: "0:0", name: "Document", type: "DOCUMENT", children: [+ { id: "1:0", name: "Onboarding", type: "CANVAS", children: [+ { id: "1:1", name: "Login", type: "FRAME", absoluteBoundingBox: { width: 375, height: 812 }, children: [+ { id: "1:2", name: "SignInBtn", type: "INSTANCE", componentId: "2:1", children: [] },+ ] },+ ] },+ { id: "1:9", name: "Components", type: "CANVAS", children: [+ { id: "2:0", name: "Button", type: "COMPONENT_SET", children: [+ { id: "2:1", name: "Primary", type: "COMPONENT", children: [] },+ { id: "2:2", name: "Secondary", type: "COMPONENT", children: [] },+ ] },+ ] },+ ],+ },+};++describe("parseDocument", () => {+ const { nodes, edges } = parseDocument(doc, "ABC123");+ const ids = nodes.map((n) => n.id);+ const has = (s: string, t: string, ty: string) =>+ edges.some((e) => e.source === s && e.target === t && e.type === ty);++ it("creates page/screen/instance/componentSet/component nodes", () => {+ expect(ids).toEqual(expect.arrayContaining([+ "page:1:0", "screen:1:1", "instance:1:2", "page:1:9", "componentSet:2:0", "component:2:1", "component:2:2",+ ]));+ });+ it("links containment, instance_of, and variant_of", () => {+ expect(has("page:1:0", "screen:1:1", "contains")).toBe(true);+ expect(has("screen:1:1", "instance:1:2", "contains")).toBe(true);+ expect(has("instance:1:2", "component:2:1", "instance_of")).toBe(true);+ expect(has("component:2:1", "componentSet:2:0", "variant_of")).toBe(true);+ expect(has("component:2:2", "componentSet:2:0", "variant_of")).toBe(true);+ });+ it("captures screen dimensions and fileKey in figmaMeta", () => {+ const screen = nodes.find((n) => n.id === "screen:1:1")!;+ expect(screen.figmaMeta?.dimensions?.width).toBe(375);+ expect(screen.figmaMeta?.fileKey).toBe("ABC123");+ });+ it("emits validateGraph-ready nodes (summary/tags/complexity present)", () => {+ expect(nodes.every((n) => n.summary && n.tags.length > 0 && n.complexity)).toBe(true);+ });+});diff --git a/understand-anything-plugin/packages/core/src/figma/parse/parse-document.ts b/understand-anything-plugin/packages/core/src/figma/parse/parse-document.tsnew file mode 100644index 00000000..d48e9d92--- /dev/null+++ b/understand-anything-plugin/packages/core/src/figma/parse/parse-document.ts@@ -0,0 +1,97 @@+import type { GraphNode, GraphEdge } from "../../types";+import type { FigmaDocument, FigmaNode } from "../source/types";++function mkNode(+ type: GraphNode["type"],+ figmaId: string,+ name: string,+ figmaMeta: GraphNode["figmaMeta"],+): GraphNode {+ return {+ id: `${type}:${figmaId}`,+ type,+ name,+ summary: name, // placeholder; design-analyzer enriches in Phase 2+ tags: [type],+ complexity: "simple",+ figmaMeta,+ };+}++export function parseDocument(doc: FigmaDocument, fileKey: string): { nodes: GraphNode[]; edges: GraphEdge[] } {+ const nodes: GraphNode[] = [];+ const edges: GraphEdge[] = [];+ const seen = new Set<string>();++ const add = (n: GraphNode) => { if (!seen.has(n.id)) { seen.add(n.id); nodes.push(n); } };+ const link = (source: string, target: string, type: GraphEdge["type"], weight: number) =>+ edges.push({ source, target, type, direction: "forward", weight });++ // Deep-read a screen subtree to find instances (shallow node set, but deep read).+ function collectInstances(n: FigmaNode, screenId: string) {+ for (const child of n.children ?? []) {+ if (child.type === "INSTANCE") {+ const inst = mkNode("instance", child.id, child.name, {+ fileKey, nodeId: child.id, figmaType: "INSTANCE",+ componentKey: child.componentId,+ prototypeTargets: child.transitionNodeID ? [child.transitionNodeID] : undefined,+ });+ add(inst);+ link(screenId, inst.id, "contains", 1.0);+ if (child.componentId) link(inst.id, `component:${child.componentId}`, "instance_of", 0.8);+ }+ if (child.children) collectInstances(child, screenId);+ }+ }++ function handlePageChild(child: FigmaNode, pageId: string) {+ switch (child.type) {+ case "FRAME": {+ const screen = mkNode("screen", child.id, child.name, {+ fileKey, nodeId: child.id, figmaType: "FRAME",+ dimensions: child.absoluteBoundingBox+ ? { width: child.absoluteBoundingBox.width, height: child.absoluteBoundingBox.height }+ : undefined,+ });+ add(screen);+ link(pageId, screen.id, "contains", 1.0);+ collectInstances(child, screen.id);+ break;+ }+ case "COMPONENT": {+ const comp = mkNode("component", child.id, child.name, { fileKey, nodeId: child.id, figmaType: "COMPONENT" });+ add(comp);+ link(pageId, comp.id, "contains", 1.0);+ break;+ }+ case "COMPONENT_SET": {+ const set = mkNode("componentSet", child.id, child.name, { fileKey, nodeId: child.id, figmaType: "COMPONENT_SET" });+ add(set);+ link(pageId, set.id, "contains", 1.0);+ for (const variant of child.children ?? []) {+ if (variant.type === "COMPONENT") {+ const comp = mkNode("component", variant.id, variant.name, { fileKey, nodeId: variant.id, figmaType: "COMPONENT" });+ add(comp);+ link(comp.id, set.id, "variant_of", 0.9);+ }+ }+ break;+ }+ case "SECTION": {+ for (const sub of child.children ?? []) handlePageChild(sub, pageId); // flatten sections in v1+ break;+ }+ default:+ break; // other top-level types are ignored in v1+ }+ }++ for (const canvas of doc.document.children ?? []) {+ if (canvas.type !== "CANVAS") continue;+ const page = mkNode("page", canvas.id, canvas.name, { fileKey, nodeId: canvas.id, figmaType: "CANVAS" });+ add(page);+ for (const child of canvas.children ?? []) handlePageChild(child, page.id);+ }++ return { nodes, edges };+}