-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat): improve styling of code blocks
- Loading branch information
Showing
11 changed files
with
50 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
apps/chat/src/modules/chats/conversation/messages/content/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './chat-message-content'; | ||
export * from './parser'; | ||
export * from './hydrate'; |
30 changes: 30 additions & 0 deletions
30
...rc/modules/chats/conversation/messages/content/sanitize-content-preserving-code-blocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import sanitizeHtml from 'sanitize-html'; | ||
|
||
export function sanitizeContentPreservingCodeBlocks(content: string): string { | ||
const codeBlocks: string[] = []; | ||
|
||
// Split content by ``` to handle unclosed blocks | ||
const parts = content.split('```'); | ||
let result = parts[0]; | ||
|
||
// Process alternating text/code blocks | ||
for (let i = 1; i < parts.length; i++) { | ||
if (i % 2 === 1) { | ||
// This is a code block | ||
const placeholder = `__CODE_BLOCK_${Math.floor(i / 2)}__`; | ||
const codeContent = `\`\`\`${parts[i]}${i < parts.length - 1 ? '```' : ''}`; | ||
codeBlocks.push(codeContent); | ||
result += placeholder; | ||
} | ||
else { | ||
// This is regular text | ||
result += parts[i]; | ||
} | ||
} | ||
|
||
const sanitizedContent = sanitizeHtml(result); | ||
|
||
// Restore code blocks | ||
return sanitizedContent.replace(/__CODE_BLOCK_(\d+)__/g, (_, index) => | ||
codeBlocks[Number.parseInt(index, 10)]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters