Categories: None [Edit]
mdtoc
# mdtoc - Markdown Table of Contents
Read Markdown files and output a table of contents.
## Installation
Requirements:
* [Ruby](https://www.ruby-lang.org/en/) (see [.ruby-version](./.ruby-version))
```bash
gem install mdtoc
```
## Usage
```bash
$ mdtoc --help
Usage: mdtoc [options] files or directories...
-h, --help Show this message
-o, --output PATH Update a table of contents in the file at PATH
-a, --[no-]append Append to the --output file if a tag isn't found
-c, --[no-]create Create the --output file if it does not exist
```
1. Add a `` tag to a Markdown file.
```bash
echo '' >> README.md
```
2. Run `mdtoc` and specify input files or directories (eg. the "test/samples" directory) and an output file (eg. "README.md").
```bash
mdtoc -aco README.md test/samples
```
## Example Rakefile
Create a `Rakefile` with the contents below, then run
[`rake`](https://github.com/ruby/rake) to:
* `git pull`
* `git add` any *.md files
* Run `mdtoc` to update the generated table of contents in the ./README.md file
* Git commit and push any changes
```ruby
task default: %w[mdtoc]
desc 'Update Markdown table of contents and push changes to the git repository'
task :mdtoc do
command = <<~CMD
set -e
if [ -n "$(git diff --name-only --diff-filter=U)" ]; then
echo 'Error: conflicts exist' >&2
exit 1
fi
mdtoc --append --create --output README.md docs/
git add *.md **/*.md
git commit -qm 'Update TOC' || true
git pull
git push
CMD
sh command, verbose: false do |ok, status|
unless ok
fail "Failed with status: #{status.exitstatus}"
end
end
end
```
See [andornaut/til](https://github.com/andornaut/til/blob/master/Rakefile) for an example.
## Development
### Setup
Requirements:
* [Bundler](https://bundler.io/)
```bash
# Install dependencies
bundle
```
### Tasks
```bash
# List rake tasks
$ rake -T
rake build ...
Total
Ranking: 119,946 of 189,128
Downloads: 5,724
Daily
Ranking: 121,766 of 189,104
Downloads: 0
Downloads Trends
Ranking Trends
Num of Versions Trends
Popular Versions (Major)
Popular Versions (Major.Minor)
Depended by
| Rank | Downloads | Name |
|---|
Depends on
| Rank | Downloads | Name |
|---|---|---|
| 9 | 1,223,147,708 | rake |
| 14 | 1,139,100,575 | minitest |
| 56 | 644,175,194 | rubocop |
| 380 | 122,063,434 | sorbet-runtime |
| 511 | 93,599,273 | unparser |
| 1,026 | 45,135,417 | sorbet |
| 1,579 | 22,679,434 | rubocop-sorbet |
| 1,930 | 14,778,476 | rubocop-shopify |
Owners
| # | Gravatar | Handle |
|---|---|---|
| 1 | andornaut |