Skip to content

Commit

Permalink
feat(ask-message): add HTML entity decoding for message rendering
Browse files Browse the repository at this point in the history
- Implemented a decodeEntities function to convert HTML entities into their corresponding characters.
- Updated the rendering logic in AskMessage to decode HTML entities in message content, enhancing text display accuracy and user experience.
  • Loading branch information
crazygo committed Jan 17, 2025
1 parent a2c4fd6 commit ebe8c51
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/components/ask-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ interface AskMessageItem {
name?: string;
}

function decodeEntities(text: string): string {
const textArea = document.createElement('textarea');
textArea.innerHTML = text;
return textArea.value;
}

function AskMessage(props: AskMessageItem) {
const { type, text, name } = props;
const [codeHover, setCodeHover] = useState<number | null>(null);
Expand Down Expand Up @@ -53,12 +59,16 @@ function AskMessage(props: AskMessageItem) {
} else {
return (
<div key={`text-${index}`}>
{block.content.map((line, lineIndex) => (
<React.Fragment key={`line-${lineIndex}`}>
{line}
{lineIndex < block.content.length - 1 && <br />}
</React.Fragment>
))}
{block.content.map((line, lineIndex) => {
// 解码 HTML 实体
const decodedLine = decodeEntities(line);
return (
<React.Fragment key={`line-${lineIndex}`}>
{decodedLine}
{lineIndex < block.content.length - 1 && <br />}
</React.Fragment>
);
})}
</div>
);
}
Expand Down

0 comments on commit ebe8c51

Please sign in to comment.