Skip to content

Commit

Permalink
Main document H1 tag can and must be a single hash tag
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelBoucey committed Sep 13, 2024
1 parent 96eb8d5 commit 9719d57
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
28 changes: 24 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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 {

Expand All @@ -128,23 +141,30 @@ 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
}

section = ""

headerLines = append(headerLines, rewrittenLine)
}

mdLines = append(mdLines, rewrittenLine)

} else if !tocLine.Match([]byte(line)) {

if tocInsertionLine.Match([]byte(line)) {

isToCInsertionLine = true

}

mdLines = append(mdLines, line)
Expand Down
4 changes: 2 additions & 2 deletions test/run
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/test-toc.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<H1>Lorem</H1>
# Lorem

<!-- ToC -->

Expand Down
2 changes: 1 addition & 1 deletion test/test.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<H1>Lorem</H1>
# Lorem

# Lorem ipsum dolor

Expand Down

0 comments on commit 9719d57

Please sign in to comment.