Skip to content

Commit 24b880c

Browse files
bitluxjba
authored andcommitted
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 <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> Reviewed-by: Junyang Shao <[email protected]>
1 parent dd59d68 commit 24b880c

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

internal/cmd/weave/weave.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"bufio"
2929
"bytes"
3030
"fmt"
31+
"io"
3132
"log"
3233
"os"
3334
"path/filepath"
@@ -72,11 +73,12 @@ func main() {
7273
depth := len(words[0])
7374
words = words[1:]
7475
text := strings.Join(words, " ")
75-
for i := range words {
76-
words[i] = strings.ToLower(words[i])
77-
}
78-
line = fmt.Sprintf("%s1. [%s](#%s)",
79-
strings.Repeat("\t", depth-1), text, strings.Join(words, "-"))
76+
anchor := strings.Join(words, "-")
77+
anchor = strings.ToLower(anchor)
78+
anchor = strings.ReplaceAll(anchor, "**", "")
79+
anchor = strings.ReplaceAll(anchor, "`", "")
80+
anchor = strings.ReplaceAll(anchor, "_", "")
81+
line = fmt.Sprintf("%s1. [%s](#%s)", strings.Repeat("\t", depth-1), text, anchor)
8082
toc = append(toc, line)
8183
}
8284
}
@@ -85,7 +87,7 @@ func main() {
8587
}
8688

8789
// Pass 2.
88-
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
90+
if _, err := f.Seek(0, io.SeekStart); err != nil {
8991
log.Fatalf("can't rewind input: %v", err)
9092
}
9193
in = bufio.NewScanner(f)
@@ -173,12 +175,6 @@ func include(file, tag string) (string, error) {
173175
return text.String(), nil
174176
}
175177

176-
func isBlank(line string) bool { return strings.TrimSpace(line) == "" }
177-
178-
func indented(line string) bool {
179-
return strings.HasPrefix(line, " ") || strings.HasPrefix(line, "\t")
180-
}
181-
182178
// cleanListing removes entirely blank leading and trailing lines from
183179
// text, and removes n leading tabs.
184180
func cleanListing(text string) string {

0 commit comments

Comments
 (0)