Add reading time estimate

This commit is contained in:
Shadowfacts 2019-06-29 16:01:05 -04:00
parent d660445899
commit 0c66619f0f
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 19 additions and 3 deletions

View File

@ -23,6 +23,8 @@ export default async function posts(): Promise<Page[]> {
} }
if (page.metadata.source && page.metadata.source!.endsWith(".md")) { if (page.metadata.source && page.metadata.source!.endsWith(".md")) {
(<PostMetadata>page.metadata).readingTime = util.getReadingTime(page.text);
page.text = markdown.render(page.text); page.text = markdown.render(page.text);
} }

View File

@ -23,6 +23,8 @@ async function generateTutorials(group: string): Promise<Page[]> {
} }
if (page.metadata.source && page.metadata.source!.endsWith(".md")) { if (page.metadata.source && page.metadata.source!.endsWith(".md")) {
(<PostMetadata>page.metadata).readingTime = util.getReadingTime(page.text);
page.text = markdown.render(page.text); page.text = markdown.render(page.text);
} }

View File

@ -17,6 +17,7 @@ export interface PostMetadata extends Metadata {
slug: string; slug: string;
category: string; category: string;
date: string | Date; date: string | Date;
readingTime?: number;
excerpt?: string; excerpt?: string;
uuid: string; uuid: string;
} }

View File

@ -12,6 +12,12 @@ export async function write(filePath: string, data: any) {
await fs.writeFile(dest, data); await fs.writeFile(dest, data);
} }
export function getReadingTime(text: string): number {
const avgWPM = 225;
const words = text.split(/\s+/).length;
return Math.max(1, Math.round(words / avgWPM))
}
export function highlight(source: string, language?: string): string { export function highlight(source: string, language?: string): string {
const res = language ? hljs.highlight(language, source) : hljs.highlightAuto(source); const res = language ? hljs.highlight(language, source) : hljs.highlightAuto(source);
const highlighted = res.value; const highlighted = res.value;

View File

@ -58,7 +58,6 @@ article {
color: var(--secondary-ui-text-color); color: var(--secondary-ui-text-color);
font-family: $monospace; font-family: $monospace;
font-size: 14px; font-size: 14px;
text-decoration: none !important;
} }
a { text-decoration: none; } a { text-decoration: none; }
} }
@ -122,6 +121,9 @@ article {
font-size: 14px; font-size: 14px;
font-weight: lighter; font-weight: lighter;
color: var(--secondary-ui-text-color); color: var(--secondary-ui-text-color);
a { text-decoration: underline; }
a::before, a::after { content: ""; }
} }
.article-content { .article-content {

View File

@ -1,4 +1,5 @@
<p class="article-meta"> <p class="article-meta">
<meta itemprop="author" value="Shadowfacts">
on on
<span> <span>
<% const formatted = formatDate(metadata.date, "MMM Do, YYYY") %> <% const formatted = formatDate(metadata.date, "MMM Do, YYYY") %>
@ -11,6 +12,8 @@
in in
<span itemprop="articleSection"><a href="/tutorials/<%= metadata.series %>" rel="category"><%= metadata.seriesName %></a></span> <span itemprop="articleSection"><a href="/tutorials/<%= metadata.series %>" rel="category"><%= metadata.seriesName %></a></span>
<% } %> <% } %>
by <% if (metadata.readingTime) { %>
<span itemprop="author">Shadowfacts</span>
<%= metadata.readingTime %> min read
<% } %>
</p> </p>