Skip to content

An AI-powered search engine with a generative UI

feat: replace citation numbers with domain names

Yoshiki Miuraac0df0a30ff8
diff --git a/components/citation-link.tsx b/components/citation-link.tsx
index 88a1105b..34b502a3 100644
--- a/components/citation-link.tsx
+++ b/components/citation-link.tsx
@@ -37,11 +37,12 @@ export const CitationLink = memo(function CitationLink({
}: CitationLinkProps) {
const [open, setOpen] = useState(false)
const childrenText = children?.toString() || ''
- const isNumber = /^\d+$/.test(childrenText)
+ // Match domain names (alphanumeric and hyphens) or numbers for backward compatibility
+ const isCitation = /^[\w-]+$/.test(childrenText)
const linkClasses = cn(
- isNumber
- ? 'text-[10px] bg-muted text-muted-foreground rounded-full w-4 h-4 px-0.5 inline-flex items-center justify-center hover:bg-muted/50 duration-200 no-underline -translate-y-0.5'
+ isCitation
+ ? 'text-[10px] bg-muted/50 text-muted-foreground/60 rounded-full h-4 px-1.5 inline-flex items-center justify-center hover:bg-primary hover:text-primary-foreground duration-200 no-underline -translate-y-0.5 whitespace-nowrap'
: 'hover:underline inline-flex items-center gap-1.5',
className
)
@@ -60,8 +61,8 @@ export const CitationLink = memo(function CitationLink({
)
}
- // For numbered citations with data, show popover on hover
- if (isNumber && citationData) {
+ // For citations with data, show popover on hover
+ if (isCitation && citationData) {
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
diff --git a/components/custom-link.tsx b/components/custom-link.tsx
index 936a3a0d..f6890e79 100644
--- a/components/custom-link.tsx
+++ b/components/custom-link.tsx
@@ -19,26 +19,27 @@ export function Citing({
}: CustomLinkProps) {
const { citationMaps } = useCitation()
const childrenText = children?.toString() || ''
- const isNumber = /^\d+$/.test(childrenText)
+ // Match domain names (alphanumeric and hyphens) or numbers for backward compatibility
+ const isCitation = /^[\w-]+$/.test(childrenText)
- // Get citation data if this is a numbered citation
+ // Get citation data if this is a citation
let citationData: SearchResultItem | undefined = undefined
- if (isNumber && citationMaps && href) {
- const citationNumber = parseInt(childrenText)
+ if (isCitation && citationMaps && href) {
+ const decodedHref = decodeURI(href)
// Try to find the citation data by checking all citation maps
- // This happens when the URL has already been processed
+ // Match by URL instead of citation number/text
for (const toolCallId in citationMaps) {
const citationMap = citationMaps[toolCallId]
- if (citationMap[citationNumber]) {
- // Check if this citation's URL matches the href
- const decodedHref = decodeURI(href)
- if (citationMap[citationNumber].url === decodedHref) {
- citationData = citationMap[citationNumber]
+ // Search through all citations in this map
+ for (const citationNum in citationMap) {
+ if (citationMap[citationNum].url === decodedHref) {
+ citationData = citationMap[citationNum]
break
}
}
+ if (citationData) break
}
}
diff --git a/components/search-results.tsx b/components/search-results.tsx
index 074cf5e5..89a11ffb 100644
--- a/components/search-results.tsx
+++ b/components/search-results.tsx
@@ -4,6 +4,7 @@ import { useState } from 'react'
import Link from 'next/link'
import { SearchResultItem } from '@/lib/types'
+import { displayUrlName } from '@/lib/utils/domain'
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'
import { Button } from '@/components/ui/button'
@@ -28,11 +29,6 @@ export function SearchResults({
// Logic for grid mode
const displayedGridResults = showAllResults ? results : results.slice(0, 3)
const additionalResultsCount = results.length > 3 ? results.length - 3 : 0
- const displayUrlName = (url: string) => {
- const hostname = new URL(url).hostname
- const parts = hostname.split('.')
- return parts.length > 2 ? parts.slice(1, -1).join('.') : parts[0]
- }
// --- List Mode Rendering ---
if (displayMode === 'list') {
@@ -69,8 +65,7 @@ export function SearchResults({
<div className="text-xs text-muted-foreground/80 mt-1 truncate">
<span className="underline">
{new URL(result.url).hostname}
- </span>{' '}
- - {index + 1}
+ </span>
</div>
</div>
</CardContent>
@@ -105,7 +100,7 @@ export function SearchResults({
</AvatarFallback>
</Avatar>
<div className="text-xs opacity-60 truncate min-w-0">
- {`${displayUrlName(result.url)} - ${index + 1}`}
+ {displayUrlName(result.url)}
</div>
</div>
</CardContent>
diff --git a/lib/utils/__tests__/citation.test.ts b/lib/utils/__tests__/citation.test.ts
new file mode 100644
index 00000000..4d2c8299
--- /dev/null
+++ b/lib/utils/__tests__/citation.test.ts
@@ -0,0 +1,157 @@
+import { describe, expect, it } from 'vitest'
+
+import type { SearchResultItem } from '@/lib/types'
+
+import { processCitations } from '../citation'
+
+describe('processCitations', () => {
+ const mockCitationMaps = {
+ toolCall1: {
+ 1: {
+ title: 'Google',
+ url: 'https://www.google.com',
+ content: 'Search engine'
+ },
+ 2: {
+ title: 'GitHub',
+ url: 'https://docs.github.com',
+ content: 'Developer platform'
+ },
+ 3: {
+ title: 'Stack Overflow',
+ url: 'https://stackoverflow.com/questions/123',
+ content: 'Q&A for developers'
+ }
+ } as Record<number, SearchResultItem>
+ }
+
+ it('converts numbered citations to domain names', () => {
+ const content = 'Check out [1](#toolCall1) and [2](#toolCall1)'
+ const result = processCitations(content, mockCitationMaps)
+
+ expect(result).toBe(
+ 'Check out [google](https://www.google.com) and [github](https://docs.github.com)'
+ )
+ })
+
+ it('handles citations with spaces', () => {
+ const content = 'See [ 1 ](#toolCall1) for details'
+ const result = processCitations(content, mockCitationMaps)
+
+ expect(result).toBe('See [google](https://www.google.com) for details')
+ })
+
+ it('handles multiple citations from same domain', () => {
+ const citationMaps = {
+ toolCall1: {
+ 1: {
+ title: 'Google Search',
+ url: 'https://www.google.com/search',
+ content: 'Search'
+ },
+ 2: {
+ title: 'Google Maps',
+ url: 'https://www.google.com/maps',
+ content: 'Maps'
+ }
+ } as Record<number, SearchResultItem>
+ }
+
+ const content = 'Try [1](#toolCall1) or [2](#toolCall1)'
+ const result = processCitations(content, citationMaps)
+
+ expect(result).toBe(
+ 'Try [google](https://www.google.com/search) or [google](https://www.google.com/maps)'
+ )
+ })
+
+ it('returns empty string for invalid citation numbers', () => {
+ const content = 'Invalid [999](#toolCall1) citation'
+ const result = processCitations(content, mockCitationMaps)
+
+ expect(result).toBe('Invalid citation')
+ })
+
+ it('returns empty string for missing toolCallId', () => {
+ const content = 'Missing [1](#nonExistentTool) tool'
+ const result = processCitations(content, mockCitationMaps)
+
+ expect(result).toBe('Missing tool')
+ })
+
+ it('returns empty string for invalid URLs', () => {
+ const citationMaps = {
+ toolCall1: {
+ 1: {
+ title: 'Invalid',
+ url: 'not-a-valid-url',
+ content: 'Invalid URL'
+ }
+ } as Record<number, SearchResultItem>
+ }
+
+ const content = 'Check [1](#toolCall1) here'
+ const result = processCitations(content, citationMaps)
+
+ expect(result).toBe('Check here')
+ })
+
+ it('handles content with no citations', () => {
+ const content = 'This is plain text without citations'
+ const result = processCitations(content, mockCitationMaps)
+
+ expect(result).toBe('This is plain text without citations')
+ })
+
+ it('returns empty string for null/undefined content', () => {
+ expect(processCitations('', mockCitationMaps)).toBe('')
+ expect(processCitations(null as any, mockCitationMaps)).toBe('')
+ })
+
+ it('handles empty citation maps', () => {
+ const content = 'Text with [1](#toolCall1) citation'
+ const result = processCitations(content, {})
+
+ // When citation maps are empty, content is returned unchanged
+ expect(result).toBe('Text with [1](#toolCall1) citation')
+ })
+
+ it('encodes URLs to prevent injection', () => {
+ const citationMaps = {
+ toolCall1: {
+ 1: {
+ title: 'Test',
+ url: 'https://example.com/page?param=value&other=test',
+ content: 'Test'
+ }
+ } as Record<number, SearchResultItem>
+ }
+
+ const content = 'See [1](#toolCall1)'
+ const result = processCitations(content, citationMaps)
+
+ expect(result).toContain('example')
+ expect(result).toContain('https://example.com/page?param=value&other=test')
+ })
+
+ it('handles complex real-world scenarios', () => {
+ const content = `According to [1](#toolCall1), the answer is 42.
+ However, [2](#toolCall1) suggests otherwise.
+ For more information, see [3](#toolCall1).`
+
+ const result = processCitations(content, mockCitationMaps)
+
+ expect(result).toContain('[google](https://www.google.com)')
+ expect(result).toContain('[github](https://docs.github.com)')
+ expect(result).toContain('[stackoverflow](https://stackoverflow.com/questions/123)')
+ })
+
+ it('handles citation numbers at edge cases', () => {
+ const content = 'Edge cases: [0](#toolCall1) [101](#toolCall1) [-1](#toolCall1)'
+ const result = processCitations(content, mockCitationMaps)
+
+ // 0 and 101 are out of bounds (1-100), so they're replaced with empty string
+ // -1 doesn't match the regex pattern \d+, so it remains unchanged
+ expect(result).toBe('Edge cases: [-1](#toolCall1)')
+ })
+})
diff --git a/lib/utils/__tests__/domain.test.ts b/lib/utils/__tests__/domain.test.ts
new file mode 100644
index 00000000..0599453d
--- /dev/null
+++ b/lib/utils/__tests__/domain.test.ts
@@ -0,0 +1,69 @@
+import { describe, expect, it } from 'vitest'
+
+import { displayUrlName } from '../domain'
+
+describe('displayUrlName', () => {
+ it('extracts domain name from standard URL', () => {
+ expect(displayUrlName('https://www.google.com')).toBe('google')
+ expect(displayUrlName('https://www.example.org')).toBe('example')
+ expect(displayUrlName('https://www.github.io')).toBe('github')
+ })
+
+ it('handles subdomains correctly', () => {
+ expect(displayUrlName('https://docs.github.com')).toBe('github')
+ expect(displayUrlName('https://api.example.org')).toBe('example')
+ expect(displayUrlName('https://en.wikipedia.org')).toBe('wikipedia')
+ })
+
+ it('handles URLs without subdomain', () => {
+ expect(displayUrlName('https://example.com')).toBe('example')
+ expect(displayUrlName('https://github.io')).toBe('github')
+ })
+
+ it('handles localhost and simple hostnames', () => {
+ expect(displayUrlName('http://localhost')).toBe('localhost')
+ expect(displayUrlName('http://localhost:3000')).toBe('localhost')
+ })
+
+ it('handles complex subdomains', () => {
+ expect(displayUrlName('https://sub.domain.example.com')).toBe(
+ 'domain.example'
+ )
+ })
+
+ it('returns fallback for invalid URLs', () => {
+ expect(displayUrlName('not-a-url')).toBe('source')
+ expect(displayUrlName('')).toBe('source')
+ expect(displayUrlName('://invalid')).toBe('source')
+ })
+
+ it('handles URLs with paths and query parameters', () => {
+ expect(displayUrlName('https://www.google.com/search?q=test')).toBe(
+ 'google'
+ )
+ expect(displayUrlName('https://docs.github.com/en/get-started')).toBe(
+ 'github'
+ )
+ })
+
+ it('handles different protocols', () => {
+ expect(displayUrlName('http://www.example.com')).toBe('example')
+ expect(displayUrlName('https://www.example.com')).toBe('example')
+ expect(displayUrlName('ftp://ftp.example.com')).toBe('example')
+ })
+
+ it('handles real-world news domains', () => {
+ expect(displayUrlName('https://www.bbc.com/news')).toBe('bbc')
+ expect(displayUrlName('https://www.cnn.com/world')).toBe('cnn')
+ expect(displayUrlName('https://techcrunch.com/article')).toBe('techcrunch')
+ })
+
+ it('handles stack overflow and similar domains', () => {
+ expect(displayUrlName('https://stackoverflow.com/questions/123')).toBe(
+ 'stackoverflow'
+ )
+ expect(displayUrlName('https://meta.stackoverflow.com')).toBe(
+ 'stackoverflow'
+ )
+ })
+})
diff --git a/lib/utils/citation.ts b/lib/utils/citation.ts
index 052bc354..f46529b0 100644
--- a/lib/utils/citation.ts
+++ b/lib/utils/citation.ts
@@ -1,5 +1,6 @@
import type { SearchResultItem, SearchResults } from '@/lib/types'
import type { UIMessage } from '@/lib/types/ai'
+import { displayUrlName } from '@/lib/utils/domain'
/**
* Validate if a string is a valid URL
@@ -65,8 +66,8 @@ export function extractCitationMapsFromMessages(
}
/**
- * Process citations in content, replacing [number](#toolCallId) with proper URLs
- * while keeping the display as just [number]
+ * Process citations in content, replacing [number](#toolCallId) with [domain](url)
+ * Display text uses domain name instead of number (e.g., [google](url))
*/
export function processCitations(
content: string,
@@ -76,31 +77,34 @@ export function processCitations(
return content || ''
}
- // Replace [number](#toolCallId) with [number](actual-url)
+ // Replace [number](#toolCallId) with [domain](actual-url)
// Also handle cases with spaces: [ number ]
return content.replace(
/\[\s*(\d+)\s*\]\(#([^)]+)\)/g,
- (match, num, toolCallId) => {
+ (_match, num, toolCallId) => {
const citationNum = parseInt(num, 10)
// Validate citation number bounds
if (isNaN(citationNum) || citationNum < 1 || citationNum > 100) {
- return match
+ return '' // Return empty string for invalid citation numbers
}
// Get the citation map for this toolCallId
const citationMap = citationMaps[toolCallId]
if (!citationMap) {
- return match // Keep original if no citation map found for this toolCallId
+ return '' // Return empty string if no citation map found
}
const citation = citationMap[citationNum]
- if (citation && isValidUrl(citation.url)) {
- // Encode URI to prevent injection attacks
- return `[${num}](${encodeURI(citation.url)})`
+ if (!citation || !isValidUrl(citation.url)) {
+ return '' // Return empty string for invalid citations
}
- return match // Keep original if no valid citation found
+ // Extract domain name from URL (removes TLD and subdomain)
+ const domainName = displayUrlName(citation.url)
+
+ // Encode URI to prevent injection attacks
+ return `[${domainName}](${encodeURI(citation.url)})`
}
)
}
diff --git a/lib/utils/domain.ts b/lib/utils/domain.ts
new file mode 100644
index 00000000..a83d4ff0
--- /dev/null
+++ b/lib/utils/domain.ts
@@ -0,0 +1,25 @@
+/**
+ * Extract display name from URL by removing TLD and www/subdomain
+ * This is a pure client-safe utility function without Next.js dependencies
+ * @param url - Full URL string
+ * @returns Domain name without TLD (e.g., "google" from "www.google.com")
+ * @example
+ * displayUrlName("https://www.google.com") // "google"
+ * displayUrlName("https://docs.github.com") // "github"
+ * displayUrlName("https://en.wikipedia.org") // "wikipedia"
+ */
+export const displayUrlName = (url: string): string => {
+ try {
+ const hostname = new URL(url).hostname
+ const parts = hostname.split('.')
+
+ // For hostnames like "www.google.com" or "docs.github.com"
+ // Extract the main domain name (second-to-last part)
+ // parts.length > 2: ["www", "google", "com"] -> "google"
+ // parts.length <= 2: ["localhost"] or ["example", "com"] -> "example"
+ return parts.length > 2 ? parts.slice(1, -1).join('.') : parts[0]
+ } catch {
+ // Fallback for invalid URLs
+ return 'source'
+ }
+}