-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dfcb865
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Markdown Table Of Contents | ||
|
||
<p> | ||
<a href="https://github.com/anotherhadi/markdown-table-of-contents/releases"><img src="https://img.shields.io/github/release/anotherhadi/markdown-table-of-contents.svg" alt="Latest Release"></a> | ||
<a href="https://pkg.go.dev/github.com/anotherhadi/markdown-table-of-contents?tab=doc"><img src="https://godoc.org/github.com/golang/gddo?status.svg" alt="GoDoc"></a> | ||
<a href="https://goreportcard.com/report/github.com/anotherhadi/markdown-table-of-contents"><img src="https://goreportcard.com/badge/github.com/anotherhadi/markdown-table-of-contents" alt="GoReportCard"></a> | ||
</p> | ||
|
||
Markdown Table Of Contents is a straightforward command-line tool designed to generate a table of contents for your Markdown files based on titles. | ||
|
||
## Features | ||
|
||
- **Automatic Generation**: Quickly generate a table of contents for your Markdown files. | ||
- **Title Detection**: Detects headings in your Markdown file and generates links to them in the table of contents. | ||
|
||
## How to Use | ||
|
||
1. **Installation**: | ||
|
||
```bash | ||
go install github.com/anotherhadi/markdown-table-of-contents@latest | ||
``` | ||
|
||
2. **Usage**: Run the executable with your Markdown file as an argument. | ||
|
||
```bash | ||
markdown-table-of-contents <markdownfile.md> | ||
``` | ||
|
||
3. **Output**: The tool will generate a table of contents based on the headings in your Markdown file. | ||
|
||
## Example | ||
|
||
Suppose you have a Markdown file named `example.md` with the following headings: | ||
|
||
```md | ||
# Introduction | ||
## Getting Started | ||
### Installation | ||
### Usage | ||
## Features | ||
# Conclusion | ||
``` | ||
|
||
Running the command: | ||
|
||
```bash | ||
markdown-table-of-contents example.md | ||
``` | ||
|
||
Would generate the following table of contents: | ||
|
||
- [Introduction](#introduction) | ||
- [Getting Started](#getting-started) | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Features](#features) | ||
- [Conclusion](#conclusion) | ||
|
||
## License | ||
|
||
This project is licensed under the [MIT License](LICENSE). | ||
|
||
## Todolist | ||
|
||
- If no H1, or H2, ..., trim spaces before | ||
- Parse command line arguments | ||
- Replace from file with {table_of_contents} tag or something |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# My document | ||
|
||
## First section | ||
|
||
I can write text here | ||
|
||
### First h3 heading | ||
|
||
or here | ||
|
||
### Second h3 heading | ||
|
||
## Another section | ||
|
||
### With a sub-section | ||
|
||
And a text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module github.com/anotherhadi/markdown-table-of-contents | ||
|
||
go 1.22.5 | ||
|
||
require github.com/anotherhadi/markdown v0.0.0-20240831131312-28f0ef0aa3bc | ||
|
||
require gopkg.in/yaml.v3 v3.0.1 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
github.com/anotherhadi/markdown v0.0.0-20240831131312-28f0ef0aa3bc h1:a/VDv5pqdO/zgzW9BVdxXXV2Ka1TTO8q4DJtoWfSVR4= | ||
github.com/anotherhadi/markdown v0.0.0-20240831131312-28f0ef0aa3bc/go.mod h1:nzt60oFya6gJjbk2Punty4tdOAZubKdu+qi+ovSxvUY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/anotherhadi/markdown" | ||
) | ||
|
||
func formatTitle(title string) string { | ||
str := strings.TrimSpace(title) | ||
str = strings.ReplaceAll(str, " ", "-") | ||
str = regexp.MustCompile(`[^a-zA-Z0-9 _-]+`).ReplaceAllString(str, "") | ||
str = strings.ToLower(str) | ||
return str | ||
} | ||
|
||
func main() { | ||
if len(os.Args) <= 1 { | ||
fmt.Println("Usage: markdown-table-of-contents [markdownfile.md] <Identation string>") | ||
os.Exit(1) | ||
} | ||
|
||
indentationString := " " // By default, use 2 spaces | ||
if len(os.Args) > 2 { | ||
indentationString = os.Args[2] | ||
} | ||
|
||
filePath := os.Args[1] | ||
md := markdown.New(filePath) | ||
err := md.Read() | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
str := "" | ||
for _, section := range md.Sections { | ||
if section.SectionType == markdown.NullSection { | ||
continue | ||
} | ||
|
||
numberOfSpaces := len(string(section.SectionType)) - 1 | ||
str += strings.Repeat(indentationString, numberOfSpaces) | ||
str += "- [" + section.Text + "](#" + formatTitle(section.Text) + ")" | ||
str += "\n" | ||
} | ||
fmt.Println(str) | ||
} |