Skip to content

Commit

Permalink
Merge pull request #33 from jorbush/reading-time
Browse files Browse the repository at this point in the history
adding reading time pipe
  • Loading branch information
jorbush authored Oct 1, 2024
2 parents f4c8db3 + c94161e commit 6cc2f49
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { AuthService } from '../../services/auth.service';
import { PostService } from '../../services/post.service';
import { CommonModule } from '@angular/common';
import { ToastService } from '../../services/toast.service';
import { ReadingTimePipe } from '../../pipes/reading-time.pipe';

@Component({
selector: 'app-post-detail',
standalone: true,
imports: [CommonModule],
imports: [CommonModule, ReadingTimePipe],
template: `
<div class="post-detail-container">
@if (post) {
Expand Down Expand Up @@ -86,6 +87,9 @@ import { ToastService } from '../../services/toast.service';
</div>
<div class="post-meta">
<span class="author">By: {{ post.user.username }}</span>
<span class="reading-time" aria-label="Estimated reading time">{{
post.content | readingTime
}}</span>
<span class="date">{{ post.createdAt | date: 'mediumDate' }}</span>
</div>
<p class="post-body">{{ post.content }}</p>
Expand Down
8 changes: 8 additions & 0 deletions postrify-frontend/src/app/pipes/reading-time.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReadingTimePipe } from './reading-time.pipe';

describe('ReadingTimePipe', () => {
it('create an instance', () => {
const pipe = new ReadingTimePipe();
expect(pipe).toBeTruthy();
});
});
15 changes: 15 additions & 0 deletions postrify-frontend/src/app/pipes/reading-time.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'readingTime',
standalone: true,
})
export class ReadingTimePipe implements PipeTransform {
transform(content: string): string {
if (!content) return '0 min read';
const wordsPerMinute = 200;
const words = content.trim().split(/\s+/).length;
const minutes = Math.ceil(words / wordsPerMinute);
return `${minutes} min read`;
}
}

0 comments on commit 6cc2f49

Please sign in to comment.