jekyll-toc-generator 0.1.0

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: c6d4565912821695402aa8731d39c5fa37db228a1d913a43b1b1c081541dbca8
4
+ data.tar.gz: d1eb16a8e95e4cd052d9974b03175a41bef067a2fa5959221930138375c8a24f
5
+ SHA512:
6
+ metadata.gz: 4d1d48cfe71db0b1a126ed5ea41c4c61b3aba641d9d9c2b536cd7aa46ff02d6fe75c124810970bcef33dde90553a4681f36417aae6ee877a2c7a65bc4783f67b
7
+ data.tar.gz: 585b4df874534717b07f4b948569cf3aa951bb2d5e691b501d2cf05a4913eab3b794611167831b356b11f21023ed1c643c677ee253de61d177e757b6f2eb1378
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,135 @@
1
+ # jekyll-toc-generator
2
+
3
+ Hook-based table of contents injection for Jekyll. Posts and pages with `toc: true` in frontmatter automatically get a TOC injected — no Liquid tag required in the content.
4
+
5
+ ## Installation
6
+
7
+ Add to your Jekyll site's `Gemfile`:
8
+
9
+ ```ruby
10
+ gem "jekyll-toc-generator"
11
+ ```
12
+
13
+ Then add to `_config.yml`:
14
+
15
+ ```yaml
16
+ plugins:
17
+ - jekyll-toc-generator
18
+ ```
19
+
20
+ Run `bundle install`.
21
+
22
+ ## Usage
23
+
24
+ Add `toc: true` to any post or page frontmatter:
25
+
26
+ ```yaml
27
+ ---
28
+ title: My Long Post
29
+ toc: true
30
+ ---
31
+ ```
32
+
33
+ The plugin automatically injects a `<nav class="toc">` element immediately before the first `<h2>` in the rendered HTML. No Liquid tag needed.
34
+
35
+ ## Configuration
36
+
37
+ All options are optional. Add to `_config.yml`:
38
+
39
+ ```yaml
40
+ toc:
41
+ min_headings: 2 # don't inject TOC if fewer than N h2s (default: 2)
42
+ title: "Contents" # TOC nav title (default: "Table of Contents")
43
+ levels: [2, 3] # heading levels to include (default: [2, 3])
44
+ ```
45
+
46
+ ### Options
47
+
48
+ | Option | Default | Description |
49
+ |---|---|---|
50
+ | `min_headings` | `2` | Minimum number of `<h2>` elements required to inject a TOC |
51
+ | `title` | `"Table of Contents"` | Text shown in the TOC `<h2>` title |
52
+ | `levels` | `[2, 3]` | Heading levels to include in the TOC |
53
+
54
+ ## TOC HTML Structure
55
+
56
+ ```html
57
+ <nav class="toc" aria-label="Table of contents">
58
+ <h2 class="toc__title">Table of Contents</h2>
59
+ <ul class="toc__list">
60
+ <li class="toc__item"><a href="#heading-slug">Heading Text</a>
61
+ <ul class="toc__sublist">
62
+ <li class="toc__item--sub"><a href="#sub-heading-slug">Sub Heading</a></li>
63
+ </ul>
64
+ </li>
65
+ </ul>
66
+ </nav>
67
+ ```
68
+
69
+ ### CSS Classes
70
+
71
+ | Class | Element | Description |
72
+ |---|---|---|
73
+ | `.toc` | `<nav>` | Outer TOC container |
74
+ | `.toc__title` | `<h2>` | TOC section title |
75
+ | `.toc__list` | `<ul>` | Top-level heading list |
76
+ | `.toc__item` | `<li>` | Top-level heading item |
77
+ | `.toc__sublist` | `<ul>` | Nested subheading list |
78
+ | `.toc__item--sub` | `<li>` | Subheading item |
79
+
80
+ ### Example CSS
81
+
82
+ ```css
83
+ .toc {
84
+ background: #f8f8f8;
85
+ border: 1px solid #ddd;
86
+ border-radius: 4px;
87
+ padding: 1rem 1.5rem;
88
+ margin-bottom: 2rem;
89
+ display: inline-block;
90
+ min-width: 200px;
91
+ }
92
+
93
+ .toc__title {
94
+ font-size: 1rem;
95
+ margin: 0 0 0.5rem;
96
+ font-weight: 600;
97
+ }
98
+
99
+ .toc__list,
100
+ .toc__sublist {
101
+ list-style: none;
102
+ margin: 0;
103
+ padding: 0;
104
+ }
105
+
106
+ .toc__sublist {
107
+ padding-left: 1rem;
108
+ }
109
+
110
+ .toc__item,
111
+ .toc__item--sub {
112
+ margin: 0.25rem 0;
113
+ }
114
+ ```
115
+
116
+ ## Slug Generation
117
+
118
+ Heading IDs are slugified to match Jekyll's default anchor generation:
119
+
120
+ - Lowercase
121
+ - Non-alphanumeric characters removed
122
+ - Spaces and underscores replaced with `-`
123
+ - Leading/trailing hyphens stripped
124
+
125
+ Example: `"What's New?"` → `#whats-new`
126
+
127
+ ## Requirements
128
+
129
+ - Jekyll >= 4.0
130
+ - Ruby >= 2.7
131
+ - No additional gem dependencies (pure Ruby, no Nokogiri)
132
+
133
+ ## License
134
+
135
+ MIT © Jason Chance 2025
@@ -0,0 +1,10 @@
1
+ require "jekyll-toc-generator/version"
2
+ require "jekyll-toc-generator/toc_builder"
3
+
4
+ Jekyll::Hooks.register [:posts, :pages, :documents], :post_render do |doc|
5
+ next unless doc.data["toc"] == true
6
+
7
+ config = doc.site.config["toc"] || {}
8
+ builder = Jekyll::TocGenerator::TocBuilder.new(doc.output, config)
9
+ doc.output = builder.build
10
+ end
@@ -0,0 +1,101 @@
1
+ module Jekyll
2
+ module TocGenerator
3
+ class TocBuilder
4
+ def initialize(html, config = {})
5
+ @html = html
6
+ @min_headings = config["min_headings"] || 2
7
+ @title = config["title"] || "Table of Contents"
8
+ @levels = config["levels"] || [2, 3]
9
+ end
10
+
11
+ def build
12
+ return @html if @html.nil? || @html.empty?
13
+
14
+ headings = extract_headings
15
+ h2s = headings.select { |h| h[:level] == 2 }
16
+ return @html if h2s.size < @min_headings
17
+
18
+ toc_html = build_toc(headings)
19
+ inject_toc(toc_html)
20
+ end
21
+
22
+ private
23
+
24
+ def extract_headings
25
+ levels_pattern = @levels.map(&:to_s).join
26
+ headings = []
27
+ @html.scan(/<h([#{levels_pattern}])[^>]*>(.*?)<\/h\1>/im) do |level, content|
28
+ text = strip_tags(content)
29
+ headings << { level: level.to_i, text: text, slug: slugify(text) }
30
+ end
31
+ headings
32
+ end
33
+
34
+ def strip_tags(html)
35
+ html.gsub(/<[^>]+>/, "").strip
36
+ end
37
+
38
+ def slugify(text)
39
+ text.downcase.gsub(/[^\w\s-]/, "").gsub(/[\s_]+/, "-").gsub(/^-+|-+$/, "")
40
+ end
41
+
42
+ def build_toc(headings)
43
+ items_html = build_items(headings)
44
+ <<~HTML
45
+ <nav class="toc" aria-label="Table of contents">
46
+ <h2 class="toc__title">#{@title}</h2>
47
+ <ul class="toc__list">
48
+ #{items_html.chomp}
49
+ </ul>
50
+ </nav>
51
+ HTML
52
+ end
53
+
54
+ def build_items(headings)
55
+ html = ""
56
+ current_h2 = nil
57
+ pending_subs = []
58
+
59
+ flush = lambda do
60
+ if current_h2
61
+ html += " <li class=\"toc__item\"><a href=\"##{current_h2[:slug]}\">#{current_h2[:text]}</a>"
62
+ unless pending_subs.empty?
63
+ html += "\n <ul class=\"toc__sublist\">\n"
64
+ pending_subs.each do |sub|
65
+ html += " <li class=\"toc__item--sub\"><a href=\"##{sub[:slug]}\">#{sub[:text]}</a></li>\n"
66
+ end
67
+ html += " </ul>\n "
68
+ end
69
+ html += "</li>\n"
70
+ pending_subs = []
71
+ current_h2 = nil
72
+ end
73
+ end
74
+
75
+ headings.each do |h|
76
+ if h[:level] == 2
77
+ flush.call
78
+ current_h2 = h
79
+ else
80
+ if current_h2
81
+ pending_subs << h
82
+ else
83
+ # h3 with no preceding h2 — treat as top-level
84
+ html += " <li class=\"toc__item\"><a href=\"##{h[:slug]}\">#{h[:text]}</a></li>\n"
85
+ end
86
+ end
87
+ end
88
+
89
+ flush.call
90
+ html
91
+ end
92
+
93
+ def inject_toc(toc_html)
94
+ first_h2 = @html.match(/<h2[^>]*>/i)
95
+ return @html unless first_h2
96
+
97
+ @html.sub(first_h2[0], toc_html.chomp + first_h2[0])
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module TocGenerator
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ require "jekyll-toc-generator/version"
2
+ require "jekyll-toc-generator/toc_builder"
3
+ require "jekyll/toc_generator"
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-toc-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
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
+ email:
27
+ - jason@jasonchance.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - LICENSE.txt
33
+ - README.md
34
+ - lib/jekyll-toc-generator.rb
35
+ - lib/jekyll-toc-generator/toc_builder.rb
36
+ - lib/jekyll-toc-generator/version.rb
37
+ - lib/jekyll/toc_generator.rb
38
+ homepage: https://github.com/jchance/jekyll-toc-generator
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '2.7'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.6.2
57
+ specification_version: 4
58
+ summary: Hook-based table of contents injection for Jekyll — no Liquid tag needed.
59
+ test_files: []