Skip to content

fix(word-count): treat apostrophes as part of a word - #339

Closed
MsfPablo wants to merge 1 commit into
kevboh:mainfrom
MsfPablo:fix/apostrophe-word-count
Closed

fix(word-count): treat apostrophes as part of a word#339
MsfPablo wants to merge 1 commit into
kevboh:mainfrom
MsfPablo:fix/apostrophe-word-count

Conversation

@MsfPablo

@MsfPablo MsfPablo commented Aug 3, 2026

Copy link
Copy Markdown

Summary

Fixes #324. WORD_COUNT_REGEX didn't include the apostrophe character, so contractions like "can't" were matched as two separate words ("can" and "t") instead of one, inflating the word count.

  • Add ' alongside the existing \- in the word-character class (the "Option A" fix suggested in the issue), so contractions and possessives count as a single word — consistent with how hyphenated words are already handled by the same regex.

Test plan

Verified the regex change against a handful of cases (contractions, possessives, hyphenated words, numbers with commas/decimals) — all count as expected, no regressions:

Input Before After
Can't 2 1
I can't won't hasn't 7 4
It's Sarah's book 5 3
This is a test 4 4
well-known fact 2 2
There are 1,000 words and 3.14 pi 7 7

I initially added a unit test under test/model/, but writing-session-tracker.ts imports from obsidian (a types-only package with no runtime entry), which vitest can't resolve outside the Obsidian runtime — the same reason the existing test/compile/steps/remove-links.test.ts only covers files that don't import obsidian. Happy to add a test if you'd like the word-counting logic split out into a pure module that could import cleanly.

WORD_COUNT_REGEX didn't include the apostrophe character, so
contractions like "can't" were split into two matches ("can" and "t")
and counted as two words instead of one.

Add ' alongside the existing hyphen in the word-character class, so
contractions and possessives ("can't", "Sarah's") count as a single
word, consistent with how hyphenated words are already handled.

Fixes kevboh#324
@b-camphart

Copy link
Copy Markdown
Contributor

FYI #338

@MsfPablo

MsfPablo commented Aug 3, 2026

Copy link
Copy Markdown
Author

Closing in favor of #338, which fixes this same issue more completely (also catches a second real bug in the HTML-comment-stripping order) and has proper test coverage via a mocked obsidian module — something I wasn't able to get working in my own test setup. Thanks for the more thorough fix!

@MsfPablo MsfPablo closed this Aug 3, 2026
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.

Bug: Apostrophes break word count

2 participants