Skip to content

Commit 56e82f0

Browse files
authored
fix: Use [title](url) format for note titles (#1153)
1 parent 710f7e1 commit 56e82f0

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/components/chat-components/ChatSingleMessage.tsx

+29-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { cn } from "@/lib/utils";
77
import { ChatMessage } from "@/sharedState";
88
import { insertIntoEditor } from "@/utils";
99
import { Bot, User } from "lucide-react";
10-
import { App, Component, MarkdownRenderer } from "obsidian";
10+
import { App, Component, MarkdownRenderer, TFile } from "obsidian";
1111
import React, { useCallback, useEffect, useRef, useState } from "react";
1212

1313
function MessageContext({ context }: { context: ChatMessage["context"] }) {
@@ -81,6 +81,15 @@ const ChatSingleMessage: React.FC<ChatSingleMessageProps> = ({
8181

8282
const preprocess = useCallback(
8383
(content: string): string => {
84+
const activeFile = app.workspace.getActiveFile();
85+
const sourcePath = activeFile ? activeFile.path : "";
86+
87+
const replaceLinks = (text: string, regex: RegExp, template: (file: TFile) => string) =>
88+
text.replace(regex, (match: string, selection: string) => {
89+
const file = app.metadataCache.getFirstLinkpathDest(selection, sourcePath);
90+
return file ? template(file) : match;
91+
});
92+
8493
// Process LaTeX
8594
const latexProcessed = content
8695
.replace(/\\\[\s*/g, "$$")
@@ -89,22 +98,28 @@ const ChatSingleMessage: React.FC<ChatSingleMessageProps> = ({
8998
.replace(/\s*\\\)/g, "$");
9099

91100
// Process only Obsidian internal images (starting with ![[)
92-
const activeFile = app.workspace.getActiveFile();
93-
const sourcePath = activeFile ? activeFile.path : "";
101+
const noteImageProcessed = replaceLinks(
102+
latexProcessed,
103+
/!\[\[(.*?)]]/g,
104+
(file) => `![](${app.vault.getResourcePath(file)})`
105+
);
94106

95-
const noteImageProcessed = latexProcessed.replace(
96-
/!\[\[(.*?)\]\]/g,
97-
(match: string, imageName: string) => {
98-
const imageFile = app.metadataCache.getFirstLinkpathDest(imageName, sourcePath);
99-
if (imageFile) {
100-
const imageUrl = app.vault.getResourcePath(imageFile);
101-
return `![](${imageUrl})`;
102-
}
103-
return match;
104-
}
107+
const clickable = (file: TFile) =>
108+
`[${file.name}](obsidian://open?vault=${encodeURIComponent(app.vault.getName())}&file=${encodeURIComponent(file.path)})`;
109+
// Transform [[link]] format but exclude ![[]] image links
110+
const noteLinksProcessed = replaceLinks(
111+
noteImageProcessed,
112+
/(?<!!)\[\[([^\]]+)]]/g,
113+
clickable
114+
);
115+
// Transform [title](app://obsidian.md/note) format but exclude ![[]] image links
116+
const appNoteLinks = replaceLinks(
117+
noteLinksProcessed,
118+
/(?<!!)\[([^\]]+)]\(app:\/\/obsidian\.md\/([^)]+)\)/g,
119+
clickable
105120
);
106121

107-
return noteImageProcessed;
122+
return appNoteLinks;
108123
},
109124
[app]
110125
);

0 commit comments

Comments
 (0)