-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2223 from cardstack/cs-8047-user-is-not-able-to-s…
…end-html-code-to-the-assistant AI message: Escape html code sent by user
- Loading branch information
Showing
4 changed files
with
79 additions
and
11 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
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
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,62 @@ | ||
export function escapeHtmlTags(html: string) { | ||
// For example, html can be <pre><code><h1>Hello</h1></code></pre> | ||
// We want to escape the <h1>Hello</h1> so that it is rendered as | ||
// <pre><code><h1>Hello</h1></code></pre>, otherwise the h1 will | ||
// be rendered as a real header, not code (same applies for other html tags, such as <template>, <style>, ...) | ||
return html.replace(/</g, '<').replace(/>/g, '>'); | ||
} | ||
|
||
// Example input: | ||
// Hey can you teach mo how to use the <h1> tag? Is this correct? | ||
// ```html | ||
// <h1>Hello</h1> | ||
// ``` | ||
// | ||
// Output: | ||
// Hey can you teach mo how to use the <h1> tag? Is this correct? | ||
// ```html | ||
// <h1>Hello</h1> | ||
// ``` | ||
export function escapeHtmlOutsideCodeBlocks(text?: string) { | ||
if (text === undefined) { | ||
return text; | ||
} | ||
|
||
let matches = []; | ||
let codeBlockRegex = /```[\s\S]*?```/g; | ||
|
||
let match; | ||
while ((match = codeBlockRegex.exec(text)) !== null) { | ||
matches.push({ | ||
content: match[0], | ||
start: match.index, | ||
end: match.index + match[0].length, | ||
}); | ||
} | ||
|
||
if (matches.length === 0) { | ||
return escapeHtmlTags(text); | ||
} | ||
|
||
let result = ''; | ||
let lastIndex = 0; | ||
|
||
for (let block of matches) { | ||
if (block.start > lastIndex) { | ||
let textBeforeBlock = text.substring(lastIndex, block.start); | ||
result += escapeHtmlTags(textBeforeBlock); | ||
} | ||
|
||
result += block.content; | ||
|
||
lastIndex = block.end; | ||
} | ||
|
||
// Process any text after the last code block | ||
if (lastIndex < text.length) { | ||
let textAfterLastBlock = text.substring(lastIndex); | ||
result += escapeHtmlTags(textAfterLastBlock); | ||
} | ||
|
||
return result; | ||
} |
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