Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TOC support #12

Merged
merged 2 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ Flags:

### Available functions

| Name | Usage |
|--------------------|---|
| `printHelp` | `{{printHelp 'hd'}}` |
| Name | Usage | Description |
|--------------------|----------------------|----------------------------------------|
| `printHelp` | `{{printHelp 'hd'}}` | Print the help text of a command |
| `printToc` | `{{printToc}}` | Print the [TOC](https://en.wikipedia.org/wiki/TOC) of the template file |

### Ignore particular items

Expand Down
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
}
return
},
"printToc": func() string {
return generateTOC(readmeTpl)
},
}).Parse(readmeTpl); err != nil {
return
}
Expand Down Expand Up @@ -152,6 +155,34 @@ func sortBy(items []map[string]interface{}, sortBy string, descending bool) {
})
}

func generateTOC(txt string) (toc string) {
items := strings.Split(txt, "\n")
for i := range items {
item := items[i]

var prefix string
var tag string
if strings.HasPrefix(item, "## ") {
tag = strings.TrimPrefix(item, "## ")
prefix = "- "
} else if strings.HasPrefix(item, "### ") {
tag = strings.TrimPrefix(item, "### ")
prefix = " - "
} else {
continue
}

// not support those titles which have whitespaces
tag = strings.TrimSpace(tag)
if len(strings.Split(tag, " ")) > 1 {
continue
}

toc = toc + fmt.Sprintf("%s[%s](#%s)\n", prefix, tag, strings.ToLower(tag))
}
return
}

func main() {
opt := &option{}
cmd := cobra.Command{
Expand Down
41 changes: 41 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,44 @@ func Test_sortBy(t *testing.T) {
})
}
}

func Test_generateTOC(t *testing.T) {
type args struct {
txt string
}
tests := []struct {
name string
args args
wantToc string
}{{
name: "simple text",
args: args{
txt: `## Good`,
},
wantToc: `- [Good](#good)
`,
}, {
name: "multiple levels of the titles",
args: args{
txt: `## Good
content
### Better`,
},
wantToc: `- [Good](#good)
- [Better](#better)
`,
}, {
name: "has whitespace between title",
args: args{
txt: `## Good
## This is good`,
},
wantToc: `- [Good](#good)
`,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.wantToc, generateTOC(tt.args.txt), "generateTOC(%v)", tt.args.txt)
})
}
}