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
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.0",
"version": "0.27.2",
"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
6 changes: 3 additions & 3 deletions plugin/skills/structured-context/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ When one run reports many issues, follow `references/fix-loop.md`.

## Embedded nodes and Relationships

Embedded nodes are nodes that live physically inside another node's file (via tables or lists) rather than as separate files.
Embedded nodes live inside another node's markdown file (as matched headings, or opt-in lists/tables) rather than as separate files.

**Adjacent Relationships** determine how these are parsed:
- **Heading matching:** When a heading matches a relationship `matcher` (case-insensitive or `/regex/`), following tables/lists are parsed as that child type.
- **Heading matching:** A heading matching a relationship `matcher` (case-insensitive or `/regex/`) becomes an embedded node of that child type owning its section. When `plugins.markdown.embedded` enables `lists` or `tables` for the enclosing type, a following list or table under the matched heading is parsed as child nodes instead.
- **Agnostic parsing:** The parser uses the semantic grandparent as the parent for items matched via a relationship heading.
- **Node content:** an embedded node's `content` is the markdown it owns; the containing page's `content` is the body outside its embedded nodes. Run `sctx docs concepts` (Node content) for the exact rules.
- **`fieldOn` direction:** Relationships support two link directions. `fieldOn: "child"` (default) sets the relationship field on each child node. `fieldOn: "parent"` instead populates the parent node's array field with `[[Child]]` wikilinks — use this when the content model lists children on the parent (e.g. `activity.tasks`). When `fieldOn: "parent"`, child nodes do not get a `parent` field from the relationship.
- **`fieldOn` direction:** Relationships support two link directions. `fieldOn: "child"` (default) writes the parent wikilink into the relationship's `field` on each child node. `fieldOn: "parent"` instead populates the parent node's array field with `[[Child]]` wikilinks — use this when the content model lists children on the parent (e.g. `activity.tasks`). When `fieldOn: "parent"`, child nodes do not get a `parent` field from the relationship.

**When to use sub-entities:**
- For fine-grained items like `Assumption`, `Risk`, or `Requirement` that would clutter the filesystem if separate.
Expand Down
39 changes: 36 additions & 3 deletions src/plugins/markdown/parse-embedded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ function processListItem(
file: string | undefined,
lineOffset: number | undefined,
walk: ListWalk,
resolveParentField: (childType: string) => string = () => 'parent',
): void {
const itemEnd = offsetsOf(item).end;
const firstPara = item.children.find((c) => c.type === 'paragraph') as Paragraph | undefined;
Expand Down Expand Up @@ -370,7 +371,10 @@ function processListItem(
type,
...fields,
};
if (parentRef && !parentFieldAppend) schemaData.parent = parentRef;
const parentField = resolveParentField(type);
if (parentRef && !parentFieldAppend && schemaData[parentField] === undefined) {
schemaData[parentField] = parentRef;
}
if (summary) schemaData.summary = summary;

const line = item.position?.start.line ? item.position.start.line + (lineOffset ?? 0) : undefined;
Expand Down Expand Up @@ -562,6 +566,24 @@ export function extractEmbeddedNodes(body: string, options: ExtractEmbeddedOptio
return undefined;
}

/** Resolve the child-side link field for a `(parentType, childType)` edge (defaults to `'parent'`). */
function resolveChildParentField(
parentType: string | undefined,
childType: string,
matched?: EmbeddingDefinition,
): string {
if (matched && matched.fieldOn !== 'parent') return matched.field;
if (parentType) {
const canonicalParent = typeAliases[parentType] ?? parentType;
const canonicalChild = typeAliases[childType] ?? childType;
const rel = relationships.find((r) => r.parent === canonicalParent && r.type === canonicalChild);
if (rel && rel.fieldOn !== 'parent') return rel.field;
const level = levels.find((l) => l.type === canonicalChild);
if (level && level.fieldOn !== 'parent') return level.field;
}
return 'parent';
}

/** Convert a relationship definition to a normalised EmbeddingDefinition. */
function relationshipToEmbedding(rel: Relationship): EmbeddingDefinition {
return {
Expand Down Expand Up @@ -825,7 +847,8 @@ export function extractEmbeddedNodes(body: string, options: ExtractEmbeddedOptio
type,
...inlineFields,
};
if (parentRef) schemaData.parent = parentRef;
const parentField = resolveChildParentField(parentContextType, type, embeddingMatch);
if (parentRef && schemaData[parentField] === undefined) schemaData[parentField] = parentRef;

const line = heading.position?.start.line ? heading.position.start.line + (options.lineOffset ?? 0) : undefined;
const column = heading.position?.start.column;
Expand Down Expand Up @@ -917,10 +940,12 @@ export function extractEmbeddedNodes(body: string, options: ExtractEmbeddedOptio
options.file,
options.lineOffset,
walk,
(childType) => resolveChildParentField(definition.parent, childType, definition),
);
}
grouping = null;
} else {
const listParentContextType = getParentContextType();
const walk: ListWalk = { claims, pos: spanStart };
for (const item of list.children) {
processListItem(
Expand All @@ -938,6 +963,7 @@ export function extractEmbeddedNodes(body: string, options: ExtractEmbeddedOptio
options.file,
options.lineOffset,
walk,
(childType) => resolveChildParentField(listParentContextType, childType),
);
}
}
Expand Down Expand Up @@ -1017,7 +1043,14 @@ export function extractEmbeddedNodes(body: string, options: ExtractEmbeddedOptio
type: rowTypeStr,
...inlineFields,
};
if (semanticParentRef && !tableParentFieldAppend) schemaData.parent = semanticParentRef;
const tableParentField = resolveChildParentField(
activeMatch?.parent ?? parentContextType,
rowTypeStr,
activeMatch,
);
if (semanticParentRef && !tableParentFieldAppend && schemaData[tableParentField] === undefined) {
schemaData[tableParentField] = semanticParentRef;
}

for (let i = 1; i < columnNames.length; i++) {
const colName = columnNames[i]!;
Expand Down
45 changes: 45 additions & 0 deletions tests/plugins/markdown/parse-embedded-relationships.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,49 @@ Our users are sad.
expect(assumptionsField).toEqual(['[[Assumption One]]', '[[Assumption Two]]']);
});
});

describe('child-side relationships with custom field name', () => {
const meetingSummaryRel: Relationship[] = [
{
parent: 'meeting',
type: 'summary',
field: 'meeting',
fieldOn: 'child',
multiple: false,
templateFormat: 'heading',
matchers: ['Summary'],
},
];

it('writes the parent link to relationship.field for matched headings (including list-first sections with default opt-in)', () => {
const body = `## Notes\nNotes text\n\n## Summary\n- Landed: a\n- Weak: b\n`;
const { nodes } = rawExtractEmbeddedNodes(body, {
page: { id: 'A.md', targets: ['A'] },
pageType: 'meeting',
metadata: { relationships: meetingSummaryRel },
});

expect(nodes).toHaveLength(1);
expect(nodes[0]?.schemaData.type).toBe('summary');
expect(nodes[0]?.schemaData.meeting).toBe('[[A]]');
expect(nodes[0]?.schemaData.parent).toBeUndefined();
expect(nodes[0]?.content).toContain('- Landed: a');
});

it('writes relationship.field for explicit [type:: ...] headings, list items, and table rows', () => {
const body = `## Recap [type:: summary]\nWent fine.\n\n## Summary\n- Item one\n\n## Summary\n\n| summary | note |\n|---|---|\n| Row one | ok |\n`;
const { nodes } = extractEmbeddedNodes(body, {
page: { id: 'A.md', targets: ['A'] },
pageType: 'meeting',
metadata: { relationships: meetingSummaryRel },
});

const summaries = nodes.filter((n) => n.type === 'summary');
expect(summaries.map((n) => n.title)).toEqual(['Recap', 'Item one', 'Row one']);
for (const s of summaries) {
expect(s.schemaData.meeting).toBe('[[A]]');
expect(s.schemaData.parent).toBeUndefined();
}
});
});
});