diff --git a/src/formatters/capture-insert-after-linkcurrent.test.ts b/src/formatters/capture-insert-after-linkcurrent.test.ts index 67e7b12f..d35ec5e2 100644 --- a/src/formatters/capture-insert-after-linkcurrent.test.ts +++ b/src/formatters/capture-insert-after-linkcurrent.test.ts @@ -39,6 +39,14 @@ function findInsertAfterIndex(lines: string[], rawTarget: string): number { const suffix = line.slice(target.length); if (/^\s*$/.test(suffix)) return i; if (partialIndex === -1) partialIndex = i; + continue; + } + + const targetIndex = line.indexOf(target); + if (targetIndex !== -1) { + const suffix = line.slice(targetIndex + target.length); + if (/^\s*$/.test(suffix)) return i; + if (partialIndex === -1) partialIndex = i; } } @@ -149,3 +157,45 @@ describe('Insert after — {{title}} resolution', () => { expect(newContent.includes('{{title}}')).toBe(false); }); }); + +describe('Insert after — table separator matching', () => { + it('matches table separator row when target appears at end of line', () => { + const fileContent = [ + '| Name | Quantity | HH:MM |', + '| ------------- | -------- | ----- |', + '| data | 5 | 10:00 |', + ].join('\n'); + + const target = '| ----- |'; + const lines = fileContent.split('\n'); + const idx = findInsertAfterIndex(lines, target); + + // Should match line 1 (index 1) where "| ----- |" appears at the end + expect(idx).toBe(1); + + const newContent = insertTextAfterPositionInBody('| new | row |\n', fileContent, idx); + const expected = [ + '| Name | Quantity | HH:MM |', + '| ------------- | -------- | ----- |', + '| new | row |', + '| data | 5 | 10:00 |', + ].join('\n'); + + expect(newContent).toBe(expected); + }); + + it('matches exact table separator row', () => { + const fileContent = [ + '| Name | Quantity |', + '| ----- |', + '| data | 5 |', + ].join('\n'); + + const target = '| ----- |'; + const lines = fileContent.split('\n'); + const idx = findInsertAfterIndex(lines, target); + + // Should match line 1 (index 1) with exact match + expect(idx).toBe(1); + }); +}); diff --git a/src/formatters/captureChoiceFormatter.ts b/src/formatters/captureChoiceFormatter.ts index 3018d092..cf213813 100644 --- a/src/formatters/captureChoiceFormatter.ts +++ b/src/formatters/captureChoiceFormatter.ts @@ -19,11 +19,11 @@ export class CaptureChoiceFormatter extends CompleteFormatter { private fileContent = ""; private sourcePath: string | null = null; /** - * Tracks whether the current formatter instance has already run Templater on the - * capture payload. This prevents the same content from being parsed twice in - * multi-stage formatting flows (see issue #533 – double execution when using - * tp.system.prompt). - */ + * Tracks whether the current formatter instance has already run Templater on the + * capture payload. This prevents the same content from being parsed twice in + * multi-stage formatting flows (see issue #533 – double execution when using + * tp.system.prompt). + */ private templaterProcessed = false; public setDestinationFile(file: TFile): void { @@ -163,16 +163,34 @@ export class CaptureChoiceFormatter extends CompleteFormatter { // 1. Exact match wins immediately if (line === target) return i; - // 2. Check for regex-compatible match (target + only whitespace suffix) + // 2. Check for prefix match (target + only whitespace suffix) + // This matches old regex behavior for lines starting with the target if (line.startsWith(target)) { const suffix = line.slice(target.length); // If suffix is only whitespace, this matches old regex behavior exactly if (/^\s*$/.test(suffix)) return i; - + // Remember first broader prefix match as fallback if (partialIndex === -1) { partialIndex = i; } + + continue; + } + + // 3. Check for substring match (target appears anywhere in line with only whitespace after) + // This restores legacy behavior where selectors like "| ----- |" can match + // table separator rows where the target appears at the end + const targetIndex = line.indexOf(target); + if (targetIndex !== -1) { + const suffix = line.slice(targetIndex + target.length); + // If suffix is only whitespace, match this line + if (/^\s*$/.test(suffix)) return i; + + // Remember first broader substring match as fallback + if (partialIndex === -1) { + partialIndex = i; + } } }