Skip to content

Commit

Permalink
Implement -max-header flag
Browse files Browse the repository at this point in the history
  • Loading branch information
thockin committed Feb 6, 2021
1 parent b07bb90 commit 4c3eed4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions mdtoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func init() {
flag.BoolVar(&defaultOptions.Dryrun, "dryrun", false, "Whether to check for changes to TOC, rather than overwriting. Requires --inplace flag.")
flag.BoolVar(&defaultOptions.Inplace, "inplace", false, "Whether to edit the file in-place, or output to STDOUT. Requires toc tags to be present.")
flag.BoolVar(&defaultOptions.SkipPrefix, "skip-prefix", true, "Whether to ignore any headers before the opening toc tag.")
flag.IntVar(&defaultOptions.MaxDepth, "max-depth", mdtoc.MaxHeaderDepth, "Limit the depth of headers that will be included in the TOC.")

flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [OPTIONS] [FILE]...\n", os.Args[0])
Expand Down
3 changes: 3 additions & 0 deletions mdtoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestDryRun(t *testing.T) {
Options: mdtoc.Options{
Dryrun: true,
SkipPrefix: !test.includePrefix,
MaxDepth: mdtoc.MaxHeaderDepth,
},
Inplace: true,
}
Expand Down Expand Up @@ -119,6 +120,7 @@ func TestInplace(t *testing.T) {
Options: mdtoc.Options{
SkipPrefix: !test.includePrefix,
Dryrun: false,
MaxDepth: mdtoc.MaxHeaderDepth,
},
}
assert.NoError(t, validateArgs(opts, []string{tmpFile.Name()}), test.file)
Expand Down Expand Up @@ -154,6 +156,7 @@ func TestOutput(t *testing.T) {
Options: mdtoc.Options{
Dryrun: false,
SkipPrefix: !test.includePrefix,
MaxDepth: mdtoc.MaxHeaderDepth,
},
}
assert.NoError(t, validateArgs(opts, []string{test.file}), test.file)
Expand Down
12 changes: 9 additions & 3 deletions pkg/mdtoc/mdtoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
StartTOC = "<!-- toc -->"
// EndTOC is the tag that marks the end of the TOC
EndTOC = "<!-- /toc -->"
//
MaxHeaderDepth = 6
)

var (
Expand All @@ -47,6 +49,7 @@ var (
type Options struct {
Dryrun bool
SkipPrefix bool
MaxDepth int
}

// parse parses a raw markdown document to an AST.
Expand All @@ -60,7 +63,7 @@ func parse(b []byte) ast.Node {
}

// GenerateTOC parses a document and returns its TOC
func GenerateTOC(doc []byte) (string, error) {
func GenerateTOC(doc []byte, opts Options) (string, error) {
anchors := make(anchorGen)

md := parse(doc)
Expand All @@ -69,6 +72,9 @@ func GenerateTOC(doc []byte) (string, error) {
toc := &bytes.Buffer{}
htmlRenderer := html.NewRenderer(html.RendererOptions{})
walkHeadings(md, func(heading *ast.Heading) {
if heading.Level > opts.MaxDepth {
return
}
anchor := anchors.mkAnchor(asText(heading))
content := headingBody(htmlRenderer, heading)
fmt.Fprintf(toc, "%s- [%s](#%s)\n", strings.Repeat(" ", heading.Level-baseLvl), content, anchor)
Expand Down Expand Up @@ -201,7 +207,7 @@ func WriteTOC(file string, opts Options) error {
if opts.SkipPrefix && start != -1 && end != -1 {
doc = raw[end:]
}
toc, err := GenerateTOC(doc)
toc, err := GenerateTOC(doc, opts)
if err != nil {
return fmt.Errorf("failed to generate toc: %v", err)
}
Expand Down Expand Up @@ -238,7 +244,7 @@ func GetTOC(file string, opts Options) (string, error) {
if opts.SkipPrefix && start != -1 && end != -1 {
startPos = end
}
toc, err := GenerateTOC(doc[startPos:])
toc, err := GenerateTOC(doc[startPos:], opts)
if err != nil {
return toc, fmt.Errorf("failed to generate toc: %v", err)
}
Expand Down

0 comments on commit 4c3eed4

Please sign in to comment.