From 476dd140767783bdd00637d3bbfeb7bee43cd0ac Mon Sep 17 00:00:00 2001 From: hoseacodes Date: Fri, 24 May 2024 20:31:27 -0500 Subject: [PATCH] fix(*): update read time --- src/Components/Article/MainContainer.jsx | 2 +- src/Pages/Articles/Article/Article.jsx | 3 ++- src/Utils/helperFunctions.js | 8 +++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Components/Article/MainContainer.jsx b/src/Components/Article/MainContainer.jsx index 82466fca..a9e6fba2 100644 --- a/src/Components/Article/MainContainer.jsx +++ b/src/Components/Article/MainContainer.jsx @@ -47,7 +47,7 @@ const MainContainer = (props) => { {timeFormater}  ·  - {readTime} min read   + {readTime > 60 ? Math.floor((readTime / 60)) + " Hours " + (readTime % 60) : readTime } min read    ·  diff --git a/src/Pages/Articles/Article/Article.jsx b/src/Pages/Articles/Article/Article.jsx index 5df212f6..6eed4fa6 100755 --- a/src/Pages/Articles/Article/Article.jsx +++ b/src/Pages/Articles/Article/Article.jsx @@ -8,6 +8,7 @@ import RightColumn from "../../../Components/Article/RightColumn"; import MainContainer from "../../../Components/Article/MainContainer"; import { BlogContent } from "../../../Layout/Container/styledArticle"; import axios from "axios"; +import { countWords } from "../../../Utils/helperFunctions"; const ArticleItem = () => { const params = useParams(); @@ -50,7 +51,7 @@ const ArticleItem = () => { const timeFormater = moment.utc(createdAt).format("MMMM Do YYYY"); const avgWordsMinRead = 238; - const wordCount = markdown.length + 700; + const wordCount = countWords(markdown); const readTime = Math.round(wordCount / avgWordsMinRead); return ( <> diff --git a/src/Utils/helperFunctions.js b/src/Utils/helperFunctions.js index 26ff7e66..c15f7191 100644 --- a/src/Utils/helperFunctions.js +++ b/src/Utils/helperFunctions.js @@ -93,5 +93,11 @@ const getBasicAuth = (username, password) => { return auth; }; +function countWords(text) { + text = text.trim(); + const words = text.split(/\s+/); + const wordCount = words.filter((word) => word.length > 0).length; + return wordCount; +} -export { copyTextToClipboard, responsive, sleep, truncate, getBasicAuth }; +export { copyTextToClipboard, responsive, sleep, truncate, getBasicAuth, countWords };