Display word count on chapter page
Some checks failed
Deploy / Deploy Production (push) Has been skipped
Deploy / Cleanup Preview (push) Has been skipped
Deploy / Deploy Preview (push) Failing after 1s
CI / Scraper / Test (pull_request) Successful in 9s
CI / UI / Build (pull_request) Failing after 14s
CI / Scraper / Lint (pull_request) Successful in 18s
CI / Scraper / Build (pull_request) Successful in 9s

This commit is contained in:
Admin
2026-03-06 20:36:27 +05:00
parent 314af375d5
commit 669fd765ee

View File

@@ -6,7 +6,15 @@
let { data }: { data: PageData } = $props(); 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 html = $state(data.html);
let fetchingContent = $state(!data.isPreview && !data.html); let fetchingContent = $state(!data.isPreview && !data.html);
let fetchError = $state(''); let fetchError = $state('');
@@ -89,7 +97,9 @@
<h1 class="text-xl font-bold text-zinc-100"> <h1 class="text-xl font-bold text-zinc-100">
{data.chapter.title || `Chapter ${data.chapter.number}`} {data.chapter.title || `Chapter ${data.chapter.number}`}
</h1> </h1>
{#if wordCount > 0}
<p class="text-zinc-600 text-xs mt-1">{wordCount.toLocaleString()} words</p>
{/if}
</div> </div>
<!-- Audio player --> <!-- Audio player -->