From 87282f57d4cc744351f79c4339ef3c265b1583aa Mon Sep 17 00:00:00 2001 From: Roger Barnes Date: Tue, 1 Dec 2026 00:19:43 +1100 Subject: [PATCH] feat(graph): sort sibling nodes by reserved numeric order field (#240) --- docs/concepts.md | 4 ++ plugin/.claude-plugin/plugin.json | 2 +- plugin/hooks/projection.js | 20 +++++++++ src/space-graph.ts | 26 ++++++++++++ tests/space-graph.test.ts | 69 +++++++++++++++++++++++++++++++ 5 files changed, 120 insertions(+), 1 deletion(-) diff --git a/docs/concepts.md b/docs/concepts.md index 92e691b1..c42e05cc 100644 --- a/docs/concepts.md +++ b/docs/concepts.md @@ -240,6 +240,10 @@ Each non-root level uses the shared `field`, `fieldOn`, and `multiple` edge opti | `matchers` | _undefined_ | Heading patterns (strings or `/regex/`) that signal this level's embedding section | | `embeddedTemplateFields` | _undefined_ | Column names used when `template-sync` generates table stubs for this level | +### Sibling order + +A node with a numeric `order` field is sorted ascending among its siblings in the space graph — across each parent's children as well as top-level roots, orphans, and non-hierarchy nodes. Siblings without a numeric `order` appear after ordered ones. Because sorting happens in `buildSpaceGraph`, every consumer (`show`, diagrams, map views) sees the same sibling order without extra schema configuration. + ### Hierarchy embedding Hierarchy levels with `templateFormat` and `matchers` support **embedded parsing**: a matching heading in a typed page types following list items or table rows without `[type:: x]` annotations (requires `lists: true` or `tables: true` in [`plugins.markdown.embedded`](config.md#embedded)). diff --git a/plugin/.claude-plugin/plugin.json b/plugin/.claude-plugin/plugin.json index 6a10bf79..838a8f0f 100644 --- a/plugin/.claude-plugin/plugin.json +++ b/plugin/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "structured-context", - "version": "0.27.2", + "version": "0.27.3", "description": "Structured content authoring for Obsidian — automatic validation hooks, slash commands, and agent skills to keep markdown files compliant with your schema.", "homepage": "https://github.com/mindsocket/structured-context", "repository": "https://github.com/mindsocket/structured-context", diff --git a/plugin/hooks/projection.js b/plugin/hooks/projection.js index 53d329d5..d1bc5345 100644 --- a/plugin/hooks/projection.js +++ b/plugin/hooks/projection.js @@ -271,6 +271,18 @@ var RELATIONSHIP_SCHEMA = { }; // src/space-graph.ts +function nodeOrder(node) { + const value = node?.schemaData?.order; + return typeof value === "number" && Number.isFinite(value) ? value : void 0; +} +function compareByOrder(a, b) { + const oa = nodeOrder(a); + const ob = nodeOrder(b); + if (oa === void 0 && ob === void 0) return 0; + if (oa === void 0) return 1; + if (ob === void 0) return -1; + return oa - ob; +} function buildSpaceGraph(nodes, hierarchy) { const levels = hierarchy?.levels ?? []; const hierarchyTypes = new Set(levels.map((l) => l.type)); @@ -326,6 +338,14 @@ function buildSpaceGraph(nodes, hierarchy) { } } } + hierarchyRoots.sort(compareByOrder); + orphans.sort(compareByOrder); + nonHierarchy.sort(compareByOrder); + for (const list of hierarchyChildrenMap.values()) list.sort(compareByOrder); + for (const list of childrenMap.values()) list.sort(compareByOrder); + for (const edges of childEdgesMap.values()) { + edges.sort((a, b) => compareByOrder(nodesMap.get(a.child), nodesMap.get(b.child))); + } const hierarchyIds = /* @__PURE__ */ new Set(); function addHierarchySubtree(node) { if (hierarchyIds.has(node.id)) return; diff --git a/src/space-graph.ts b/src/space-graph.ts index e9633e07..53b221f8 100644 --- a/src/space-graph.ts +++ b/src/space-graph.ts @@ -47,6 +47,22 @@ export type SpaceGraph = { readonly hierarchy: Hierarchy | undefined; }; +/** Read a finite numeric `order` field from a node's schemaData, or `undefined` if absent or non-numeric. */ +function nodeOrder(node: SpaceNode | undefined): number | undefined { + const value = (node?.schemaData as Record | undefined)?.order; + return typeof value === 'number' && Number.isFinite(value) ? value : undefined; +} + +/** Sort siblings by numeric `order` ascending; unordered or non-numeric nodes go at the end. */ +function compareByOrder(a: SpaceNode | undefined, b: SpaceNode | undefined): number { + const oa = nodeOrder(a); + const ob = nodeOrder(b); + if (oa === undefined && ob === undefined) return 0; + if (oa === undefined) return 1; + if (ob === undefined) return -1; + return oa - ob; +} + /** Build a SpaceGraph from a flat list of SpaceNodes and the hierarchy to structure it by. */ export function buildSpaceGraph(nodes: SpaceNode[], hierarchy: Hierarchy | undefined): SpaceGraph { const levels = hierarchy?.levels ?? []; @@ -122,6 +138,16 @@ export function buildSpaceGraph(nodes: SpaceNode[], hierarchy: Hierarchy | undef } } + // Sort sibling collections by numeric `order` ascending (unordered/non-numeric at the end) + hierarchyRoots.sort(compareByOrder); + orphans.sort(compareByOrder); + nonHierarchy.sort(compareByOrder); + for (const list of hierarchyChildrenMap.values()) list.sort(compareByOrder); + for (const list of childrenMap.values()) list.sort(compareByOrder); + for (const edges of childEdgesMap.values()) { + edges.sort((a, b) => compareByOrder(nodesMap.get(a.child), nodesMap.get(b.child))); + } + // Build hierarchyIds: BFS from roots + orphans through hierarchyChildren const hierarchyIds = new Set(); diff --git a/tests/space-graph.test.ts b/tests/space-graph.test.ts index 14ffbd2c..3ac3e269 100644 --- a/tests/space-graph.test.ts +++ b/tests/space-graph.test.ts @@ -197,4 +197,73 @@ describe('buildSpaceGraph', () => { expect(graph.hierarchy).toBe(hierarchy); }); }); + + describe('sibling ordering via reserved order field', () => { + const withOrder = (title: string, type: string, order?: unknown) => { + const node = makeNode(title, type); + if (order !== undefined) (node.schemaData as Record).order = order; + return node; + }; + + it('sorts children, childEdges, and roots ascending by numeric order with unordered and non-numeric nodes at the end', () => { + const rootB = withOrder('Goal B', 'goal', 2); + const rootUnordered = withOrder('Goal U', 'goal'); + const rootA = withOrder('Goal A', 'goal', 1); + + const childUnordered = withOrder('Opp Unordered', 'opportunity'); + const childThird = withOrder('Opp 3', 'opportunity', 30); + const childNonNumeric = withOrder('Opp Str', 'opportunity', '1'); + const childFirst = withOrder('Opp 1', 'opportunity', 1); + const childSecond = withOrder('Opp 2', 'opportunity', 2); + for (const c of [childUnordered, childThird, childNonNumeric, childFirst, childSecond]) { + c.resolvedParents = [makeParentRef('Goal A')]; + } + + const graph = buildSpaceGraph( + [rootB, rootUnordered, rootA, childUnordered, childThird, childNonNumeric, childFirst, childSecond], + makeHierarchy(levels), + ); + + expect(graph.hierarchyRoots.map((n) => n.title)).toEqual(['Goal A', 'Goal B', 'Goal U']); + expect(graph.hierarchyChildren.get('Goal A' as NodeId)?.map((n) => n.title)).toEqual([ + 'Opp 1', + 'Opp 2', + 'Opp 3', + 'Opp Unordered', + 'Opp Str', + ]); + expect(graph.children.get('Goal A' as NodeId)?.map((n) => n.title)).toEqual([ + 'Opp 1', + 'Opp 2', + 'Opp 3', + 'Opp Unordered', + 'Opp Str', + ]); + expect(graph.childEdges.get('Goal A' as NodeId)?.map((e) => String(e.child))).toEqual([ + 'Opp 1', + 'Opp 2', + 'Opp 3', + 'Opp Unordered', + 'Opp Str', + ]); + // graph.nodes preserves insertion order + expect([...graph.nodes.keys()].slice(0, 3).map(String)).toEqual(['Goal B', 'Goal U', 'Goal A']); + }); + + it('applies a node’s single order value under each of its parents and preserves tie order', () => { + const goal1 = withOrder('Goal 1', 'goal', 1); + const goal2 = withOrder('Goal 2', 'goal', 2); + const shared = withOrder('Shared', 'opportunity', 1); + const other1 = withOrder('Other 1', 'opportunity', 5); + const other2 = withOrder('Other 2', 'opportunity', 1); + shared.resolvedParents = [makeParentRef('Goal 1'), makeParentRef('Goal 2')]; + other1.resolvedParents = [makeParentRef('Goal 1')]; + other2.resolvedParents = [makeParentRef('Goal 2')]; + + const graph = buildSpaceGraph([goal1, goal2, other1, other2, shared], makeHierarchy(levels)); + + expect(graph.hierarchyChildren.get('Goal 1' as NodeId)?.map((n) => n.title)).toEqual(['Shared', 'Other 1']); + expect(graph.hierarchyChildren.get('Goal 2' as NodeId)?.map((n) => n.title)).toEqual(['Other 2', 'Shared']); + }); + }); });