display word counts

This commit is contained in:
2026-02-21 09:44:02 +10:00
parent e21d1c7b58
commit 9cf57fe508
7 changed files with 96 additions and 4 deletions

View File

@@ -69,3 +69,33 @@ export async function writeMarkdownFile(filePath: string, content: string): Prom
assertInDraftRoot(filePath)
await writeFile(filePath, content, 'utf-8')
}
function countWords(text: string): number {
return text.trim() === '' ? 0 : text.trim().split(/\s+/).length
}
async function collectMarkdownPaths(dir: string): Promise<string[]> {
const entries = await readdir(dir, { withFileTypes: true })
const paths: string[] = []
for (const entry of entries) {
if (entry.name.startsWith('.')) continue
const fullPath = join(dir, entry.name)
if (entry.isDirectory()) {
const nested = await collectMarkdownPaths(fullPath)
paths.push(...nested)
} else if (entry.name.endsWith('.md')) {
paths.push(fullPath)
}
}
return paths
}
export async function getProjectWordCount(): Promise<number> {
const paths = await collectMarkdownPaths(DRAFT_ROOT)
let total = 0
for (const p of paths) {
const content = await readFile(p, 'utf-8')
total += countWords(content)
}
return total
}

View File

@@ -1,5 +1,5 @@
import { ipcMain } from 'electron'
import { listDraftFiles, readMarkdownFile, writeMarkdownFile } from './fileSystem'
import { listDraftFiles, readMarkdownFile, writeMarkdownFile, getProjectWordCount } from './fileSystem'
import { streamMessage } from './aiService'
import type { AIPayload } from '../renderer/types/editor'
@@ -16,6 +16,10 @@ export function registerIpcHandlers(): void {
await writeMarkdownFile(filePath, content)
})
ipcMain.handle('fs:projectWordCount', async () => {
return await getProjectWordCount()
})
ipcMain.handle('ai:streamMessage', async (event, payload: AIPayload) => {
try {
await streamMessage(payload, (chunk: string) => {