fix: handle undefined html title (#6229)

This commit is contained in:
one 2025-05-20 20:57:14 +08:00 committed by GitHub
parent 312b73225a
commit 6cd324f8ba
3 changed files with 8 additions and 1 deletions

View File

@ -13,7 +13,6 @@ interface Props {
const Artifacts: FC<Props> = ({ html }) => {
const { t } = useTranslation()
const title = extractTitle(html) || 'Artifacts ' + t('chat.artifacts.button.preview')
const { openMinapp } = useMinappPopup()
/**
@ -23,6 +22,7 @@ const Artifacts: FC<Props> = ({ 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,

View File

@ -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', () => {

View File

@ -54,6 +54,8 @@ $$
}
export function extractTitle(html: string): string | null {
if (!html) return null
// 处理标准闭合的标题标签
const titleRegex = /<title>(.*?)<\/title>/i
const match = html.match(titleRegex)