Skip to content

Commit 78f21d4

Browse files
committed
- Add optional in-document Table of Contents
1 parent bb67662 commit 78f21d4

9 files changed

Lines changed: 99 additions & 4 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ group :development do
99
# gem 'github-pages', group: :jekyll_plugins
1010

1111
gem 'byebug'
12+
gem 'lp'
1213
gem 'rack-test'
1314
gem 'rdoc'
1415
gem 'rspec'

lib/madness.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
require 'byebug' if ENV['BYEBUG']
1+
if ENV['BYEBUG']
2+
require 'byebug'
3+
require 'lp'
4+
end
25

36
require 'requires'
47

lib/madness/document.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def markdown
9999
def markdown_to_html
100100
doc = CommonMarker.render_doc markdown, :DEFAULT, [:table]
101101

102+
replace_toc_marker doc
102103
add_anchor_ids doc
103104
html = doc.to_html :UNSAFE
104105
html = syntax_highlight(html) if config.highlighter
@@ -121,6 +122,34 @@ def add_anchor_ids(doc)
121122
end
122123
end
123124

125+
# Replace <!-- TOC --> with a Table of Contents for the page
126+
def replace_toc_marker(doc)
127+
toc_marker = doc.find do |node|
128+
node.type == :html and node.string_content.include? "<!-- TOC -->"
129+
end
130+
131+
return unless toc_marker
132+
133+
toc = []
134+
doc.walk do |node|
135+
next unless node.type == :header
136+
level = node.header_level
137+
next unless level.between? 2, 3
138+
text = node.first_child.string_content
139+
140+
if level == 2
141+
toc << "- [#{text}](##{text.to_slug})"
142+
else
143+
toc << " - [#{text}](##{text.to_slug})"
144+
end
145+
end
146+
147+
toc = toc.join "\n"
148+
toc = CommonMarker.render_doc toc
149+
toc_marker.insert_after toc.first_child
150+
toc_marker.insert_after CommonMarker.render_doc("## Table of Contents").first_child
151+
end
152+
124153
# If the document does not start with an H1 tag, add it.
125154
def prepend_h1(html)
126155
unless html[0..3] == "<h1>"

madness.gemspec

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ Gem::Specification.new do |s|
2525
s.add_runtime_dependency 'json', '~> 2.1'
2626
s.add_runtime_dependency 'naturally', '~> 2.2'
2727
s.add_runtime_dependency 'os', '~> 1.0'
28-
s.add_runtime_dependency 'puma', '~> 5.0'
29-
# s.add_runtime_dependency 'rack-contrib', '~> 2.1'
28+
s.add_runtime_dependency 'puma', '~> 4.0'
3029
s.add_runtime_dependency 'rack-ssl', '~> 1.4'
3130
s.add_runtime_dependency 'requires', '~> 0.1'
3231
s.add_runtime_dependency 'sass', '~> 3.7'
3332
s.add_runtime_dependency 'sinatra', '~> 2.0', '>= 2.0.5'
34-
# s.add_runtime_dependency 'sinatra-contrib', '~> 2.0', '>= 2.0.5'
3533
s.add_runtime_dependency 'slim', '~> 4.0'
3634
s.add_runtime_dependency 'extended_yaml', '~> 0.2'
3735
s.add_runtime_dependency 'addressable', '~> 2.7'

out.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Table of Contents
2+
3+
- [Purpose](#purpose)
4+
- [Scope](#scope)
5+
- [Background](#background)
6+
- [Exceptions](#exceptions)
7+
- [Policy](#policy)
8+
- [Phase 1: Preliminary analysis](#phase-1-preliminary-analysis)
9+
- [Phase 2: Systems analysis and requirements definition](#phase-2-systems-analysis-and-requirements-definition)
10+
- [Phase 3: Systems design](#phase-3-systems-design)
11+
- [Phase 4: Development](#phase-4-development)
12+
- [Phase 5: Integration and testing](#phase-5-integration-and-testing)
13+
- [Phase 6: Acceptance and deployment](#phase-6-acceptance-and-deployment)
14+
- [Phase 7: Maintenance plan](#phase-7-maintenance-plan)
15+
- [Appendix A - Security aspects](#appendix-a---security-aspects)
16+
- [Appendix B - Dependency upgrade guidelines](#appendix-b---dependency-upgrade-guidelines)
17+
- [Upgrading vulnerable dependencies](#upgrading-vulnerable-dependencies)
18+
- [Upgrading high-impact dependencies](#upgrading-high-impact-dependencies)
19+
- [Upgrading low-impact dependencies](#upgrading-low-impact-dependencies)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# File with TOC
2+
3+
<!-- TOC -->
4+
5+
## Subject 1
6+
7+
Some text
8+
9+
### Subject 1.1
10+
11+
Some text
12+
13+
### Subject 1.2
14+
15+
Some text
16+
17+
## Subject 2
18+
19+
Some text

spec/fixtures/document-toc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<h1><a id='file-with-toc'></a>File with TOC</h1>
2+
<!-- TOC -->
3+
<h2><a id='table-of-contents'></a>Table of Contents</h2>
4+
<ul>
5+
<li><a href="#subject-1">Subject 1</a>
6+
<ul>
7+
<li><a href="#subject-11">Subject 1.1</a></li>
8+
<li><a href="#subject-12">Subject 1.2</a></li>
9+
</ul>
10+
</li>
11+
<li><a href="#subject-2">Subject 2</a></li>
12+
</ul>
13+
<h2><a id='subject-1'></a>Subject 1</h2>
14+
<p>Some text</p>
15+
<h3><a id='subject-11'></a>Subject 1.1</h3>
16+
<p>Some text</p>
17+
<h3><a id='subject-12'></a>Subject 1.2</h3>
18+
<p>Some text</p>
19+
<h2><a id='subject-2'></a>Subject 2</h2>
20+
<p>Some text</p>

spec/fixtures/toc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
1. [File](/File)
2929
1. [File-with-Dashes](/File-with-Dashes)
3030
1. [File with Spaces](/File%20with%20Spaces)
31+
1. [File with TOC](/File%20with%20TOC)
3132
1. [File without H1](/File%20without%20H1)

spec/madness/document_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@
248248
expect(doc.content).to have_tag :a, id: 'just-a-file'
249249
end
250250

251+
it "replaces <!-- TOC --> markers with table of contents" do
252+
doc = described_class.new "File with TOC"
253+
expect(doc.content).to match_approval 'document-toc'
254+
end
255+
251256
context "when headers contain links" do
252257
subject { described_class.new "CHANGELOG" }
253258

0 commit comments

Comments
 (0)