forked from shadowfacts/shadowfacts.net
Include word count in article metadata
This commit is contained in:
parent
ab20b01216
commit
8b7a65671c
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ export interface PostMetadata extends Metadata {
|
|||
category: string;
|
||||
date: string | Date;
|
||||
readingTime?: number;
|
||||
wordCount?: number;
|
||||
excerpt?: string;
|
||||
uuid: string;
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,6 @@
|
|||
<% } %>
|
||||
<% if (metadata.readingTime) { %>
|
||||
•
|
||||
<%= metadata.readingTime %> min read
|
||||
<span title="<%= metadata.wordCount %> words"><%= metadata.readingTime %> min read</span>
|
||||
<% } %>
|
||||
</p>
|
||||
|
|
Loading…
Reference in New Issue