Skip to content

Commit 8eb39af

Browse files
In chat template, display preformatted/code snippet outputs sensibly (#6137)
1 parent 9cd1186 commit 8eb39af

4 files changed

Lines changed: 32 additions & 0 deletions

File tree

  • src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData.Web-CSharp
  • test/ProjectTemplates/Microsoft.Extensions.AI.Templates.IntegrationTests/Snapshots/aichatweb.Basic.verified/aichatweb

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData.Web-CSharp/Components/Pages/Chat/ChatMessageItem.razor.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,11 @@
110110
::deep th, ::deep tr:nth-child(even) {
111111
background-color: rgba(0, 0, 0, 0.05);
112112
}
113+
114+
::deep pre > code {
115+
background-color: white;
116+
display: block;
117+
padding: 0.5rem 1rem;
118+
margin: 1rem 0;
119+
overflow-x: auto;
120+
}

src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/ChatWithCustomData/ChatWithCustomData.Web-CSharp/wwwroot/app.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ customElements.define('assistant-message', class extends HTMLElement {
1111
newValue = newValue.replace(/<citation.*?<\/citation>/gs, '');
1212
const elements = marked.parse(newValue.replace(/</g, '&lt;'));
1313
this.innerHTML = purify.sanitize(elements, { KEEP_CONTENT: false });
14+
15+
// Within text nodes, unescape the &lt; entities otherwise it will be displayed
16+
// to the user as escaped if the element uses preformatted styling. This is safe
17+
// because we're only updating the text content of text nodes.
18+
const walker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT);
19+
while (walker.nextNode()) {
20+
walker.currentNode.textContent = walker.currentNode.textContent.replace(/&lt;/g, '<');
21+
}
1422
}
1523
}
1624
});

test/ProjectTemplates/Microsoft.Extensions.AI.Templates.IntegrationTests/Snapshots/aichatweb.Basic.verified/aichatweb/Components/Pages/Chat/ChatMessageItem.razor.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,11 @@
110110
::deep th, ::deep tr:nth-child(even) {
111111
background-color: rgba(0, 0, 0, 0.05);
112112
}
113+
114+
::deep pre > code {
115+
background-color: white;
116+
display: block;
117+
padding: 0.5rem 1rem;
118+
margin: 1rem 0;
119+
overflow-x: auto;
120+
}

test/ProjectTemplates/Microsoft.Extensions.AI.Templates.IntegrationTests/Snapshots/aichatweb.Basic.verified/aichatweb/wwwroot/app.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ customElements.define('assistant-message', class extends HTMLElement {
1111
newValue = newValue.replace(/<citation.*?<\/citation>/gs, '');
1212
const elements = marked.parse(newValue.replace(/</g, '&lt;'));
1313
this.innerHTML = purify.sanitize(elements, { KEEP_CONTENT: false });
14+
15+
// Within text nodes, unescape the &lt; entities otherwise it will be displayed
16+
// to the user as escaped if the element uses preformatted styling. This is safe
17+
// because we're only updating the text content of text nodes.
18+
const walker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT);
19+
while (walker.nextNode()) {
20+
walker.currentNode.textContent = walker.currentNode.textContent.replace(/&lt;/g, '<');
21+
}
1422
}
1523
}
1624
});

0 commit comments

Comments
 (0)