Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Refactor: make all url inside tooltip as link (#18943)

Wang Qi29af552aefb6
diff --git a/web/src/components/linkify-text.tsx b/web/src/components/linkify-text.tsx
index ebfd8febae..d05e249715 100644
--- a/web/src/components/linkify-text.tsx
+++ b/web/src/components/linkify-text.tsx
@@ -14,20 +14,19 @@
* limitations under the License.
*/
-import type { ReactNode } from 'react';
+import { Children, cloneElement, Fragment, isValidElement } from 'react';
+import type { ReactElement, ReactNode } from 'react';
const URL_REGEX = /(https?:\/\/[^\s<>)"{}|^`[\]]+)/gi;
+const TRAILING_PUNCTUATION_REGEX = /[.,!?;:'。,!?;:)】》」』’”]+$/u;
+const LINK_CLASS_NAME = 'text-buttonBlueText underline hover:opacity-80';
interface LinkifyTextProps {
children: ReactNode;
className?: string;
}
-export function LinkifyText({ children, className }: LinkifyTextProps) {
- if (typeof children !== 'string') {
- return <span className={className}>{children}</span>;
- }
-
+function linkifyString(children: string) {
const parts: ReactNode[] = [];
let lastIndex = 0;
let match;
@@ -41,21 +40,27 @@ export function LinkifyText({ children, className }: LinkifyTextProps) {
);
}
- const url = match[0];
+ const matchedUrl = match[0];
+ const url = matchedUrl.replace(TRAILING_PUNCTUATION_REGEX, '');
parts.push(
<a
key={`link-${match.index}`}
href={url}
target="_blank"
rel="noopener noreferrer"
- className="text-buttonBlueText underline hover:opacity-80"
+ className={LINK_CLASS_NAME}
onClick={(e) => e.stopPropagation()}
>
{url}
</a>,
);
- lastIndex = match.index + url.length;
+ const trailingPunctuation = matchedUrl.slice(url.length);
+ if (trailingPunctuation) {
+ parts.push(trailingPunctuation);
+ }
+
+ lastIndex = match.index + matchedUrl.length;
}
if (lastIndex < children.length) {
@@ -64,5 +69,72 @@ export function LinkifyText({ children, className }: LinkifyTextProps) {
);
}
- return <span className={className}>{parts}</span>;
+ return parts;
+}
+
+function linkifyHtml(html: string) {
+ let isInsideLink = false;
+
+ return html
+ .split(/(<[^>]+>)/g)
+ .map((part) => {
+ if (part.startsWith('<')) {
+ if (/^<a(?:\s|>)/i.test(part)) isInsideLink = true;
+ if (/^<\/a\s*>/i.test(part)) isInsideLink = false;
+ return part;
+ }
+
+ if (isInsideLink) return part;
+
+ return part.replace(URL_REGEX, (matchedUrl) => {
+ const url = matchedUrl.replace(TRAILING_PUNCTUATION_REGEX, '');
+ const trailingPunctuation = matchedUrl.slice(url.length);
+ return `<a href="${url}" target="_blank" rel="noopener noreferrer" class="${LINK_CLASS_NAME}">${url}</a>${trailingPunctuation}`;
+ });
+ })
+ .join('');
+}
+
+function linkifyNode(node: ReactNode): ReactNode {
+ if (typeof node === 'string') {
+ return linkifyString(node);
+ }
+
+ if (
+ !isValidElement(node) ||
+ (typeof node.type === 'string' && node.type === 'a')
+ ) {
+ return node;
+ }
+
+ const element = node as ReactElement<{
+ children?: ReactNode;
+ dangerouslySetInnerHTML?: { __html: string };
+ }>;
+ if (element.props.dangerouslySetInnerHTML) {
+ return cloneElement(element, {
+ dangerouslySetInnerHTML: {
+ __html: linkifyHtml(element.props.dangerouslySetInnerHTML.__html),
+ },
+ });
+ }
+
+ if (element.props.children === undefined) {
+ return node;
+ }
+
+ return cloneElement(
+ element,
+ undefined,
+ Children.map(element.props.children, linkifyNode),
+ );
+}
+
+export function LinkifyText({ children, className }: LinkifyTextProps) {
+ const content = Children.map(children, linkifyNode);
+ return className ? (
+ <span className={className}>{content}</span>
+ ) : (
+ <Fragment>{content}</Fragment>
+ );
}
diff --git a/web/src/components/ui/tooltip.tsx b/web/src/components/ui/tooltip.tsx
index 13505a4f29..b4383839ba 100644
--- a/web/src/components/ui/tooltip.tsx
+++ b/web/src/components/ui/tooltip.tsx
@@ -5,6 +5,7 @@ import * as React from 'react';
import { cn } from '@/lib/utils';
import { CircleQuestionMark } from 'lucide-react';
+import { LinkifyText } from '../linkify-text';
const TooltipProvider = TooltipPrimitive.Provider;
@@ -15,7 +16,7 @@ const TooltipTrigger = TooltipPrimitive.Trigger;
const TooltipContent = React.forwardRef<
React.ElementRef<typeof TooltipPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>
->(({ className, sideOffset = 4, ...props }, ref) => (
+>(({ className, sideOffset = 4, children, ...props }, ref) => (
<TooltipPrimitive.Portal>
<TooltipPrimitive.Content
ref={ref}
@@ -25,7 +26,9 @@ const TooltipContent = React.forwardRef<
className,
)}
{...props}
- />
+ >
+ <LinkifyText>{children}</LinkifyText>
+ </TooltipPrimitive.Content>
</TooltipPrimitive.Portal>
));
TooltipContent.displayName = TooltipPrimitive.Content.displayName;
@@ -132,7 +135,7 @@ export const AntToolTip: React.FC<AntToolTipProps> = ({
className,
)}
>
- {title}
+ <LinkifyText>{title}</LinkifyText>
<div
className={cn(
'absolute w-2 h-2 bg-muted ',
diff --git a/web/src/pages/user-setting/setting-model/layout/system-setting.tsx b/web/src/pages/user-setting/setting-model/layout/system-setting.tsx
index b792cdf3ec..8ee467883c 100644
--- a/web/src/pages/user-setting/setting-model/layout/system-setting.tsx
+++ b/web/src/pages/user-setting/setting-model/layout/system-setting.tsx
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-import { LinkifyText } from '@/components/linkify-text';
import { ModelTreeSelect, ModelTypeMap } from '@/components/model-tree-select';
import {
Tooltip,
@@ -57,9 +56,7 @@ function ModelFieldItem({
{label}
{tooltip && (
<Tooltip>
- <TooltipContent>
- <LinkifyText>{tooltip}</LinkifyText>
- </TooltipContent>
+ <TooltipContent>{tooltip}</TooltipContent>
<TooltipTrigger>
<CircleQuestionMark
size={12}