Skip to content

Commit

Permalink
feat: add reading time for articles
Browse files Browse the repository at this point in the history
  • Loading branch information
GabsEdits committed Jul 22, 2024
1 parent cfd2ad1 commit 2df5243
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

- Make comments in articles optional

- Reading time for articles

- Make Numeric Headings a plugin (slightly breaking change)

## 2.5.0 (2024-07-21)
Expand Down
2 changes: 1 addition & 1 deletion Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const { site, frontmatter, page, theme } = useData();
<ArticleHead />
</div>
<NotFound v-if="page.isNotFound" />
<div v-else :class="frontmatter.pageClass">
<div v-else :class="frontmatter.pageClass" id="content">
<Content />
</div>
<template v-if="frontmatter.layout === 'helpful'">
Expand Down
20 changes: 20 additions & 0 deletions src/layouts/ArticleHead.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
}}<span v-if="index !== frontmatter.author.length - 1">, </span>
</template>
</span>
<span>
&middot; {{ readingTime }} minutes to read
</span>
<span class="tags">
&middot;
<span v-for="(tag, index) in frontmatter.tags" :key="index">{{
Expand All @@ -63,9 +66,26 @@

<script setup lang="ts">
import { useData } from "vitepress";
import { computed, onMounted, ref } from "vue";
const { frontmatter, theme } = useData();
const contentRef = ref<HTMLElement | null>(null);
const readingTime = ref<string>('');
onMounted(() => {
contentRef.value = document.getElementById('content');
if (contentRef.value) {

Check failure on line 78 in src/layouts/ArticleHead.vue

View workflow job for this annotation

GitHub Actions / format

'computed' is defined but never used
const words = contentRef.value.innerText.trim().split(/\s+/).length;
const wordsPerMinute = 200; // Average reading speed
const time = Math.ceil(words / wordsPerMinute);
readingTime.value = time.toString();
}

Check failure on line 83 in src/layouts/ArticleHead.vue

View workflow job for this annotation

GitHub Actions / format

'string' is not defined
});
</script>


<style lang="scss">
#article-head {
padding-top: 2.0938rem;
Expand Down

0 comments on commit 2df5243

Please sign in to comment.