diff --git a/main.go b/main.go index f739453..5595336 100644 --- a/main.go +++ b/main.go @@ -43,8 +43,9 @@ var ( func main() { - version := "2.1.0" + version := "3.0.0" + var firstH1Done bool = false var headerCounters [7]int var headerLines []string var isToCInsertionLine bool = false @@ -69,6 +70,7 @@ func main() { removeFlag := flag.Bool("r", false, "Remove table of contents and section numbers from the .md file") versionFlag := flag.Bool("v", false, "Show version") writeFlag := flag.Bool("w", false, "Write section numbers to the .md file (default to stdout)") + noTitleSkipFlag := flag.Bool("t", false, "Do section numbering from the main document title (H1)") flag.Parse() @@ -114,7 +116,18 @@ func main() { header := matches[1] currentHeaderType := len(matches[1]) title := matches[3] - headerCounters[currentHeaderType]++ + + if firstH1Done || *noTitleSkipFlag { + + headerCounters[currentHeaderType]++ + + } + + if !firstH1Done && currentHeaderType == 1 { + + firstH1Done = true + + } if *removeFlag { @@ -128,7 +141,13 @@ func main() { } - rewrittenLine = header + " " + section + " " + title + if section != "" { + section += " " + } + + rewrittenLine = header + " " + section + title + + headerLines = append(headerLines, rewrittenLine) for i := currentHeaderType + 1; i <= 6; i++ { headerCounters[i] = 0 @@ -136,7 +155,6 @@ func main() { section = "" - headerLines = append(headerLines, rewrittenLine) } mdLines = append(mdLines, rewrittenLine) @@ -144,7 +162,9 @@ func main() { } else if !tocLine.Match([]byte(line)) { if tocInsertionLine.Match([]byte(line)) { + isToCInsertionLine = true + } mdLines = append(mdLines, line) diff --git a/test/run b/test/run index 446d2e4..f2bf414 100755 --- a/test/run +++ b/test/run @@ -8,8 +8,8 @@ function filesum () { } DUMBER=~/go/bin/dumber -RIGHT_NUMBERED_SECTIONS_CHECKSUM=a60c33256a196cf50bc7b9b5f1d5927c -RIGHT_NUMBERED_SECTIONS_AND_TOC_CHECKSUM=9c643b5d4c36f83ec979fe513ab1f233 +RIGHT_NUMBERED_SECTIONS_CHECKSUM=796eb0234b17da57d4424c3196c620a1 +RIGHT_NUMBERED_SECTIONS_AND_TOC_CHECKSUM=0503fcbd091e6fa626ff71f8d5f1ae7b echo diff --git a/test/test-toc.md b/test/test-toc.md index 9421e77..c868c6b 100644 --- a/test/test-toc.md +++ b/test/test-toc.md @@ -1,4 +1,4 @@ -

Lorem

+# Lorem diff --git a/test/test.md b/test/test.md index 62f44cf..b37d2a0 100644 --- a/test/test.md +++ b/test/test.md @@ -1,4 +1,4 @@ -

Lorem

+# Lorem # Lorem ipsum dolor