Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:修复单行CodeBlock中显示sub #597

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"adm-zip": "^0.5.16",
"apache-arrow": "^18.1.0",
"docx": "^9.0.2",
"dompurify": "^3.2.3",
"electron-log": "^5.1.5",
"electron-store": "^8.2.0",
"electron-updater": "^6.3.9",
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/src/pages/home/Markdown/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import CopyIcon from '@renderer/components/Icons/CopyIcon'
import { useSyntaxHighlighter } from '@renderer/context/SyntaxHighlighterProvider'
import { useSettings } from '@renderer/hooks/useSettings'
import React, { memo, useEffect, useRef, useState } from 'react'
import DOMPurify from 'dompurify'
import { useTranslation } from 'react-i18next'
import styled from 'styled-components'

Expand Down Expand Up @@ -37,6 +38,7 @@ const ExpandButton: React.FC<{
</ExpandButtonWrapper>
)
}
const ALLOWED_TAGS = ['sub'] // 允许的HTML标签

const CodeBlock: React.FC<CodeBlockProps> = ({ children, className }) => {
const match = /language-(\w+)/.exec(className || '')
Expand Down Expand Up @@ -133,7 +135,15 @@ const CodeBlock: React.FC<CodeBlockProps> = ({ children, className }) => {
{language === 'html' && children?.includes('</html>') && <Artifacts html={children} />}
</CodeBlockWrapper>
) : (
<code className={className}>{children}</code>
<code
className={className}
dangerouslySetInnerHTML={{
__html: DOMPurify.sanitize(children, {
ALLOWED_TAGS,
ALLOWED_ATTR: [] // 不允许任何属性
})
}}
/>
)
}

Expand Down