Include word count in article metadata

This commit is contained in:
Shadowfacts 2019-11-11 21:28:52 -05:00
parent ab20b01216
commit 8b7a65671c
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 13 additions and 7 deletions

View File

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

View File

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

View File

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

View File

@ -12,9 +12,12 @@ export async function write(filePath: string, data: any) {
await fs.writeFile(dest, data);
}
export function getReadingTime(text: string): number {
export function getWordCount(text: string): number {
return text.split(/\s+/).length;
}
export function getReadingTime(words: number): number {
const avgWPM = 225;
const words = text.split(/\s+/).length;
return Math.max(1, Math.round(words / avgWPM))
}

View File

@ -17,6 +17,6 @@
<% } %>
<% if (metadata.readingTime) { %>
<%= metadata.readingTime %> min read
<span title="<%= metadata.wordCount %> words"><%= metadata.readingTime %> min read</span>
<% } %>
</p>