diff --git a/src/renderer/src/components/CodeBlockView/HtmlArtifacts.tsx b/src/renderer/src/components/CodeBlockView/HtmlArtifacts.tsx index e979ea1541..0dbb0aabb2 100644 --- a/src/renderer/src/components/CodeBlockView/HtmlArtifacts.tsx +++ b/src/renderer/src/components/CodeBlockView/HtmlArtifacts.tsx @@ -13,7 +13,6 @@ interface Props { const Artifacts: FC = ({ html }) => { const { t } = useTranslation() - const title = extractTitle(html) || 'Artifacts ' + t('chat.artifacts.button.preview') const { openMinapp } = useMinappPopup() /** @@ -23,6 +22,7 @@ const Artifacts: FC = ({ html }) => { const path = await window.api.file.create('artifacts-preview.html') await window.api.file.write(path, html) const filePath = `file://${path}` + const title = extractTitle(html) || 'Artifacts ' + t('chat.artifacts.button.preview') openMinapp({ id: 'artifacts-preview', name: title, diff --git a/src/renderer/src/utils/__tests__/formats.test.ts b/src/renderer/src/utils/__tests__/formats.test.ts index fc3df8bc51..56eddae67b 100644 --- a/src/renderer/src/utils/__tests__/formats.test.ts +++ b/src/renderer/src/utils/__tests__/formats.test.ts @@ -204,6 +204,11 @@ describe('formats', () => { it('should handle empty string', () => { expect(extractTitle('')).toBeNull() }) + + it('should handle undefined', () => { + // @ts-ignore for testing + expect(extractTitle(undefined)).toBeNull() + }) }) describe('removeSvgEmptyLines', () => { diff --git a/src/renderer/src/utils/formats.ts b/src/renderer/src/utils/formats.ts index a83ca4c632..2b67fd2c9a 100644 --- a/src/renderer/src/utils/formats.ts +++ b/src/renderer/src/utils/formats.ts @@ -54,6 +54,8 @@ $$ } export function extractTitle(html: string): string | null { + if (!html) return null + // 处理标准闭合的标题标签 const titleRegex = /(.*?)<\/title>/i const match = html.match(titleRegex)