Repository navigation
[ReadingTime] Count words separated by any white-space - #576
Open
NopeCharon wants to merge 1 commit into
Open
NopeCharon wants to merge 1 commit into
NopeCharon wants to merge 1 commit into
Conversation
The word count split the text content of the article on spaces only, after trimming it and collapsing runs of spaces. Words separated by new lines or tabs were therefore counted as a single word: the last word of a paragraph or list item and the first word of the next one, every line of hard-wrapped text, indented code... This underestimated the reading time (e.g. 8 instead of 11 words for four short hard-wrapped lines), and an empty article counted as one word. Count the sequences of non-white-space characters instead, which treats every kind of white-space (spaces, new lines, tabs, non-breaking spaces) as a word separator and counts 0 words for an empty article. The reading time of an empty article stays "<1 m". Bump the extension to 1.6.3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The words metric split the text content of the article on spaces only (after trimming it and collapsing runs of spaces), so words separated by a new line or a tab were counted as one word. This happens between the last word of a paragraph or list item and the first word of the next one when the HTML has new lines between the tags, and on every line of hard-wrapped text or indented code, so the reading time was underestimated. An empty article also counted as one word.
Changes:
/\S+/g) instead, so any white-space (space, new line, tab, non-breaking space) separates words, and an empty article counts 0 words (its reading time stays<1 m)The letters metric is unchanged.
Validation
Old and new counting functions on sample text contents:
The quick brown fox jumps over the lazy dog.\n<p>One two three.</p>\n<p>Four five six.</p>\nLorem ipsum dolor\nsit amet, consectetur\nadipiscing elit,\nsed do eiusmodif (a) {\n\treturn b;\n}\n\tfoo(bar);\n <li>alpha beta</li>\n <li>gamma delta</li>\n <li>epsilon zeta</li>\n(HTML tags shown for context; the extension counts the text content.)
eslintandmarkdownlintpass.