Skip to content

Commit

Permalink
Add reading time to posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Richard committed Jul 8, 2024
1 parent 69dd4a6 commit e4171c4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"pagefind": "^1.1.0",
"posthtml": "^0.16.6",
"posthtml-match-helper": "^2.0.0",
"reading-time": "^1.5.0",
"requestidlecallback": "^0.3.0",
"shiki": "^1.10.0",
"slugify": "^1.6.6",
Expand Down
7 changes: 7 additions & 0 deletions site/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions site/src/components/ReadingTime.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script lang="ts">
import readingTime from 'reading-time';
import { toPlainText } from 'astro-portabletext/utils';
import type { PortableTextBlock } from '@sanity/types';
export let body: PortableTextBlock[];
export let label = 'Reading time';
export let duration = `{{n}} minutes`;
export let wrapper = 'div';
const { minutes } = readingTime(toPlainText(body));
const length = duration.replace('{{n}}', `${Math.ceil(minutes)}`);
</script>

<svelte:element this={wrapper} class="extras-section">
<h4 class="type--label">{label}</h4>
<p class="type--h6">{length}</p>
</svelte:element>

<style>
.type--h6 {
font-weight: 400;
}
</style>
3 changes: 3 additions & 0 deletions site/src/layouts/views/Post.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import YouTube from '$components/YouTube.svelte';
import type { PostHeroProps } from '$components/PostHero.svelte';
import Text from '$components/Text.astro';
import { buildTOC } from '$lib/portabletext';
import ReadingTime from '$components/ReadingTime.svelte';
import type { Post, Microcopy, Share } from '$types/sanity';
export interface Props {
Expand Down Expand Up @@ -102,6 +103,8 @@ const showUpdated =
<Fragment slot="extras">
<!-- Author Info -->
<Authors {...authors} />
<!-- Reading Time -->
<ReadingTime body={post.body} />
<!-- Published date info -->
<Published
locale={locale.code}
Expand Down

0 comments on commit e4171c4

Please sign in to comment.