jekyll-smart-reading-time 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aa31644aaebee41f97dc831c82d9b73af771d5ea675ed3a097ff4fa5da30974a
4
+ data.tar.gz: 3cca14536c0a78344b609ed0abef31c60051d2d096869e11e104cbcf82233dea
5
+ SHA512:
6
+ metadata.gz: d33553d2dd8ca3dc7a0c109d663142ba0496107f5b7a93c7ab5735ad3bcb35e226dbb5f483a173051c3aa6fcdf3e7d7c4a9bbb1bf91195186398af23d9114c43
7
+ data.tar.gz: e08136b9fbd990037a2e1e292f0f5bd64c09b1e9eabcd3d018b5b61178dc18bb2aa0b49be4903b1ee4f67ee7c79e63ab13865b4c77d7bac39974aa61a15c7b9f
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Jason Chance
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # jekyll-smart-reading-time
2
+
3
+ Smart reading time estimation for Jekyll — strips code blocks before counting.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/jekyll-smart-reading-time.svg)](https://badge.fury.io/rb/jekyll-smart-reading-time)
6
+
7
+ Most existing Jekyll reading-time gems are abandoned and count everything — including
8
+ hundreds of lines of code samples — as prose. This gem strips fenced code blocks,
9
+ indented code blocks, HTML tags, and Liquid tags before counting words, so the
10
+ estimate actually reflects reading time for your *content*.
11
+
12
+ ## Why not the others?
13
+
14
+ | Gem | Last release | Strips code? |
15
+ |-----|-------------|--------------|
16
+ | `jekyll-smart-reading-time` (this gem) | 2025 | ✅ Yes |
17
+ | `jekyll-timetoread` | 2014 | ❌ No |
18
+ | `reading_time` | 2013 | ❌ No |
19
+
20
+ ## Installation
21
+
22
+ Add to your site's `Gemfile`:
23
+
24
+ ```ruby
25
+ gem "jekyll-smart-reading-time"
26
+ ```
27
+
28
+ Then add to `_config.yml`:
29
+
30
+ ```yaml
31
+ plugins:
32
+ - jekyll-smart-reading-time
33
+ ```
34
+
35
+ Run:
36
+
37
+ ```bash
38
+ bundle install
39
+ ```
40
+
41
+ ## Usage
42
+
43
+ ### In templates (recommended)
44
+
45
+ The gem automatically injects `page.reading_time` into every post and page:
46
+
47
+ ```liquid
48
+ {{ page.reading_time }} min read
49
+ ```
50
+
51
+ Example in a post layout:
52
+
53
+ ```html
54
+ <span class="reading-time">{{ page.reading_time }} min read</span>
55
+ ```
56
+
57
+ ### Liquid tag
58
+
59
+ You can also use the `{% reading_time %}` tag, which renders `"X min read"` directly:
60
+
61
+ ```liquid
62
+ {% reading_time %}
63
+ ```
64
+
65
+ ## Configuration
66
+
67
+ Override the default 238 WPM in `_config.yml`:
68
+
69
+ ```yaml
70
+ reading_time:
71
+ wpm: 200
72
+ ```
73
+
74
+ The default of **238 WPM** is drawn from [research on average adult reading speed](https://journals.sagepub.com/doi/10.1177/0956797617738271).
75
+
76
+ ## What gets stripped
77
+
78
+ Before counting words, the following are removed:
79
+
80
+ 1. YAML frontmatter (`---` blocks)
81
+ 2. Fenced code blocks (`` ``` ... ``` ``)
82
+ 3. Indented code blocks (lines starting with 4+ spaces or a tab)
83
+ 4. HTML tags (`<...>`)
84
+ 5. Liquid tags (`{{ ... }}` and `{% ... %}`)
85
+
86
+ ## License
87
+
88
+ [MIT](LICENSE.txt) © 2025 Jason Chance
@@ -0,0 +1,55 @@
1
+ require "jekyll"
2
+
3
+ module Jekyll
4
+ # Generator that injects `page.reading_time` (integer minutes) into every
5
+ # post and page before Jekyll renders them.
6
+ class ReadingTimeGenerator < Jekyll::Generator
7
+ priority :low
8
+
9
+ def generate(site)
10
+ wpm = site.config.dig("reading_time", "wpm") || 238
11
+
12
+ all_docs = site.posts.docs + site.pages
13
+ site.collections.each_value { |col| all_docs += col.docs unless col.label == "posts" }
14
+ all_docs.each do |doc|
15
+ next unless doc.respond_to?(:content) && doc.content
16
+ doc.data["reading_time"] = reading_time(doc.content, wpm)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def reading_time(content, wpm = 238)
23
+ text = content.dup
24
+
25
+ # 1. Strip YAML frontmatter
26
+ text.gsub!(/\A---.*?---\s*/m, "")
27
+
28
+ # 2. Strip fenced code blocks (``` ... ```)
29
+ text.gsub!(/```.*?```/m, "")
30
+
31
+ # 3. Strip indented code blocks (lines starting with 4+ spaces or a tab)
32
+ text.gsub!(/^( {4,}|\t).*$/, "")
33
+
34
+ # 4. Strip HTML tags
35
+ text.gsub!(/<[^>]+>/, "")
36
+
37
+ # 5. Strip Liquid tags ({% ... %} and {{ ... }})
38
+ text.gsub!(/\{[{%].*?[%}]\}/m, "")
39
+
40
+ words = text.split.length
41
+ [[(words / wpm.to_f).ceil, 1].max, 1].max
42
+ end
43
+ end
44
+
45
+ # Liquid tag: {% reading_time %} — renders "X min read" for the current page.
46
+ class ReadingTimeTag < Liquid::Tag
47
+ def render(context)
48
+ page = context["page"]
49
+ minutes = page && page["reading_time"]
50
+ minutes ? "#{minutes} min read" : "1 min read"
51
+ end
52
+ end
53
+ end
54
+
55
+ Liquid::Template.register_tag("reading_time", Jekyll::ReadingTimeTag)
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module SmartReadingTime
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "jekyll-smart-reading-time/version"
2
+ require "jekyll/reading_time_generator"
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-smart-reading-time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason Chance
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2026-08-21 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: jekyll
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '4.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '4.0'
26
+ description: Injects page.reading_time into every Jekyll post and page. Strips fenced
27
+ code blocks, indented code, HTML tags, and Liquid tags before counting words at
28
+ 238 WPM (configurable).
29
+ email:
30
+ - jason@jasonchance.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE.txt
36
+ - README.md
37
+ - lib/jekyll-smart-reading-time.rb
38
+ - lib/jekyll-smart-reading-time/version.rb
39
+ - lib/jekyll/reading_time_generator.rb
40
+ homepage: https://github.com/jchance/jekyll-smart-reading-time
41
+ licenses:
42
+ - MIT
43
+ metadata: {}
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '2.7'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.6.2
59
+ specification_version: 4
60
+ summary: Smart reading time estimation for Jekyll — strips code blocks before counting.
61
+ test_files: []