Skip to content

Commit 52c5092

Browse files
Chat: Fix html parsing in AI demo (#28772)
1 parent 9b523f5 commit 52c5092

File tree

18 files changed

+78
-81
lines changed

18 files changed

+78
-81
lines changed

apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.component.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@
4444
margin-bottom: 0;
4545
}
4646

47-
::ng-deep .dx-chat-messagebubble-content ol,
48-
::ng-deep .dx-chat-messagebubble-content ul {
49-
white-space: normal;
50-
}
51-
5247
::ng-deep .dx-chat-messagebubble-content h1,
5348
::ng-deep .dx-chat-messagebubble-content h2,
5449
::ng-deep .dx-chat-messagebubble-content h3,

apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.service.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { unified } from 'unified';
55
import remarkParse from 'remark-parse';
66
import remarkRehype from 'remark-rehype';
77
import rehypeStringify from 'rehype-stringify';
8+
import rehypeMinifyWhitespace from 'rehype-minify-whitespace';
89
import {
910
User,
1011
Alert,
11-
MessageEnteredEvent
12+
MessageEnteredEvent,
1213
} from 'devextreme/ui/chat';
1314
import DataSource from 'devextreme/data/data_source';
1415
import CustomStore from 'devextreme/data/custom_store';
@@ -26,9 +27,10 @@ export class AppService {
2627
apiVersion: '2024-02-01',
2728
endpoint: 'https://public-api.devexpress.com/demo-openai',
2829
apiKey: 'DEMO',
29-
}
30+
};
3031

3132
REGENERATION_TEXT = 'Regeneration...';
33+
3234
ALERT_TIMEOUT = 1000 * 60;
3335

3436
user: User = {
@@ -41,7 +43,9 @@ export class AppService {
4143
};
4244

4345
store: any[] = [];
46+
4447
messages: any[] = [];
48+
4549
alerts: Alert[] = [];
4650

4751
customStore: CustomStore;
@@ -54,7 +58,7 @@ export class AppService {
5458

5559
constructor() {
5660
this.chatService = new AzureOpenAI(this.AzureOpenAIConfig);
57-
this.initDataSource()
61+
this.initDataSource();
5862
this.typingUsersSubject.next([]);
5963
this.alertsSubject.next([]);
6064
}
@@ -73,28 +77,24 @@ export class AppService {
7377
'dxChat-emptyListMessage': 'Chat is Empty',
7478
'dxChat-emptyListPrompt': 'AI Assistant is ready to answer your questions.',
7579
'dxChat-textareaPlaceholder': 'Ask AI Assistant...',
76-
}
77-
}
80+
},
81+
};
7882
}
7983

8084
initDataSource() {
8185
this.customStore = new CustomStore({
8286
key: 'id',
83-
load: () => {
84-
return new Promise((resolve) => {
85-
setTimeout(() => {
86-
resolve([...this.store]);
87-
}, 0);
87+
load: () => new Promise((resolve) => {
88+
setTimeout(() => {
89+
resolve([...this.store]);
90+
}, 0);
91+
}),
92+
insert: (message) => new Promise((resolve) => {
93+
setTimeout(() => {
94+
this.store.push(message);
95+
resolve(message);
8896
});
89-
},
90-
insert: (message) => {
91-
return new Promise((resolve) => {
92-
setTimeout(() => {
93-
this.store.push(message);
94-
resolve(message);
95-
});
96-
});
97-
},
97+
}),
9898
});
9999

100100
this.dataSource = new DataSource({
@@ -160,7 +160,7 @@ export class AppService {
160160

161161
alertLimitReached() {
162162
this.setAlerts([{
163-
message: 'Request limit reached, try again in a minute.'
163+
message: 'Request limit reached, try again in a minute.',
164164
}]);
165165

166166
setTimeout(() => {
@@ -189,6 +189,7 @@ export class AppService {
189189
const result = unified()
190190
.use(remarkParse)
191191
.use(remarkRehype)
192+
.use(rehypeMinifyWhitespace)
192193
.use(rehypeStringify)
193194
.processSync(value)
194195
.toString();

apps/demos/Demos/Chat/AIAndChatbotIntegration/React/Message.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Button from 'devextreme-react/button';
33
import { unified } from 'unified';
44
import remarkParse from 'remark-parse';
55
import remarkRehype from 'remark-rehype';
6+
import rehypeMinifyWhitespace from 'rehype-minify-whitespace';
67
import rehypeStringify from 'rehype-stringify';
78
import HTMLReactParser from 'html-react-parser';
89

@@ -13,6 +14,7 @@ function convertToHtml(value: string): string {
1314
const result = unified()
1415
.use(remarkParse)
1516
.use(remarkRehype)
17+
.use(rehypeMinifyWhitespace)
1618
.use(rehypeStringify)
1719
.processSync(value)
1820
.toString();

apps/demos/Demos/Chat/AIAndChatbotIntegration/React/styles.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@
4444
margin-bottom: 0;
4545
}
4646

47-
.dx-chat-messagebubble-content ol,
48-
.dx-chat-messagebubble-content ul {
49-
white-space: normal;
50-
}
51-
5247
.dx-chat-messagebubble-content h1,
5348
.dx-chat-messagebubble-content h2,
5449
.dx-chat-messagebubble-content h3,

apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/Message.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Button from 'devextreme-react/button';
33
import { unified } from 'unified';
44
import remarkParse from 'remark-parse';
55
import remarkRehype from 'remark-rehype';
6+
import rehypeMinifyWhitespace from 'rehype-minify-whitespace';
67
import rehypeStringify from 'rehype-stringify';
78
import HTMLReactParser from 'html-react-parser';
89
import { REGENERATION_TEXT } from './data.js';
@@ -11,6 +12,7 @@ function convertToHtml(value) {
1112
const result = unified()
1213
.use(remarkParse)
1314
.use(remarkRehype)
15+
.use(rehypeMinifyWhitespace)
1416
.use(rehypeStringify)
1517
.processSync(value)
1618
.toString();

apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/styles.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@
4444
margin-bottom: 0;
4545
}
4646

47-
.dx-chat-messagebubble-content ol,
48-
.dx-chat-messagebubble-content ul {
49-
white-space: normal;
50-
}
51-
5247
.dx-chat-messagebubble-content h1,
5348
.dx-chat-messagebubble-content h2,
5449
.dx-chat-messagebubble-content h3,

apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
<div
2727
v-html="convertToHtml(data.message.text)"
2828
class="dx-chat-messagebubble-text"
29-
>
30-
</div>
29+
/>
3130
<div class="dx-bubble-button-container">
3231
<DxButton
3332
:icon="copyButtonIcon"
@@ -99,7 +98,7 @@ function toggleDisabledState(disabled, event = undefined) {
9998
} else {
10099
event?.target.focus();
101100
}
102-
};
101+
}
103102
104103
function updateLastMessage(text = REGENERATION_TEXT) {
105104
const items = dataSource.items();
@@ -148,7 +147,7 @@ async function processMessageSending(message, event) {
148147
149148
function alertLimitReached() {
150149
alerts.value = [{
151-
message: 'Request limit reached, try again in a minute.'
150+
message: 'Request limit reached, try again in a minute.',
152151
}];
153152
154153
setTimeout(() => {
@@ -185,7 +184,7 @@ function onCopyButtonClick(message) {
185184
copyButtonIcon.value = 'check';
186185
187186
setTimeout(() => {
188-
copyButtonIcon.value = 'copy';
187+
copyButtonIcon.value = 'copy';
189188
}, 2500);
190189
}
191190
@@ -245,11 +244,6 @@ function onRegenerateButtonClick() {
245244
margin-bottom: 0;
246245
}
247246
248-
.dx-chat-messagebubble-content ol,
249-
.dx-chat-messagebubble-content ul {
250-
white-space: normal;
251-
}
252-
253247
.dx-chat-messagebubble-content h1,
254248
.dx-chat-messagebubble-content h2,
255249
.dx-chat-messagebubble-content h3,
Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
import CustomStore from "devextreme/data/custom_store";
2-
import DataSource from "devextreme/data/data_source";
1+
import CustomStore from 'devextreme/data/custom_store';
2+
import DataSource from 'devextreme/data/data_source';
33
import { unified } from 'unified';
44
import remarkParse from 'remark-parse';
55
import remarkRehype from 'remark-rehype';
66
import rehypeStringify from 'rehype-stringify';
7+
import rehypeMinifyWhitespace from 'rehype-minify-whitespace';
78

89
export const dictionary = {
910
en: {
1011
'dxChat-emptyListMessage': 'Chat is Empty',
1112
'dxChat-emptyListPrompt': 'AI Assistant is ready to answer your questions.',
1213
'dxChat-textareaPlaceholder': 'Ask AI Assistant...',
1314
},
14-
}
15+
};
1516

1617
export const AzureOpenAIConfig = {
1718
dangerouslyAllowBrowser: true,
1819
deployment: 'gpt-4o-mini',
1920
apiVersion: '2024-02-01',
2021
endpoint: 'https://public-api.devexpress.com/demo-openai',
2122
apiKey: 'DEMO',
22-
}
23+
};
2324

2425
export const REGENERATION_TEXT = 'Regeneration...';
2526
export const CHAT_DISABLED_CLASS = 'dx-chat-disabled';
@@ -39,35 +40,32 @@ export const messages = [];
3940

4041
const customStore = new CustomStore({
4142
key: 'id',
42-
load: () => {
43-
return new Promise((resolve) => {
44-
setTimeout(() => {
45-
resolve([...store]);
46-
}, 0);
43+
load: () => new Promise((resolve) => {
44+
setTimeout(() => {
45+
resolve([...store]);
46+
}, 0);
47+
}),
48+
insert: (message) => new Promise((resolve) => {
49+
setTimeout(() => {
50+
store.push(message);
51+
resolve(message);
4752
});
48-
},
49-
insert: (message) => {
50-
return new Promise((resolve) => {
51-
setTimeout(() => {
52-
store.push(message);
53-
resolve(message);
54-
});
55-
});
56-
},
53+
}),
5754
});
5855

5956
export const dataSource = new DataSource({
6057
store: customStore,
6158
paginate: false,
62-
})
59+
});
6360

6461
export function convertToHtml(value: string) {
6562
const result = unified()
66-
.use(remarkParse)
67-
.use(remarkRehype)
68-
.use(rehypeStringify)
69-
.processSync(value)
70-
.toString();
63+
.use(remarkParse)
64+
.use(remarkRehype)
65+
.use(rehypeMinifyWhitespace)
66+
.use(rehypeStringify)
67+
.processSync(value)
68+
.toString();
7169

7270
return result;
7371
}

apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
import remarkParse from "https://esm.sh/remark-parse@11?bundle";
1515
import remarkRehype from "https://esm.sh/remark-rehype@11?bundle";
1616
import rehypeStringify from "https://esm.sh/rehype-stringify@10?bundle";
17+
import rehypeMinifyWhitespace from "https://esm.sh/rehype-minify-whitespace@6?bundle";
1718

1819
window.AzureOpenAI = AzureOpenAI;
1920
window.unified = unified;
2021
window.remarkParse = remarkParse;
2122
window.remarkRehype = remarkRehype;
23+
window.rehypeMinifyWhitespace = rehypeMinifyWhitespace;
2224
window.rehypeStringify = rehypeStringify;
2325
</script>
2426

apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ $(() => {
124124
const result = unified()
125125
.use(remarkParse)
126126
.use(remarkRehype)
127+
.use(rehypeMinifyWhitespace)
127128
.use(rehypeStringify)
128129
.processSync(value)
129130
.toString();

0 commit comments

Comments
 (0)