Skip to content

Commit

Permalink
fix: Fix problems related to html parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ximu3 committed Nov 27, 2024
1 parent b5a9148 commit 80f1693
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Card, CardContent, CardHeader, CardTitle } from '@ui/card'
import { DescriptionDialog } from './DescriptionDialog'
import { cn } from '~/utils'
import { cn, HTMLParserOptions } from '~/utils'
import { useDBSyncedState } from '~/hooks'
import parse from 'html-react-parser'

Expand All @@ -24,9 +24,14 @@ export function DescriptionCard({
</CardHeader>
<CardContent className={cn('')}>
<div
className={cn('max-h-[310px] text-sm overflow-auto scrollbar-base', '3xl:max-h-[500px]')}
className={cn(
'max-h-[310px] text-sm overflow-auto scrollbar-base',
'3xl:max-h-[500px]',
'prose-a:text-primary', // 链接颜色
'prose-a:no-underline hover:prose-a:underline' // 下划线效果
)}
>
{description ? parse(description) : '暂无简介'}
{description ? parse(description, HTMLParserOptions) : '暂无简介'}
</div>
</CardContent>
</Card>
Expand Down
12 changes: 12 additions & 0 deletions src/renderer/src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as csstree from 'css-tree'
import { HTMLReactParserOptions, Element } from 'html-react-parser'

interface CSSValidationResult {
isValid: boolean
Expand All @@ -22,3 +23,14 @@ export function isValidCSS(cssContent: string): CSSValidationResult {
}
}
}

export const HTMLParserOptions: HTMLReactParserOptions = {
replace: (domNode) => {
if (domNode instanceof Element && domNode.name === 'a') {
// 确保链接打开在新标签页中
domNode.attribs.target = '_blank'
// 可选:添加 rel="noopener noreferrer" 以提高安全性
domNode.attribs.rel = 'noopener noreferrer'
}
}
}
3 changes: 2 additions & 1 deletion tailwind.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ module.exports = {
plugins: [
require('tailwindcss-animate'),
addDynamicIconSelectors(),
require('tailwind-scrollbar')({ nocompatible: true, preferredStrategy: 'pseudoelements' })
require('tailwind-scrollbar')({ nocompatible: true, preferredStrategy: 'pseudoelements' }),
require('@tailwindcss/typography')
]
}

0 comments on commit 80f1693

Please sign in to comment.