Skip to content

Commit

Permalink
Feature - Add article readtime (denoland#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLCarveth authored Sep 13, 2022
1 parent 427cdc6 commit 36cc6c3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ async function loadPost(postsDirectory: string, path: string) {
ogImage: data.get("og:image"),
tags: data.get("tags"),
allowIframes: data.get("allow_iframes"),
readTime: readingTime(content),
};
POSTS.set(pathname, post);
console.log("Load: ", post.pathname);
}

export async function handler(
Expand Down Expand Up @@ -543,3 +543,9 @@ function recordGetter(data: Record<string, unknown>) {
},
};
}

function readingTime(text: string) {
const wpm = 225;
const words = text.split(/\s+/).length;
return Math.ceil(words / wpm);
}
2 changes: 2 additions & 0 deletions blog_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const BLOG_SETTINGS = await configureBlog(BLOG_URL, false, {
"second.html": "second",
}),
],
readtime: true,
});
const CONN_INFO = {
localAddr: {
Expand Down Expand Up @@ -70,6 +71,7 @@ Deno.test("posts/ first", async () => {
assertStringIncludes(body, `<img src="first/hello.png" />`);
assertStringIncludes(body, `<p>Lorem Ipsum is simply dummy text`);
assertStringIncludes(body, `$100, $200, $300, $400, $500`);
assertStringIncludes(body, `min read`);
});

Deno.test("posts/ second", async () => {
Expand Down
2 changes: 2 additions & 0 deletions components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ export function PostPage({ post, state }: PostPageProps) {
<h1 class="text-4xl text-gray-900 dark:text-gray-100 font-bold">
{post.title}
</h1>
{state.readtime &&
<p>{post.readTime} min read</p>}
<Tags tags={post.tags} />
<p class="mt-1 text-gray-500">
{(post.author || state.author) && (
Expand Down
27 changes: 27 additions & 0 deletions testdata/posts/third.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,30 @@ typesetting, remaining essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software like Aldus PageMaker including
versions of Lorem Ipsum.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a type specimen
book. It has survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software like Aldus PageMaker including
versions of Lorem Ipsum.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a type specimen
book. It has survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software like Aldus PageMaker including
versions of Lorem Ipsum.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a type specimen
book. It has survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing Lorem Ipsum passages, and more
recently with desktop publishing software like Aldus PageMaker including
versions of Lorem Ipsum.
3 changes: 3 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export interface BlogSettings {
port?: number;
/** The hostname to serve the blog on */
hostname?: string;
/** Whether to display readtime or not */
readtime?: boolean;
}

export interface BlogState extends BlogSettings {
Expand All @@ -95,4 +97,5 @@ export interface Post {
ogImage?: string;
tags?: string[];
allowIframes?: boolean;
readTime: number;
}

0 comments on commit 36cc6c3

Please sign in to comment.