mdtoc 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c01cef42824ceeae479dc235cbe2c7565c2d6ef045074c8d55b34a821ee719d
4
- data.tar.gz: c25c67f2bf841dd9e1e3bb6a632035693299e7a53367b5dd255ecfbc125adab2
3
+ metadata.gz: f53365d2172397bc4b50a26f88ce05d13c8766453b56c96fd1513151b2d72cad
4
+ data.tar.gz: c49f859803c2bf3928c51f70535ba35a9bffda3ccc69457713977497c6a50f6e
5
5
  SHA512:
6
- metadata.gz: 603bec1ab105a7438bb2d0c2e4fb0c7eead9c63b57bfe33ca9354b05458d435805ce614cd11549527749092f2cffdc1289691a177b3c86161bac714f877c3eb7
7
- data.tar.gz: 6bb0b63c4217f899d2277ba35baf7966cd97219e88f111c7a842b535108c698ac0260c0cc1f2c7e24858c9464a9cac333ea1fa4a7244104b051ba3184ed326bd
6
+ metadata.gz: de596f6886429e537d249cc2c2516521cfb23ee8c407f002348a68fe93b3de30f0b6e4809d64c5c2e75858e33791e4507d9060c35c79a8f0459202c105e54b73
7
+ data.tar.gz: 8da62161d757e8be2c7970ed076b58ea1dbc0b1416e35d497dbc5545faf84764ab88e08895f21c3995e98acd2c1f0f88bec4c9b96b5bcb3f27ad6a80ef07b88e
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 andornaut
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,113 @@
1
+ # mdtoc - Markdown Table of Contents
2
+
3
+ Read Markdown files and output a table of contents.
4
+
5
+ ## Installation
6
+
7
+ Requirements:
8
+
9
+ * [Ruby](https://www.ruby-lang.org/en/) (see [.ruby-version](./.ruby-version))
10
+
11
+ ```bash
12
+ gem install mdtoc
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```bash
18
+ mdtoc --help
19
+ Usage: mdtoc [options] files or directories...
20
+ -h, --help Show this message
21
+ -o, --output PATH Update a table of contents in the file at PATH
22
+ -a, --[no-]append Append to the --output file if a <!-- mdtoc --> tag isn't found
23
+ -c, --[no-]create Create the --output file if it does not exist
24
+ ```
25
+
26
+ 1. Add a `<!-- mdtoc -->` tag to a Markdown file.
27
+
28
+ ```bash
29
+ echo '<!-- mdtoc -->' >> README.md
30
+ ```
31
+
32
+ 2. Run `mdtoc` and specify input files or directories (eg. the "test/samples" directory) and an output file (eg. "README.md").
33
+
34
+ ```bash
35
+ mdtoc -aco README.md test/samples
36
+ ```
37
+
38
+ ## Example Rakefile
39
+
40
+ Create a `Rakefile` with the contents below, then run
41
+ [`rake`](https://github.com/ruby/rake) to:
42
+
43
+ * `git pull`
44
+ * `git add` any `*.md` files
45
+ * Run `mdtoc` to update the generated table of contents in the ./README.md file
46
+ * Git commit and push any changes
47
+
48
+ ```ruby
49
+ task default: %w[mdtoc]
50
+
51
+ desc 'Update Markdown table of contents and push changes to the git repository'
52
+ task :mdtoc do
53
+ command = <<~CMD
54
+ set -e
55
+ if [ -n "$(git diff --name-only --diff-filter=U)" ]; then
56
+ echo 'Error: conflicts exist' >&2
57
+ exit 1
58
+ fi
59
+ mdtoc --append --create --output README.md docs/
60
+ git add *.md **/*.md
61
+ git commit -qm 'Update TOC' || true
62
+ git pull
63
+ git push
64
+ CMD
65
+ sh command, verbose: false do |ok, status|
66
+ unless ok
67
+ fail "Failed with status: #{status.exitstatus}"
68
+ end
69
+ end
70
+ end
71
+ ```
72
+
73
+ See [andornaut/til](https://github.com/andornaut/til/blob/master/Rakefile) for an example.
74
+
75
+ ## Development
76
+
77
+ ### Setup
78
+
79
+ Requirements:
80
+
81
+ * [Bundler](https://bundler.io/)
82
+ * [chruby](https://github.com/postmodern/chruby) (recommended)
83
+
84
+ ```bash
85
+ # Setup development environment
86
+ bin/setup
87
+ ```
88
+
89
+ ### Tasks
90
+
91
+ ```bash
92
+ # List rake tasks
93
+ rake -T
94
+ rake build # Build gem into the pkg directory
95
+ rake default # Run the build, rubocop, sorbet and test tasks
96
+ rake install # Build and install gem into system gems
97
+ rake rubocop # Run RuboCop
98
+ rake sorbet # Run the Sorbet type checker
99
+ rake test # Run tests
100
+
101
+ # Run mdtoc with test inputs
102
+ ruby -Ilib bin/mdtoc test/samples
103
+
104
+ # Run mdtoc with test inputs, and write to a newly created output file
105
+ f=$(mktemp) && ruby -Ilib bin/mdtoc -aco ${f} test/samples ; cat ${f}
106
+ ```
107
+
108
+ ### Publishing
109
+
110
+ 1. Bump version in `lib/mdtoc/version.rb`
111
+ 2. Run `bundle install` to update `Gemfile.lock`
112
+ 3. Commit the changes
113
+ 4. Run `rake release` to publish the gem to RubyGems, create the git tag, and push
data/bin/setup ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'fileutils'
5
+
6
+ # path to your application root.
7
+ APP_ROOT = File.expand_path('..', __dir__)
8
+
9
+ def system!(*args)
10
+ system(*args) || abort("
11
+ == Command #{args} failed ==")
12
+ end
13
+
14
+ FileUtils.chdir APP_ROOT do
15
+ # This script is a way to set up a development environment or update your current one.
16
+ # You can run it with `bin/setup`.
17
+
18
+ puts '== Installing dependencies =='
19
+ system! 'gem install bundler --conservative'
20
+ system('bundle check') || system!('bundle install')
21
+
22
+ puts "
23
+ == Removing old RBIs =="
24
+ FileUtils.rm_rf('sorbet/rbi/gems')
25
+ FileUtils.rm_rf('sorbet/rbi/todo.rbi')
26
+
27
+ puts "
28
+ == Generating RBIs =="
29
+ system! 'bundle exec tapioca gems'
30
+
31
+ puts "
32
+ == Initializing Sorbet =="
33
+ system! 'bundle exec srb tc'
34
+
35
+ puts "
36
+ == Setup complete! =="
37
+ end
@@ -9,6 +9,9 @@ module Mdtoc
9
9
  class Header
10
10
  extend T::Sig
11
11
 
12
+ sig { returns(Integer) }
13
+ attr_accessor :depth
14
+
12
15
  sig { params(depth: Integer, label: String, url: String).void }
13
16
  def initialize(depth, label, url)
14
17
  raise ArgumentError, "Header depth must be >= 0, but was #{depth}" if depth.negative?
data/lib/mdtoc/node.rb CHANGED
@@ -26,9 +26,12 @@ module Mdtoc
26
26
 
27
27
  sig { params(paths: T::Array[String]).returns(String) }
28
28
  def render(paths)
29
- paths
30
- .flat_map { |path| for_path(path).headers }
31
- .join("\n")
29
+ headers = paths.flat_map { |path| for_path(path).headers }
30
+ min_depth = headers.map(&:depth).min || 0
31
+ if min_depth.positive?
32
+ headers.each { |h| h.depth -= min_depth }
33
+ end
34
+ headers.join("\n")
32
35
  end
33
36
  end
34
37
 
data/lib/mdtoc/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Mdtoc
5
- VERSION = '0.3.0'
5
+ VERSION = '0.4.0'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mdtoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - andornaut
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-03-03 00:00:00.000000000 Z
11
+ date: 2026-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.25'
19
+ version: '6.0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '5.25'
26
+ version: '6.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -72,28 +72,28 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.10.0
75
+ version: 0.12.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.10.0
82
+ version: 0.12.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: unparser
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 0.6.0
89
+ version: 0.8.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 0.6.0
96
+ version: 0.8.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: sorbet-runtime
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -126,7 +126,7 @@ description: |
126
126
  ## Usage
127
127
 
128
128
  ```bash
129
- $ mdtoc --help
129
+ mdtoc --help
130
130
  Usage: mdtoc [options] files or directories...
131
131
  -h, --help Show this message
132
132
  -o, --output PATH Update a table of contents in the file at PATH
@@ -136,23 +136,23 @@ description: |
136
136
 
137
137
  1. Add a `<!-- mdtoc -->` tag to a Markdown file.
138
138
 
139
- ```bash
140
- echo '<!-- mdtoc -->' >> README.md
141
- ```
139
+ ```bash
140
+ echo '<!-- mdtoc -->' >> README.md
141
+ ```
142
142
 
143
143
  2. Run `mdtoc` and specify input files or directories (eg. the "test/samples" directory) and an output file (eg. "README.md").
144
144
 
145
- ```bash
146
- mdtoc -aco README.md test/samples
147
- ```
145
+ ```bash
146
+ mdtoc -aco README.md test/samples
147
+ ```
148
148
 
149
149
  ## Example Rakefile
150
150
 
151
151
  Create a `Rakefile` with the contents below, then run
152
152
  [`rake`](https://github.com/ruby/rake) to:
153
153
 
154
- * `git pullgem push pkg/mdtoc-0.2.0.gem`
155
- * `git add` any *.md files
154
+ * `git pull`
155
+ * `git add` any `*.md` files
156
156
  * Run `mdtoc` to update the generated table of contents in the ./README.md file
157
157
  * Git commit and push any changes
158
158
 
@@ -201,33 +201,37 @@ description: |
201
201
 
202
202
  ```bash
203
203
  # List rake tasks
204
- $ rake -T
204
+ rake -T
205
205
  rake build # Build gem into the pkg directory
206
206
  rake default # Run the build, rubocop, sorbet and test tasks
207
207
  rake install # Build and install gem into system gems
208
208
  rake rubocop # Run RuboCop
209
209
  rake sorbet # Run the Sorbet type checker
210
210
  rake test # Run tests
211
- ```
211
+
212
212
  # Run mdtoc with test inputs
213
- $ ruby -Ilib bin/mdtoc test/samples
213
+ ruby -Ilib bin/mdtoc test/samples
214
214
 
215
215
  # Run mdtoc with test inputs, and write to a newly created output file
216
- $ f=$(mktemp) && ruby -Ilib bin/mdtoc -aco ${f} test/samples ; cat ${f}
216
+ f=$(mktemp) && ruby -Ilib bin/mdtoc -aco ${f} test/samples ; cat ${f}
217
217
  ```
218
218
 
219
219
  ### Publishing
220
220
 
221
- ```bash
222
- rake release
223
- ```
221
+ 1. Bump version in `lib/mdtoc/version.rb`
222
+ 2. Run `bundle install` to update `Gemfile.lock`
223
+ 3. Commit the changes
224
+ 4. Run `rake release` to publish the gem to RubyGems, create the git tag, and push
224
225
  email:
225
226
  executables:
226
227
  - mdtoc
227
228
  extensions: []
228
229
  extra_rdoc_files: []
229
230
  files:
231
+ - LICENSE
232
+ - README.md
230
233
  - bin/mdtoc
234
+ - bin/setup
231
235
  - lib/mdtoc.rb
232
236
  - lib/mdtoc/cli.rb
233
237
  - lib/mdtoc/markdown/fragment_generator.rb
@@ -249,7 +253,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
249
253
  requirements:
250
254
  - - ">="
251
255
  - !ruby/object:Gem::Version
252
- version: 2.7.2
256
+ version: 3.1.0
253
257
  required_rubygems_version: !ruby/object:Gem::Requirement
254
258
  requirements:
255
259
  - - ">="