1
0
silverbullet/plugs/editor/stats.ts

22 lines
677 B
TypeScript
Raw Normal View History

2023-08-28 15:12:15 +00:00
import { editor, space } from "$sb/syscalls.ts";
function countWords(str: string): number {
const matches = str.match(/[\w\d\'-]+/gi);
return matches ? matches.length : 0;
}
function readingTime(wordCount: number): number {
// 225 is average word reading speed for adults
return Math.ceil(wordCount / 225);
}
export async function statsCommand() {
2022-10-14 13:11:33 +00:00
const text = await editor.getText();
const allPages = await space.listPages();
const wordCount = countWords(text);
const time = readingTime(wordCount);
2022-10-14 13:11:33 +00:00
await editor.flashNotification(
2023-02-11 13:19:02 +00:00
`${text.length} characters; ${wordCount} words; ${time} minutes read; ${allPages.length} total pages in space.`,
2022-08-01 16:27:50 +00:00
);
}