diff --git a/README.md b/README.md new file mode 100644 index 0000000..21cf129 --- /dev/null +++ b/README.md @@ -0,0 +1,68 @@ +# Markdown Table Of Contents + +

+ Latest Release + GoDoc + GoReportCard +

+ +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 + ``` + +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 diff --git a/example_markdown.md b/example_markdown.md new file mode 100644 index 0000000..ae2a8db --- /dev/null +++ b/example_markdown.md @@ -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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..694b854 --- /dev/null +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4eba945 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100644 index 0000000..b643816 --- /dev/null +++ b/main.go @@ -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] ") + 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) +}