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;
|
||||
}
|
||||
|
@ -34,4 +35,4 @@ export async function get(path: string): Promise<Page> {
|
|||
configure(metadata);
|
||||
}
|
||||
return {text, metadata};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
@ -52,4 +55,4 @@ export function render(template: string, data: any, filename?: string): string {
|
|||
return ejs.render(template, data, {
|
||||
filename
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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