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);
|
page.text = util.render(page.text, { metadata: page.metadata }, postPath);
|
||||||
|
|
||||||
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);
|
(<PostMetadata>page.metadata).wordCount = util.getWordCount(page.text);
|
||||||
|
(<PostMetadata>page.metadata).readingTime = util.getReadingTime((<PostMetadata>page.metadata).wordCount);
|
||||||
|
|
||||||
page.text = markdown.render(page.text);
|
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")) {
|
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);
|
page.text = markdown.render(page.text);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ export interface PostMetadata extends Metadata {
|
||||||
category: string;
|
category: string;
|
||||||
date: string | Date;
|
date: string | Date;
|
||||||
readingTime?: number;
|
readingTime?: number;
|
||||||
|
wordCount?: number;
|
||||||
excerpt?: string;
|
excerpt?: string;
|
||||||
uuid: string;
|
uuid: string;
|
||||||
}
|
}
|
||||||
|
@ -34,4 +35,4 @@ export async function get(path: string): Promise<Page> {
|
||||||
configure(metadata);
|
configure(metadata);
|
||||||
}
|
}
|
||||||
return {text, metadata};
|
return {text, metadata};
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,9 +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 {
|
export function getWordCount(text: string): number {
|
||||||
|
return text.split(/\s+/).length;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getReadingTime(words: number): number {
|
||||||
const avgWPM = 225;
|
const avgWPM = 225;
|
||||||
const words = text.split(/\s+/).length;
|
|
||||||
return Math.max(1, Math.round(words / avgWPM))
|
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, {
|
return ejs.render(template, data, {
|
||||||
filename
|
filename
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,6 @@
|
||||||
<% } %>
|
<% } %>
|
||||||
<% if (metadata.readingTime) { %>
|
<% if (metadata.readingTime) { %>
|
||||||
•
|
•
|
||||||
<%= metadata.readingTime %> min read
|
<span title="<%= metadata.wordCount %> words"><%= metadata.readingTime %> min read</span>
|
||||||
<% } %>
|
<% } %>
|
||||||
</p>
|
</p>
|
||||||
|
|
Loading…
Reference in New Issue