example/weave: update TOC anchors to remove **, _, and ` When GitHub's Markdown parser creates anchors for sections, it removes formatting directives like **, _, and `. See the discussion at https://support.github.com/ticket/personal/0/3324945 The links to the sections with formatting directives in the current TOC at https://github.com/golang/example/blob/master/slog-handler-guide/README.md are broken for this reason. This change modifies weave to also remove those characters from the anchor links it generates. This change also replaces the deprecated os.SET_SEEK with io.SeekStart and removes two unused functions. Change-Id: I66e3aa8140e14146bdb349c4ccfb773fe7414c67 Reviewed-on: https://go-review.googlesource.com/c/example/+/664015 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Junyang Shao <shaojunyang@google.com>
diff --git a/internal/cmd/weave/weave.go b/internal/cmd/weave/weave.go index 91c06e4..d833653 100644 --- a/internal/cmd/weave/weave.go +++ b/internal/cmd/weave/weave.go
@@ -28,6 +28,7 @@ "bufio" "bytes" "fmt" + "io" "log" "os" "path/filepath" @@ -72,11 +73,12 @@ depth := len(words[0]) words = words[1:] text := strings.Join(words, " ") - for i := range words { - words[i] = strings.ToLower(words[i]) - } - line = fmt.Sprintf("%s1. [%s](#%s)", - strings.Repeat("\t", depth-1), text, strings.Join(words, "-")) + anchor := strings.Join(words, "-") + anchor = strings.ToLower(anchor) + anchor = strings.ReplaceAll(anchor, "**", "") + anchor = strings.ReplaceAll(anchor, "`", "") + anchor = strings.ReplaceAll(anchor, "_", "") + line = fmt.Sprintf("%s1. [%s](#%s)", strings.Repeat("\t", depth-1), text, anchor) toc = append(toc, line) } } @@ -85,7 +87,7 @@ } // Pass 2. - if _, err := f.Seek(0, os.SEEK_SET); err != nil { + if _, err := f.Seek(0, io.SeekStart); err != nil { log.Fatalf("can't rewind input: %v", err) } in = bufio.NewScanner(f) @@ -173,12 +175,6 @@ return text.String(), nil } -func isBlank(line string) bool { return strings.TrimSpace(line) == "" } - -func indented(line string) bool { - return strings.HasPrefix(line, " ") || strings.HasPrefix(line, "\t") -} - // cleanListing removes entirely blank leading and trailing lines from // text, and removes n leading tabs. func cleanListing(text string) string {