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

In chat template, display preformatted/code snippet outputs sensibly #6137

Merged
merged 2 commits into from
Mar 18, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -110,3 +110,11 @@
::deep th, ::deep tr:nth-child(even) {
background-color: rgba(0, 0, 0, 0.05);
}

::deep pre > code {
background-color: white;
display: block;
padding: 0.5rem 1rem;
margin: 1rem 0;
overflow-x: auto;
}
Original file line number Diff line number Diff line change
@@ -11,6 +11,14 @@ customElements.define('assistant-message', class extends HTMLElement {
newValue = newValue.replace(/<citation.*?<\/citation>/gs, '');
const elements = marked.parse(newValue.replace(/</g, '&lt;'));
this.innerHTML = purify.sanitize(elements, { KEEP_CONTENT: false });

// Within text nodes, unescape the &lt; entities otherwise it will be displayed
// to the user as escaped if the element uses preformatted styling. This is safe
// because we're only updating the text content of text nodes.
const walker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT);
while (walker.nextNode()) {
walker.currentNode.textContent = walker.currentNode.textContent.replace(/&lt;/g, '<');
}
}
}
});
Original file line number Diff line number Diff line change
@@ -110,3 +110,11 @@
::deep th, ::deep tr:nth-child(even) {
background-color: rgba(0, 0, 0, 0.05);
}

::deep pre > code {
background-color: white;
display: block;
padding: 0.5rem 1rem;
margin: 1rem 0;
overflow-x: auto;
}
Original file line number Diff line number Diff line change
@@ -11,6 +11,14 @@ customElements.define('assistant-message', class extends HTMLElement {
newValue = newValue.replace(/<citation.*?<\/citation>/gs, '');
const elements = marked.parse(newValue.replace(/</g, '&lt;'));
this.innerHTML = purify.sanitize(elements, { KEEP_CONTENT: false });

// Within text nodes, unescape the &lt; entities otherwise it will be displayed
// to the user as escaped if the element uses preformatted styling. This is safe
// because we're only updating the text content of text nodes.
const walker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT);
while (walker.nextNode()) {
walker.currentNode.textContent = walker.currentNode.textContent.replace(/&lt;/g, '<');
}
}
}
});