Skip to content

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

fix(markdown): support non-numeric citation IDs in reference markers (#19846)

chanx3fe110b52edd
diff --git a/web/src/components/floating-chat-widget-markdown.tsx b/web/src/components/floating-chat-widget-markdown.tsx
index 6f20c1d590..0a46830b49 100644
--- a/web/src/components/floating-chat-widget-markdown.tsx
+++ b/web/src/components/floating-chat-widget-markdown.tsx
@@ -268,7 +268,7 @@ const FloatingChatWidgetMarkdown = ({
const renderReference = useCallback(
(text: string) => {
return reactStringReplace(text, currentReg, (match, i) => {
- const chunkIndex = getChunkIndex(match);
+ const chunkIndex = getChunkIndex(match) as number;
const info = getReferenceInfo(chunkIndex);
if (!info) {
diff --git a/web/src/components/markdown-content/index.tsx b/web/src/components/markdown-content/index.tsx
index bd23acce4a..a16266c777 100644
--- a/web/src/components/markdown-content/index.tsx
+++ b/web/src/components/markdown-content/index.tsx
@@ -325,7 +325,7 @@ const MarkdownContent = ({
const renderReference = useCallback(
(text: string) => {
const replacedText = reactStringReplace(text, currentReg, (match, i) => {
- const chunkIndex = getChunkIndex(match);
+ const chunkIndex = getChunkIndex(match) as number;
return (
<HoverCard key={i}>
diff --git a/web/src/components/next-markdown-content/index.tsx b/web/src/components/next-markdown-content/index.tsx
index a165c245da..ff92dab51f 100644
--- a/web/src/components/next-markdown-content/index.tsx
+++ b/web/src/components/next-markdown-content/index.tsx
@@ -49,7 +49,6 @@ import {
replaceThinkToSection,
unescapeAngleBrackets,
} from '@/utils/chat';
-import { citationMarkerReg } from '@/utils/citation-utils';
import { getDirAttribute } from '@/utils/text-direction';
import { useFetchDocumentThumbnailsByIds } from '@/hooks/use-document-request';
@@ -69,7 +68,7 @@ import {
import message from '../ui/message';
import styles from './index.module.less';
-const getChunkIndex = (match: string) => parseCitationIndex(match);
+const getChunkKey = (match: string) => parseCitationIndex(match);
const isArtifactUrl = (url?: string) =>
Boolean(url && url.includes('/api/v1/documents/artifact/'));
@@ -273,7 +272,10 @@ function MarkdownContent({
const getReferenceInfo = useCallback(
(chunkIndex: number) => {
const chunks = reference?.chunks ?? {};
- const chunkItem = chunks[chunkIndex];
+ // Agent reference.chunks is a Record keyed by the original citation ID
+ // (e.g. "3092e7ae6831b877"); the caller maps those IDs to a positional
+ // display index, so look up the N-th entry by insertion order.
+ const chunkItem = Object.values(chunks)[chunkIndex];
const documentList = Object.values(reference?.doc_aggs ?? {});
const document = documentList.find(
@@ -373,18 +375,28 @@ function MarkdownContent({
const renderReference = useCallback(
(text: string) => {
- const replacedText = reactStringReplace(text, currentReg, (match, i) => {
- const chunkIndex = getChunkIndex(match);
+ // Assign each unique citation marker a stable display number based on
+ // its first occurrence, so the same ID always renders as the same [N]
+ // chip. The actual chunk lookup uses that index against the positional
+ // order of IReferenceObject.chunks (see getReferenceInfo).
+ const keyToDisplay = new Map<number | string, number>();
+ const replacedText = reactStringReplace(text, currentReg, (match) => {
+ const chunkKey = getChunkKey(match);
+ let displayIndex = keyToDisplay.get(chunkKey);
+ if (displayIndex === undefined) {
+ displayIndex = keyToDisplay.size;
+ keyToDisplay.set(chunkKey, displayIndex);
+ }
return (
- <HoverCard key={i}>
+ <HoverCard key={`${String(chunkKey)}-${displayIndex}`}>
<HoverCardTrigger>
<bdi className="text-text-secondary bg-bg-card rounded-2xl px-1 mx-1 text-nowrap inline-block">
- [{chunkIndex + 1}]
+ [{displayIndex + 1}]
</bdi>
</HoverCardTrigger>
<HoverCardContent className="max-w-3xl">
- {renderPopoverContent(chunkIndex)}
+ {renderPopoverContent(displayIndex)}
</HoverCardContent>
</HoverCard>
);
@@ -395,7 +407,7 @@ function MarkdownContent({
[renderPopoverContent, t],
);
- const dir = getDirAttribute(content?.replace(citationMarkerReg, ''));
+ const dir = getDirAttribute(content?.replace(currentReg, ''));
const showLoadingDots = useLoadingPause(loading, content);
return (
diff --git a/web/src/pages/next-search/markdown-content/index.tsx b/web/src/pages/next-search/markdown-content/index.tsx
index 3f9369f940..8fe91119c4 100644
--- a/web/src/pages/next-search/markdown-content/index.tsx
+++ b/web/src/pages/next-search/markdown-content/index.tsx
@@ -248,7 +248,7 @@ const MarkdownContent = ({
const renderReference = useCallback(
(text: string) => {
const replacedText = reactStringReplace(text, currentReg, (match) => {
- const chunkIndex = getChunkIndex(match);
+ const chunkIndex = getChunkIndex(match) as number;
return (
<Popover>
diff --git a/web/src/utils/citation-utils.ts b/web/src/utils/citation-utils.ts
index 96d395ff81..95e5a58f01 100644
--- a/web/src/utils/citation-utils.ts
+++ b/web/src/utils/citation-utils.ts
@@ -28,13 +28,21 @@ export const normalizeCitationDigits = (text: string) => {
});
};
-export const parseCitationIndex = (value: string) => {
+export type CitationKey = number | string;
+
+export const parseCitationIndex = (value: string): CitationKey => {
const normalized = normalizeCitationDigits(value);
- const markerMatch = normalized.match(/\[(?:ID:)?(\d+)\]/);
- if (markerMatch) return Number(markerMatch[1]);
+ const markerMatch = normalized.match(
+ /\[(?:ID:)?([0-9a-fA-F\u0660-\u0669\u06F0-\u06F9]+)\]/,
+ );
+ if (markerMatch) {
+ const captured = markerMatch[1];
+ return /^\d+$/.test(captured) ? Number(captured) : captured;
+ }
if (/^\d+$/.test(normalized)) return Number(normalized);
+ if (/^[0-9a-fA-F]+$/.test(normalized)) return normalized;
return Number.NaN;
};
export const citationMarkerReg =
- /\[(?:ID:)?([0-9\u0660-\u0669\u06F0-\u06F9]+)\]/g;
+ /\[(?:ID:)?([0-9a-fA-F\u0660-\u0669\u06F0-\u06F9]+)\]/g;