Skip to content

Commit 964d672

Browse files
committed
fix: normalize capture title for non-markdown targets
1 parent 652737d commit 964d672

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/engine/CaptureChoiceEngine.selection.test.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import type ICaptureChoice from "../types/choices/ICaptureChoice";
55
import type { IChoiceExecutor } from "../IChoiceExecutor";
66
import { isFolder, openFile } from "../utilityObsidian";
77

8-
const { setUseSelectionAsCaptureValueMock } = vi.hoisted(() => ({
8+
const { setUseSelectionAsCaptureValueMock, setTitleMock } = vi.hoisted(() => ({
99
setUseSelectionAsCaptureValueMock: vi.fn(),
10+
setTitleMock: vi.fn(),
1011
}));
1112

1213
vi.mock("../formatters/captureChoiceFormatter", () => ({
@@ -15,7 +16,9 @@ vi.mock("../formatters/captureChoiceFormatter", () => ({
1516
setUseSelectionAsCaptureValue(value: boolean) {
1617
setUseSelectionAsCaptureValueMock(value);
1718
}
18-
setTitle() {}
19+
setTitle(value: string) {
20+
setTitleMock(value);
21+
}
1922
setDestinationFile() {}
2023
setDestinationSourcePath() {}
2124
async formatContentOnly(content: string) {
@@ -130,6 +133,7 @@ const createExecutor = (): IChoiceExecutor => ({
130133
describe("CaptureChoiceEngine selection-as-value resolution", () => {
131134
beforeEach(() => {
132135
setUseSelectionAsCaptureValueMock.mockClear();
136+
setTitleMock.mockClear();
133137
vi.mocked(openFile).mockClear();
134138
});
135139

@@ -214,6 +218,7 @@ describe("CaptureChoiceEngine selection-as-value resolution", () => {
214218
describe("CaptureChoiceEngine capture target resolution", () => {
215219
beforeEach(() => {
216220
vi.mocked(isFolder).mockReset();
221+
setTitleMock.mockClear();
217222
});
218223

219224
it("treats folder path without trailing slash as folder when folder exists", () => {
@@ -292,4 +297,40 @@ describe("CaptureChoiceEngine capture target resolution", () => {
292297

293298
expect(result).toBe("Boards/Map.canvas");
294299
});
300+
301+
it("uses extensionless title for created .base/.canvas capture files", async () => {
302+
const app = createApp() as any;
303+
app.vault.read = vi.fn(async () => "");
304+
305+
const engine = new CaptureChoiceEngine(
306+
app,
307+
{ settings: { useSelectionAsCaptureValue: false } } as any,
308+
createChoice({
309+
createFileIfItDoesntExist: {
310+
enabled: true,
311+
createWithTemplate: false,
312+
template: "",
313+
},
314+
}),
315+
createExecutor(),
316+
);
317+
318+
(engine as any).createFileWithInput = vi.fn(async (path: string) => ({
319+
path,
320+
basename: path.split("/").pop()?.replace(/\.(base|canvas)$/i, "") ?? "",
321+
extension: path.endsWith(".base") ? "base" : "canvas",
322+
}));
323+
324+
await (engine as any).onCreateFileIfItDoesntExist(
325+
"Boards/Kanban.base",
326+
"capture",
327+
);
328+
await (engine as any).onCreateFileIfItDoesntExist(
329+
"Boards/Map.canvas",
330+
"capture",
331+
);
332+
333+
expect(setTitleMock).toHaveBeenCalledWith("Kanban");
334+
expect(setTitleMock).toHaveBeenCalledWith("Map");
335+
});
295336
});

src/engine/CaptureChoiceEngine.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
} from "../utilityObsidian";
3434
import { isCancellationError, reportError } from "../utils/errorUtils";
3535
import { normalizeFileOpening } from "../utils/fileOpeningDefaults";
36+
import { basenameWithoutMdOrCanvas } from "../utils/pathUtils";
3637
import { QuickAddChoiceEngine } from "./QuickAddChoiceEngine";
3738
import { ChoiceAbortError } from "../errors/ChoiceAbortError";
3839
import { MacroAbortError } from "../errors/MacroAbortError";
@@ -469,8 +470,8 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
469470
newFileContent: string;
470471
captureContent: string;
471472
}> {
472-
// Extract filename without extension from the full path
473-
const fileBasename = filePath.split("/").pop()?.replace(/\.md$/, "") || "";
473+
// Extract filename without extension from the full path.
474+
const fileBasename = basenameWithoutMdOrCanvas(filePath);
474475
this.formatter.setTitle(fileBasename);
475476

476477
// Set the destination path so formatters can generate proper relative links

0 commit comments

Comments
 (0)