Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: keep artifact script content in card (#14135)

YuTengjing933cfbf7891e
diff --git a/src/features/Conversation/utils/markdown.test.ts b/src/features/Conversation/utils/markdown.test.ts
index b42d7a1190e..b4dbddb9593 100644
--- a/src/features/Conversation/utils/markdown.test.ts
+++ b/src/features/Conversation/utils/markdown.test.ts
@@ -323,7 +323,7 @@ Content 2 still going`;
<lobeArtifact identifier="generating" type="text/markdown" title="Generating">Content 2 still going`);
});
- it('should preserve HTML script blocks while flattening artifact markup', () => {
+ it('should keep HTML script blocks inside flattened artifact markup', () => {
const input = `<lobeArtifact identifier="snake-game" type="text/html" title="Snake Game">
<!DOCTYPE html>
<html>
@@ -340,16 +340,10 @@ window.snakeStarted = true;
const output = processWithArtifact(input);
- expect(output).toContain(`<script>
-// Move the snake every frame
-const url = " // keep string content untouched";
-window.snakeStarted = true;
-</script
- bar>`);
- expect(output).not.toContain('// Move the snake every framewindow.snakeStarted = true;');
expect(output).toContain(
- '<lobeArtifact identifier="snake-game" type="text/html" title="Snake Game"><!DOCTYPE html><html><body><script>',
+ '<lobeArtifact identifier="snake-game" type="text/html" title="Snake Game"><!DOCTYPE html><html><body><script>// Move the snake every frameconst url = " // keep string content untouched";window.snakeStarted = true;</script\t bar></body></html></lobeArtifact>',
);
+ expect(output).not.toContain('<script>\n');
});
});
diff --git a/src/features/Conversation/utils/markdown.ts b/src/features/Conversation/utils/markdown.ts
index 9e2bfd6c7ab..aa23a9a631b 100644
--- a/src/features/Conversation/utils/markdown.ts
+++ b/src/features/Conversation/utils/markdown.ts
@@ -2,22 +2,6 @@ import { ARTIFACT_THINKING_TAG_REGEX } from '@lobechat/const';
const ARTIFACT_TAG_REGEX_GLOBAL =
/<lobeArtifact\b[^>]*>(?<content>[\S\s]*?)(?:<\/lobeArtifact>|$)/g;
-const HTML_SCRIPT_TAG_REGEX = /<script\b[^>]*>[\S\s]*?<\/script(?:\s[^>]*)?>/gi;
-const SCRIPT_PLACEHOLDER_PREFIX = '____LOBE_ARTIFACT_SCRIPT_BLOCK_';
-
-const removeArtifactLineBreaks = (content: string) => {
- const scripts: string[] = [];
- const contentWithoutScripts = content.replaceAll(HTML_SCRIPT_TAG_REGEX, (script) => {
- const index = scripts.length;
- scripts.push(script);
- return `${SCRIPT_PLACEHOLDER_PREFIX}${index}____`;
- });
-
- return scripts.reduce(
- (result, script, index) => result.replace(`${SCRIPT_PLACEHOLDER_PREFIX}${index}____`, script),
- contentWithoutScripts.replaceAll(/\r?\n|\r/g, ''),
- );
-};
/**
* Replace all line breaks in the matched `lobeArtifact` tag with an empty string
@@ -67,7 +51,11 @@ export const processWithArtifact = (input: string = '') => {
// If the input contains `lobeArtifact` tags, replace all line breaks with an empty string
// Use global regex to handle multiple artifacts in the same message
- output = output.replaceAll(ARTIFACT_TAG_REGEX_GLOBAL, removeArtifactLineBreaks);
+ // Keep artifact markup as one raw HTML segment for the rehype artifact plugin. Preserving
+ // script block newlines here can make Markdown parse script text outside the custom tag.
+ output = output.replaceAll(ARTIFACT_TAG_REGEX_GLOBAL, (match) =>
+ match.replaceAll(/\r?\n|\r/g, ''),
+ );
// if not match, check if it's start with <lobeArtifact but not closed
const regex = /<lobeArtifact\b(?:(?!\/?>)[\s\S])*$/;