Skip to content

Commit 7eb9823

Browse files
authored
Merge pull request #628 from tsdamas/issue-617
Fixing TagHelper that guestimates the read time.
2 parents c45aa6d + 6613b64 commit 7eb9823

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

source/DasBlog.Web.UI/TagHelpers/Post/PostReadTimeTagHelper.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using DasBlog.Web.Models.BlogViewModels;
33
using Microsoft.AspNetCore.Razor.TagHelpers;
44
using System;
5-
using System.Linq;
5+
using System.Text.RegularExpressions;
66

77
namespace DasBlog.Web.TagHelpers.Post
88
{
@@ -20,9 +20,12 @@ public PostReadTimeTagHelper(IDasBlogSettings dasBlogSettings)
2020

2121
public override void Process(TagHelperContext context, TagHelperOutput output)
2222
{
23-
var delimiters = new char[] { ' ', '\r', '\n' };
24-
var minute = Math.Round((double)dasBlogSettings.FilterHtml(Post.Content).Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Length / 200);
23+
var rx = new Regex(@"<img.*?src="".*?"".*?>");
24+
var numberImages = rx.Matches(Post.Content).Count;
2525

26+
var imgMinutes = (double)(numberImages*30)/60;
27+
var delimiters = new char[] { ' ', '\r', '\n' };
28+
var minute = Math.Round((double)dasBlogSettings.FilterHtml(Post.Content).Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Length / 200) + Math.Round(imgMinutes, MidpointRounding.AwayFromZero);
2629
output.TagName = "span";
2730
output.TagMode = TagMode.StartTagAndEndTag;
2831
output.Attributes.SetAttribute("class", "dbc-post-readtime");

0 commit comments

Comments
 (0)