Skip to content

Commit

Permalink
Fix: make sure source link is configured and open in external browser
Browse files Browse the repository at this point in the history
Signed-off-by: Daishan Peng <[email protected]>
  • Loading branch information
StrongMonkey committed Sep 11, 2024
1 parent ceefbf8 commit 1b5669e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions components/chat/messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Avatar, Button, Tooltip } from '@nextui-org/react';
import type { CallFrame } from '@gptscript-ai/gptscript';
import Calls from './messages/calls';
import { GoIssueReopened } from 'react-icons/go';
import { defaultUrlTransform } from 'react-markdown';

export enum MessageType {
Alert,
Expand All @@ -30,6 +31,14 @@ const abbreviate = (name: string) => {
return firstLetters.slice(0, 2).join('').toUpperCase();
};

// Allow links for file references in messages if it starts with file://, otherwise this will cause an empty href and cause app to reload when clicking on it
const urlTransformAllowFiles = (u: string) => {
if (u.startsWith('file://')) {
return u;
}
return defaultUrlTransform(u);
};

const Message = React.memo(
({
message,
Expand Down Expand Up @@ -88,6 +97,7 @@ const Message = React.memo(
rehypePlugins={[
[rehypeExternalLinks, { target: '_blank' }],
]}
urlTransform={urlTransformAllowFiles}
>
{message.message}
</Markdown>
Expand Down Expand Up @@ -156,6 +166,7 @@ const Message = React.memo(
rehypePlugins={[
[rehypeExternalLinks, { target: '_blank' }],
]}
urlTransform={urlTransformAllowFiles}
>
{message.message}
</Markdown>
Expand Down
7 changes: 7 additions & 0 deletions electron/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ function createWindow(url) {
// Allow navigation for internal URLs
return { action: 'allow' };
});

win.webContents.on('will-navigate', (event, url) => {
if (url.startsWith('file://')) {
event.preventDefault();
shell.openExternal(url);
}
});
}

function ensureDirExists(dir) {
Expand Down

0 comments on commit 1b5669e

Please sign in to comment.