Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(markdown): render GitHub / Linear / external links as rich chips (#15561)

Arvin Xueae47f527c00
diff --git a/package.json b/package.json
index c4b608ae701..a9033af1e9e 100644
--- a/package.json
+++ b/package.json
@@ -285,11 +285,11 @@
"@lobehub/analytics": "^1.6.2",
"@lobehub/charts": "^5.0.0",
"@lobehub/desktop-ipc-typings": "workspace:*",
- "@lobehub/editor": "^4.16.1",
+ "@lobehub/editor": "^4.17.0",
"@lobehub/icons": "^5.0.0",
"@lobehub/market-sdk": "0.33.3",
"@lobehub/tts": "^5.1.2",
- "@lobehub/ui": "^5.15.5",
+ "@lobehub/ui": "^5.15.10",
"@modelcontextprotocol/sdk": "^1.26.0",
"@napi-rs/canvas": "^0.1.88",
"@neondatabase/serverless": "^1.0.2",
diff --git a/src/features/Conversation/Markdown/plugins/Link/Render/FaviconIcon.tsx b/src/features/Conversation/Markdown/plugins/Link/Render/FaviconIcon.tsx
new file mode 100644
index 00000000000..243a2023d08
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/Link/Render/FaviconIcon.tsx
@@ -0,0 +1,32 @@
+import { Globe } from 'lucide-react';
+import { memo, useState } from 'react';
+
+interface FaviconIconProps {
+ domain: string;
+ size?: number;
+}
+
+/**
+ * Inline site favicon for generic external links. Falls back to a globe glyph
+ * when the favicon cannot be loaded.
+ */
+const FaviconIcon = memo<FaviconIconProps>(({ domain, size = 15 }) => {
+ const [failed, setFailed] = useState(false);
+
+ if (failed) return <Globe size={size} />;
+
+ return (
+ <img
+ alt=""
+ height={size}
+ src={`https://icons.duckduckgo.com/ip3/${domain}.ico`}
+ style={{ borderRadius: 3, objectFit: 'contain' }}
+ width={size}
+ onError={() => setFailed(true)}
+ />
+ );
+});
+
+FaviconIcon.displayName = 'FaviconIcon';
+
+export default FaviconIcon;
diff --git a/src/features/Conversation/Markdown/plugins/Link/Render/LinearIcon.tsx b/src/features/Conversation/Markdown/plugins/Link/Render/LinearIcon.tsx
new file mode 100644
index 00000000000..4b90ca8ca0c
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/Link/Render/LinearIcon.tsx
@@ -0,0 +1,23 @@
+import { memo } from 'react';
+
+interface LinearIconProps {
+ size?: number | string;
+}
+
+/** Official Linear logo mark (simple-icons), inherits `currentColor`. */
+const LinearIcon = memo<LinearIconProps>(({ size = '1em' }) => (
+ <svg
+ fill="currentColor"
+ height={size}
+ role="img"
+ viewBox="0 0 24 24"
+ width={size}
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path d="M2.886 4.18A11.982 11.982 0 0 1 11.99 0C18.624 0 24 5.376 24 12.009c0 3.64-1.62 6.903-4.18 9.105L2.887 4.18ZM1.817 5.626l16.556 16.556c-.524.33-1.075.62-1.65.866L.951 7.277c.247-.575.537-1.126.866-1.65ZM.322 9.163l14.515 14.515c-.71.172-1.443.282-2.195.322L0 11.358a12 12 0 0 1 .322-2.195Zm-.17 4.862 9.823 9.824a12.02 12.02 0 0 1-9.824-9.824Z" />
+ </svg>
+));
+
+LinearIcon.displayName = 'LinearIcon';
+
+export default LinearIcon;
diff --git a/src/features/Conversation/Markdown/plugins/Link/Render/LinkChip.tsx b/src/features/Conversation/Markdown/plugins/Link/Render/LinkChip.tsx
new file mode 100644
index 00000000000..2109cbbf623
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/Link/Render/LinkChip.tsx
@@ -0,0 +1,38 @@
+'use client';
+
+import { createStaticStyles } from 'antd-style';
+import { memo, type ReactNode } from 'react';
+
+const styles = createStaticStyles(({ css, cssVar }) => ({
+ chip: css`
+ color: ${cssVar.colorLink};
+ text-decoration: none;
+ transition: color 0.15s;
+
+ &:hover {
+ color: ${cssVar.colorLinkHover};
+ }
+ `,
+ icon: css`
+ display: inline-flex;
+ margin-inline-end: 4px;
+ vertical-align: -0.15em;
+ `,
+}));
+
+interface LinkChipProps {
+ href?: string;
+ icon: ReactNode;
+ label: string;
+}
+
+const LinkChip = memo<LinkChipProps>(({ href, icon, label }) => (
+ <a className={styles.chip} href={href} rel="noopener noreferrer" target="_blank">
+ <span className={styles.icon}>{icon}</span>
+ {label}
+ </a>
+));
+
+LinkChip.displayName = 'LinkChip';
+
+export default LinkChip;
diff --git a/src/features/Conversation/Markdown/plugins/Link/Render/index.tsx b/src/features/Conversation/Markdown/plugins/Link/Render/index.tsx
new file mode 100644
index 00000000000..bbed80d9d5d
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/Link/Render/index.tsx
@@ -0,0 +1,50 @@
+'use client';
+
+import { Github } from '@lobehub/icons';
+import { Mail } from 'lucide-react';
+import { memo } from 'react';
+
+import { type MarkdownElementProps } from '../../type';
+import { type LobeLinkKind } from '../parse';
+import FaviconIcon from './FaviconIcon';
+import LinearIcon from './LinearIcon';
+import LinkChip from './LinkChip';
+
+const ICON_SIZE = 15;
+
+interface LobeLinkProperties {
+ linkDomain?: string;
+ linkHref?: string;
+ linkKind?: LobeLinkKind;
+ linkLabel?: string;
+}
+
+const Render = memo<MarkdownElementProps<LobeLinkProperties>>(({ node }) => {
+ const { linkHref, linkKind, linkLabel, linkDomain } = node?.properties || {};
+
+ const label = linkLabel || linkHref || '';
+
+ if (linkKind === 'github') {
+ return <LinkChip href={linkHref} icon={<Github size={ICON_SIZE} />} label={label} />;
+ }
+
+ if (linkKind === 'linear') {
+ return <LinkChip href={linkHref} icon={<LinearIcon size={ICON_SIZE} />} label={label} />;
+ }
+
+ if (linkKind === 'email') {
+ return <LinkChip href={linkHref} icon={<Mail size={ICON_SIZE} />} label={label} />;
+ }
+
+ return (
+ <LinkChip
+ href={linkHref}
+ icon={<FaviconIcon domain={linkDomain || ''} size={ICON_SIZE} />}
+ label={label}
+ />
+ );
+});
+
+Render.displayName = 'LobeLinkRender';
+
+export default Render;
diff --git a/src/features/Conversation/Markdown/plugins/Link/index.ts b/src/features/Conversation/Markdown/plugins/Link/index.ts
new file mode 100644
index 00000000000..ae5b50ec18d
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/Link/index.ts
@@ -0,0 +1,15 @@
+import { type FC } from 'react';
+
+import { type MarkdownElement, type MarkdownElementProps } from '../type';
+import { LOBE_LINK_TAG } from './parse';
+import { rehypeLobeLink } from './rehypePlugin';
+import Render from './Render';
+
+const LinkElement: MarkdownElement = {
+ Component: Render as FC<MarkdownElementProps>,
+ rehypePlugin: rehypeLobeLink,
+ scope: 'all',
+ tag: LOBE_LINK_TAG,
+};
+
+export default LinkElement;
diff --git a/src/features/Conversation/Markdown/plugins/Link/parse.test.ts b/src/features/Conversation/Markdown/plugins/Link/parse.test.ts
new file mode 100644
index 00000000000..e221e454543
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/Link/parse.test.ts
@@ -0,0 +1,96 @@
+import { describe, expect, it } from 'vitest';
+
+import { parseLobeLink } from './parse';
+
+describe('parseLobeLink', () => {
+ it('parses github pull request', () => {
+ expect(parseLobeLink('https://github.com/lobehub/lobehub/pull/15557')).toEqual({
+ canonicalLabel: 'lobehub/lobehub#15557',
+ kind: 'github',
+ });
+ });
+
+ it('parses github issue', () => {
+ expect(parseLobeLink('https://github.com/lobehub/lobehub/issues/15554')).toEqual({
+ canonicalLabel: 'lobehub/lobehub#15554',
+ kind: 'github',
+ });
+ });
+
+ it('parses github commit (short sha)', () => {
+ expect(parseLobeLink('https://github.com/lobehub/lobehub/commit/d36aa75701abc')).toEqual({
+ canonicalLabel: 'lobehub/lobehub@d36aa75',
+ kind: 'github',
+ });
+ });
+
+ it('parses github repo root', () => {
+ expect(parseLobeLink('https://github.com/lobehub/lobehub')).toEqual({
+ canonicalLabel: 'lobehub/lobehub',
+ kind: 'github',
+ });
+ });
+
+ it('parses linear issue', () => {
+ expect(parseLobeLink('https://linear.app/lobehub/issue/LOBE-10141/codex-pptx-preview')).toEqual(
+ {
+ canonicalLabel: 'LOBE-10141',
+ kind: 'linear',
+ },
+ );
+ });
+
+ it('parses github user / org pages with the github icon', () => {
+ expect(parseLobeLink('https://github.com/lobehub')).toEqual({
+ canonicalLabel: 'lobehub',
+ kind: 'github',
+ });
+ });
+
+ it('uses the full URL as label for generic http links', () => {
+ expect(parseLobeLink('https://example.com/foo')).toEqual({
+ canonicalLabel: 'https://example.com/foo',
+ domain: 'example.com',
+ kind: 'generic',
+ });
+ // bare github.com (no owner) β†’ generic
+ expect(parseLobeLink('https://github.com')?.kind).toBe('generic');
+ });
+
+ it('labels npm packages by package name', () => {
+ expect(parseLobeLink('https://www.npmjs.com/package/@lobehub/ui')).toEqual({
+ canonicalLabel: '@lobehub/ui',
+ domain: 'npmjs.com',
+ kind: 'generic',
+ });
+ expect(parseLobeLink('https://www.npmjs.com/package/react/v/18.0.0')?.canonicalLabel).toBe(
+ 'react',
+ );
+ });
+
+ it('labels figma links by file name', () => {
+ expect(parseLobeLink('https://www.figma.com/file/abc123/Design-File')?.canonicalLabel).toBe(
+ 'Design File',
+ );
+ expect(parseLobeLink('https://www.figma.com/design/abc123/My-Board')?.canonicalLabel).toBe(
+ 'My Board',
+ );
+ });
+
+ it('parses mailto links as email', () => {
+ expect(parseLobeLink('mailto:[email protected]')).toEqual({
+ canonicalLabel: '[email protected]',
+ kind: 'email',
+ });
+ expect(parseLobeLink('mailto:[email protected]?subject=Hello')?.canonicalLabel).toBe(
+ );
+ });
+
+ it('ignores citation, footnote, relative and non-http hrefs', () => {
+ expect(parseLobeLink('citation-1')).toBeNull();
+ expect(parseLobeLink('#user-content-fn-1')).toBeNull();
+ expect(parseLobeLink('/foo/bar')).toBeNull();
+ expect(parseLobeLink(undefined)).toBeNull();
+ });
+});
diff --git a/src/features/Conversation/Markdown/plugins/Link/parse.ts b/src/features/Conversation/Markdown/plugins/Link/parse.ts
new file mode 100644
index 00000000000..459b0237b81
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/Link/parse.ts
@@ -0,0 +1,101 @@
+export const LOBE_LINK_TAG = 'lobeLink';
+
+export type LobeLinkKind = 'github' | 'linear' | 'email' | 'generic';
+
+export interface ParsedLobeLink {
+ /**
+ * Canonical label used when the link has no author-provided text, e.g.
+ * `lobehub/lobehub#15554` / `LOBE-10141` / `@lobehub/ui` / the full URL.
+ */
+ canonicalLabel: string;
+ /** Host for generic links, used to fetch a favicon. */
+ domain?: string;
+ kind: LobeLinkKind;
+}
+
+const stripWww = (host: string) => host.replace(/^www\./, '');
+
+/** npmjs.com/package/<name> β†’ `<name>` (handles scoped packages and versions). */
+const npmPackageName = (segments: string[]): string | undefined => {
+ const idx = segments.indexOf('package');
+ const rest = idx >= 0 ? segments.slice(idx + 1) : [];
+ if (rest.length === 0) return undefined;
+ return rest[0].startsWith('@') ? rest.slice(0, 2).join('/') : rest[0];
+};
+
+/** figma.com/(file|design)/<key>/<name> β†’ the human file name. */
+const figmaFileName = (segments: string[]): string | undefined => {
+ if ((segments[0] === 'file' || segments[0] === 'design') && segments[2]) {
+ try {
+ return decodeURIComponent(segments[2]).replaceAll('-', ' ');
+ } catch {
+ return segments[2];
+ }
+ }
+ return undefined;
+};
+
+/**
+ * Classify an href and derive a canonical short label.
+ *
+ * - GitHub repo / PR / issue / commit and Linear issues get a rich label.
+ * - npm packages / Figma files keep their favicon but get a friendly label.
+ * - `mailto:` links become an `email` chip.
+ * - Any other absolute http(s) link becomes a `generic` chip (favicon + full URL).
+ * - Citation links (`citation-1`), footnote refs, anchors and relative paths
+ * return `null` and keep the default link renderer untouched.
+ */
+export const parseLobeLink = (href?: string): ParsedLobeLink | null => {
+ if (!href) return null;
+
+ if (href.startsWith('mailto:')) {
+ const email = href.slice('mailto:'.length).split('?')[0];
+ return email ? { canonicalLabel: email, kind: 'email' } : null;
+ }
+
+ let url: URL;
+ try {
+ url = new URL(href);
+ } catch {
+ return null;
+ }
+
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') return null;
+
+ const host = stripWww(url.hostname);
+ const segments = url.pathname.split('/').filter(Boolean);
+
+ if (host === 'github.com') {
+ const [owner, repo, type, id] = segments;
+ if (owner) {
+ if (repo) {
+ if ((type === 'pull' || type === 'issues') && id) {
+ return { canonicalLabel: `${owner}/${repo}#${id}`, kind: 'github' };
+ }
+ if (type === 'commit' && id) {
+ return { canonicalLabel: `${owner}/${repo}@${id.slice(0, 7)}`, kind: 'github' };
+ }
+ return { canonicalLabel: `${owner}/${repo}`, kind: 'github' };
+ }
+ // user / org page β†’ keep the GitHub icon, show the handle
+ return { canonicalLabel: owner, kind: 'github' };
+ }
+ // bare github.com β†’ fall through to generic
+ }
+
+ if (host === 'linear.app') {
+ // workspace/issue/LOBE-123/slug
+ const issueIndex = segments.indexOf('issue');
+ const id = issueIndex >= 0 ? segments[issueIndex + 1] : undefined;
+ if (id) return { canonicalLabel: id.toUpperCase(), kind: 'linear' };
+ // fall through to generic
+ }
+
+ // Generic chip: favicon + a friendly label, falling back to the full URL.
+ const friendlyLabel =
+ (host === 'npmjs.com' && npmPackageName(segments)) ||
+ (host === 'figma.com' && figmaFileName(segments)) ||
+ href;
+
+ return { canonicalLabel: friendlyLabel, domain: host, kind: 'generic' };
+};
diff --git a/src/features/Conversation/Markdown/plugins/Link/rehypePlugin.ts b/src/features/Conversation/Markdown/plugins/Link/rehypePlugin.ts
new file mode 100644
index 00000000000..5c6cbb35041
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/Link/rehypePlugin.ts
@@ -0,0 +1,45 @@
+import { SKIP, visit } from 'unist-util-visit';
+
+import { LOBE_LINK_TAG, parseLobeLink } from './parse';
+
+/** Recursively collect the visible text of a HAST node. */
+const getNodeText = (node: any): string => {
+ if (!node) return '';
+ if (node.type === 'text') return String(node.value ?? '');
+ if (Array.isArray(node.children)) return node.children.map(getNodeText).join('');
+ return '';
+};
+
+/**
+ * Rehype plugin that rewrites GitHub / Linear anchor (`<a>`) elements into a
+ * custom `<lobeLink>` element so they can be rendered as rich inline chips.
+ *
+ * Anchors that are not GitHub / Linear links – including citation links
+ * (`citation-1`) and footnote refs – are left untouched and keep the default
+ * link renderer.
+ */
+export const rehypeLobeLink = () => (tree: any) => {
+ visit(tree, 'element', (node: any) => {
+ if (node.tagName !== 'a') return;
+
+ const href = node.properties?.href as string | undefined;
+ const parsed = parseLobeLink(href);
+ if (!parsed) return;
+
+ const text = getNodeText(node).trim();
+ // Prefer an author-provided label; fall back to the canonical short form
+ // when the link text is empty or just the raw URL.
+ const label = !text || text === href ? parsed.canonicalLabel : text;
+
+ node.tagName = LOBE_LINK_TAG;
+ node.children = [];
+ node.properties = {
+ linkDomain: parsed.domain,
+ linkHref: href,
+ linkKind: parsed.kind,
+ linkLabel: label,
+ };
+
+ return SKIP;
+ });
+};
diff --git a/src/features/Conversation/Markdown/plugins/index.ts b/src/features/Conversation/Markdown/plugins/index.ts
index 17a21e47fdd..5b121c9de61 100644
--- a/src/features/Conversation/Markdown/plugins/index.ts
+++ b/src/features/Conversation/Markdown/plugins/index.ts
@@ -1,4 +1,5 @@
import ImageSearchRef from './ImageSearchRef';
+import Link from './Link';
import LobeAgents from './LobeAgents';
import LobeArtifact from './LobeArtifact';
import LobeThinking from './LobeThinking';
@@ -25,4 +26,5 @@ export const markdownElements: MarkdownElement[] = [
UserFeedback,
ImageSearchRef,
LobeAgents,
+ Link,
];