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
50 changes: 50 additions & 0 deletions src/formatters/capture-insert-after-linkcurrent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
});
});
32 changes: 25 additions & 7 deletions src/formatters/captureChoiceFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
}

Expand Down