Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
2 changes: 1 addition & 1 deletion plugin/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 20 additions & 0 deletions plugin/hooks/projection.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions src/space-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> | 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 ?? [];
Expand Down Expand Up @@ -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<NodeId>();

Expand Down
69 changes: 69 additions & 0 deletions tests/space-graph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>).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']);
});
});
});