Skip to content

Commit 261c6b6

Browse files
authored
Merge pull request #562 from GetStream/custom-link-render
feat: custom markup for links in messages
2 parents c5dc9aa + d5ba51c commit 261c6b6

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

projects/stream-chat-angular/src/lib/message.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,20 @@ import { Injectable } from '@angular/core';
99
export class MessageService {
1010
/**
1111
* Decides if the message content should be formatted as text or HTML
12+
*
13+
* If you display messages as text the following parts are still be displayed as HTML:
14+
* - user mentions -> you can customize this with your own template using the [`customTemplatesService.mentionTemplate$`](https://getstream.io/chat/docs/sdk/angular/services/CustomTemplatesService/#mentiontemplate)
15+
* - links -> you can customize this by providing you own [`customLinkRenderer`](#customlinkrenderer) method
1216
*/
1317
displayAs: 'text' | 'html' = 'text';
1418

19+
/**
20+
* You can provide a custom method to display links
21+
*
22+
* @param url the url the link should refer to
23+
* @returns the HTML markup as a string for the link
24+
*/
25+
customLinkRenderer?: (url: string) => string;
26+
1527
constructor() {}
1628
}

projects/stream-chat-angular/src/lib/message/message.component.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { AvatarPlaceholderComponent } from '../avatar-placeholder/avatar-placeho
2323
import { ThemeService } from '../theme.service';
2424
import { BehaviorSubject, of } from 'rxjs';
2525
import { MessageActionsService } from '../message-actions.service';
26+
import { MessageService } from '../message.service';
2627

2728
describe('MessageComponent', () => {
2829
let component: MessageComponent;
@@ -1331,4 +1332,19 @@ describe('MessageComponent', () => {
13311332
).not.toBe(null);
13321333
});
13331334
});
1335+
1336+
it('should replace URL links inside text content - custom link renderer', () => {
1337+
const service = TestBed.inject(MessageService);
1338+
service.customLinkRenderer = (url) =>
1339+
`<a href="${url}" class="my-special-class">${url}</a>`;
1340+
component.message = {
1341+
html: '<p>This is a message with a link <a href="https://getstream.io/" rel="nofollow">https://getstream.io/</a></p>\n',
1342+
text: 'This is a message with a link https://getstream.io/',
1343+
} as any as StreamMessage;
1344+
component.ngOnChanges({ message: {} as SimpleChange });
1345+
1346+
expect(component.messageTextParts![0].content).toContain(
1347+
' <a href="https://getstream.io/" class="my-special-class">https://getstream.io/</a>'
1348+
);
1349+
});
13341350
});

projects/stream-chat-angular/src/lib/message/message.component.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,10 @@ export class MessageComponent
494494
if (this.displayAs === 'html') {
495495
return content;
496496
}
497-
content = content.replace(
498-
this.urlRegexp,
499-
(match) =>
500-
`<a href="${match}" target="_blank" rel="nofollow">${match}</a>`
497+
content = content.replace(this.urlRegexp, (match) =>
498+
this.messageService.customLinkRenderer
499+
? this.messageService.customLinkRenderer(match)
500+
: `<a href="${match}" target="_blank" rel="nofollow">${match}</a>`
501501
);
502502

503503
return content;

0 commit comments

Comments
 (0)