Skip to content

fix: derive word_count from the content actually resolved - #22

Merged
ttpears merged 1 commit into
mainfrom
fix/word-count-wysiwyg
Aug 27, 2026
Merged

fix: derive word_count from the content actually resolved#22
ttpears merged 1 commit into
mainfrom
fix/word-count-wysiwyg

Conversation

@ttpears

@ttpears ttpears commented Aug 27, 2026

Copy link
Copy Markdown
Owner

enhancePageResponse derived word_count from page.text, which BookStack only populates for markdown-authored pages. Every WYSIWYG page reported word_count: 0 next to a content_total_chars in the thousands — #10 has one at 6393 chars reading as empty.

countWords() falls back textmarkdown → tag-stripped html, and splits on /\s+/ rather than ' '. The second half is a separate bug the first exposed: newline-separated text on a markdown page counted as one word.

Also documents the Export Content role permission that the WYSIWYG markdown fallback in getPage depends on. Without it the export call throws, the catch logs to stderr, and the page reads as empty — the reporter hit this on a read-only Viewer token.

What a reviewer should check

  • src/util/word-count.ts — the fallback order. text first preserves existing behaviour for markdown pages; html is last because it needs stripping.
  • stripHtml drops script/style bodies and  , nothing else. Other entities count as part of whatever token they sit in — approximate by design.
  • enhancePageResponse passes the whole page, so the count no longer depends on the requested format. A page now reports the same word_count whether it was fetched as markdown or html.

Not covered

No end-to-end run against a live instance. The tests feed enhancePageResponse's input shape (text: '' with a populated markdown) rather than a real WYSIWYG page.

type-check / build / test pass — 17 tests, up from 9.

Closes #10.

BookStack only populates page.text for markdown-authored pages, so
enhancePageResponse reported word_count: 0 for every WYSIWYG page even when it
returned thousands of characters of content. Fall back to markdown, then to
tag-stripped html.

Splitting on /\s+/ rather than a single space also corrects the count for
newline-separated text on markdown-authored pages, which previously read as one
word.

Documents the Export Content role permission the WYSIWYG markdown fallback
needs; without it the fallback fails silently and the page reads as empty.
@ttpears
ttpears merged commit 468d4e2 into main Aug 27, 2026
3 checks passed
@ttpears
ttpears deleted the fix/word-count-wysiwyg branch August 27, 2026 02:31
Ashton210z added a commit to Ashton210z/bookstack-mcp that referenced this pull request Sep 4, 2026
Brings in two fixes this fork was missing:

- ttpears#22: word_count is derived from the content actually resolved, instead
  of always reporting 0.
- ttpears#9: search filters {created_by:X}/{updated_by:X}/{owned_by:X} take a
  user slug or 'me' rather than a numeric ID, and say so when given one.

Both sides had independently added update_book (client method and tool)
and update_chapter. The implementations were equivalent, so the
duplicates from upstream were dropped and this fork's kept — they carry
the "Optional:" describe() prefix used throughout the file, and
update_chapter retains the book_id/priority arguments for moving and
reordering, which upstream's version does not have.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YadjrGWa6jL6vu2Fd8eJna
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

word_count is always 0 for WYSIWYG pages, even when content is returned

1 participant