Skip to content

Commit 7a733ab

Browse files
committed
feat: render sanitized HTML for articles to improve security and content display
1 parent e08f9c8 commit 7a733ab

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

src/app/pages/article-page/article-page.component.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
</mat-toolbar-row>
2828
</mat-toolbar>
2929
<h2>{{ article()?.title }}</h2>
30-
<p>
31-
{{ article()?.contentSnippet }}
30+
<p [innerHtml]="safeHtml(article()?.content)">
3231
</p>
3332
<mat-divider/>
3433
@if (article()?.categories?.length) {

src/app/pages/article-page/article-page.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { DatePipe } from '@angular/common'
1515
import { HttpErrorResponse } from '@angular/common/http'
1616
import { TagService } from '../../services/tag-service'
1717
import { TitleService } from '../../services/title-service'
18+
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
1819

1920
@Component({
2021
selector: 'app-article',
@@ -38,6 +39,7 @@ export class ArticlePage implements OnInit {
3839
route = inject(ActivatedRoute)
3940
titleService = inject(TitleService)
4041
destroyRef = inject(DestroyRef)
42+
domSanitizer = inject(DomSanitizer)
4143

4244
article = signal<Article | null>(null)
4345

@@ -47,6 +49,13 @@ export class ArticlePage implements OnInit {
4749
return !!this.article()?.read
4850
})
4951

52+
safeHtml(html?: string): SafeHtml | undefined {
53+
if (!html) {
54+
return
55+
}
56+
return this.domSanitizer.bypassSecurityTrustHtml(html)
57+
}
58+
5059
ngOnInit() {
5160
this.route.params
5261
.pipe(

src/app/pages/home/home-page.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</mat-card-header>
3838
@if (display() !== 'title') {
3939
<mat-card-content>
40-
<p>{{ a.contentSnippet }}</p>
40+
<p [innerHtml]="safeHtml(a.content)"></p>
4141
</mat-card-content>
4242
}
4343
<mat-card-actions>

src/app/pages/home/home-page.component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { TagService } from '../../services/tag-service'
1818
import { Tag } from '../../entities/tag/tag.types'
1919
import { MatChipOption, MatChipSet } from '@angular/material/chips'
2020
import { TitleService } from '../../services/title-service'
21+
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
2122

2223
@Component({
2324
selector: 'app-home',
@@ -45,6 +46,7 @@ export class HomePage implements OnInit {
4546
destroyRef = inject(DestroyRef)
4647
tagService = inject(TagService)
4748
titleService = inject(TitleService)
49+
htmlSanitizer = inject(DomSanitizer)
4850

4951
articles = signal<Article[]>([])
5052
articleIds = computed(() => this.articles().map(({ _id }) => _id))
@@ -130,6 +132,10 @@ export class HomePage implements OnInit {
130132
this.display.set(display)
131133
}
132134

135+
safeHtml(html: string): SafeHtml {
136+
return this.htmlSanitizer.bypassSecurityTrustHtml(html)
137+
}
138+
133139
async onArticleClick(article: Article) {
134140
await this.router.navigate(['subscription', article.subscriptionId, 'article', article._id])
135141
}

0 commit comments

Comments
 (0)