Skip to content

Commit 8f2a0b4

Browse files
authored
Merge pull request #51 from linkdotnet/feature/read-time
Feature/read time
2 parents 6cceedd + e2e1445 commit 8f2a0b4

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/LinkDotNet.Blog.Web/Shared/ShortBlogPost.razor

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@using System.Net
2+
@using System.Text.RegularExpressions
23
@using System.Web
34
@using LinkDotNet.Blog.Domain
45
@inherits MarkdownComponentBase
@@ -19,6 +20,7 @@
1920
</ul>
2021
</li>
2122
}
23+
<li class="read-time">@readTime min</li>
2224
</ul>
2325
</div>
2426
<div class="description">
@@ -40,4 +42,29 @@
4042
public bool UseAlternativeStyle { get; set; }
4143

4244
private string AltCssClass => UseAlternativeStyle ? "alt" : string.Empty;
45+
46+
private int readTime;
47+
48+
protected override void OnInitialized()
49+
{
50+
var images = GetAllOccurrences(RenderMarkupString(BlogPost.Content).Value, "<img");
51+
var words = BlogPost.Content.Split(Array.Empty<char>(), StringSplitOptions.RemoveEmptyEntries).Length;
52+
53+
var readTimeInPrecision = images * 15 + words / 3.333;
54+
readTime = (int)Math.Ceiling(readTimeInPrecision / 60);
55+
}
56+
57+
private int GetAllOccurrences(string text, string lookup)
58+
{
59+
var currentPosition = -1;
60+
var count = 0;
61+
62+
while ((currentPosition = text.IndexOf(lookup, currentPosition + 1, StringComparison.Ordinal)) != -1)
63+
{
64+
count++;
65+
}
66+
67+
return count;
68+
}
69+
4370
}

src/LinkDotNet.Blog.Web/Shared/ShortBlogPost.razor.css

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@
8282
.blog-card .details .tags li {
8383
margin-right: 2px;
8484
}
85-
.blog-card .details .tags li:first-child {
86-
margin-left: -4px;
87-
}
85+
8886
.blog-card .description {
8987
padding: 1rem;
9088
background: #fff;
@@ -117,6 +115,14 @@
117115
margin-left: 5px;
118116
opacity: 1;
119117
}
118+
119+
.blog-card .read-time:before {
120+
font-family: "Font Awesome 5 Free";
121+
font-weight: 900;
122+
margin-right: 10px;
123+
content: "\f017";
124+
}
125+
120126
.blog-card p {
121127
position: relative;
122128
margin: 1rem 0 0;

tests/LinkDotNet.Blog.UnitTests/Web/Shared/ShortBlogPostTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,17 @@ public void ShouldNavigateToEscapedTagSiteWhenClickingOnTag()
3333

3434
searchByTagLink.Attributes.Single(a => a.Name == "href").Value.Should().Be("/searchByTag/Tag%201");
3535
}
36-
}
36+
37+
[Fact]
38+
public void ShouldCalculateReadTime()
39+
{
40+
var content = string.Join(' ', Enumerable.Repeat("word", 700)) + string.Join(' ', Enumerable.Repeat("<img>", 4));
41+
var blogPost = new BlogPostBuilder().WithContent(content).Build();
42+
var cut = RenderComponent<ShortBlogPost>(
43+
p => p.Add(c => c.BlogPost, blogPost));
44+
45+
var readTime = cut.Find(".read-time");
46+
47+
readTime.TextContent.Should().Be("5 min");
48+
}
49+
}

0 commit comments

Comments
 (0)