diff --git a/plugin/.claude-plugin/plugin.json b/plugin/.claude-plugin/plugin.json index d726370..6a10bf7 100644 --- a/plugin/.claude-plugin/plugin.json +++ b/plugin/.claude-plugin/plugin.json @@ -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", diff --git a/plugin/skills/structured-context/SKILL.md b/plugin/skills/structured-context/SKILL.md index 765895e..a734f78 100644 --- a/plugin/skills/structured-context/SKILL.md +++ b/plugin/skills/structured-context/SKILL.md @@ -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. diff --git a/src/plugins/markdown/parse-embedded.ts b/src/plugins/markdown/parse-embedded.ts index c8b4bb4..1eb2cca 100644 --- a/src/plugins/markdown/parse-embedded.ts +++ b/src/plugins/markdown/parse-embedded.ts @@ -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; @@ -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; @@ -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 { @@ -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; @@ -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( @@ -938,6 +963,7 @@ export function extractEmbeddedNodes(body: string, options: ExtractEmbeddedOptio options.file, options.lineOffset, walk, + (childType) => resolveChildParentField(listParentContextType, childType), ); } } @@ -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]!; diff --git a/tests/plugins/markdown/parse-embedded-relationships.test.ts b/tests/plugins/markdown/parse-embedded-relationships.test.ts index 30e483f..b074d87 100644 --- a/tests/plugins/markdown/parse-embedded-relationships.test.ts +++ b/tests/plugins/markdown/parse-embedded-relationships.test.ts @@ -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(); + } + }); + }); });