diff --git a/ui/src/routes/books/[slug]/chapters/[n]/+page.svelte b/ui/src/routes/books/[slug]/chapters/[n]/+page.svelte index e28c1f5..854eefd 100644 --- a/ui/src/routes/books/[slug]/chapters/[n]/+page.svelte +++ b/ui/src/routes/books/[slug]/chapters/[n]/+page.svelte @@ -6,7 +6,15 @@ let { data }: { data: PageData } = $props(); - // ── Live-fetch fallback when chapter text is missing in storage ─────────── + // ── Word count ──────────────────────────────────────────────────────────── + function countWords(htmlStr: string | null): number { + if (!htmlStr) return 0; + // Strip HTML tags, collapse whitespace, split on whitespace + return htmlStr.replace(/<[^>]+>/g, ' ').trim().split(/\s+/).filter(Boolean).length; + } + + const wordCount = $derived(countWords(html)); + let html = $state(data.html); let fetchingContent = $state(!data.isPreview && !data.html); let fetchError = $state(''); @@ -89,7 +97,9 @@

{data.chapter.title || `Chapter ${data.chapter.number}`}

- + {#if wordCount > 0} +

{wordCount.toLocaleString()} words

+ {/if}