Skip to content

Commit 5246328

Browse files
committed
tools/docs2wiki: place source edit link instead of static footer
As suggested in #2094 (comment) Since we rely on GitHub's default wiki feature instead of GH pages or custom hosting sites, options are limited currently. Updates #2094 Change-Id: I3090ee79c9794e2b56fc78fa564770266767f3dd Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/412055 Reviewed-by: Jamal Carvalho <[email protected]>
1 parent 5659cbc commit 5246328

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

docs/_Footer.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

tools/docs2wiki/main.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"path/filepath"
1818
"regexp"
1919
"strings"
20+
"text/template"
2021
)
2122

2223
var writeFlag = flag.Bool("w", false, "Overwrite new file contents to disk.")
@@ -28,13 +29,15 @@ func main() {
2829
errorf("Usage: %v <dir>", os.Args[0])
2930
os.Exit(1)
3031
}
31-
if err := rewriteLinks(flag.Arg(0), *writeFlag); err != nil {
32+
33+
genFooter := footerGenerator("https://github.com/golang/vscode-go/edit/master/docs/")
34+
if err := rewriteLinks(flag.Arg(0), genFooter, *writeFlag); err != nil {
3235
errorf("failed to rewrite links: %v", err)
3336
os.Exit(1)
3437
}
3538
}
3639

37-
func rewriteLinks(dir string, overwrite bool) error {
40+
func rewriteLinks(dir string, genFooter func(srcPath string) []byte, overwrite bool) error {
3841
return filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
3942
if err != nil {
4043
return err
@@ -55,6 +58,10 @@ func rewriteLinks(dir string, overwrite bool) error {
5558
}
5659
converted := stripTitleInPage(data)
5760
converted = markdownLink2WikiLink(converted)
61+
if name != "_Footer.md" && name != "_Sidebar.md" {
62+
relPath, _ := filepath.Rel(dir, path)
63+
converted = append(converted, genFooter(filepath.ToSlash(relPath))...)
64+
}
5865
if overwrite {
5966
return ioutil.WriteFile(path, converted, 0644)
6067
}
@@ -128,3 +135,19 @@ func markdownLink2WikiLink(src []byte) []byte {
128135
func errorf(format string, a ...interface{}) {
129136
fmt.Fprintf(os.Stderr, format+"\n", a...)
130137
}
138+
139+
func footerGenerator(sourceEditURL string) func(string) []byte {
140+
return func(srcPath string) []byte {
141+
editURL := sourceEditURL + srcPath
142+
buf := new(bytes.Buffer)
143+
footerTmpl.Execute(buf, editURL)
144+
return buf.Bytes()
145+
}
146+
}
147+
148+
var footerTmpl = template.Must(template.New("footer").Parse(`
149+
150+
---
151+
[*✏️ Want to contribute to this wiki?*]({{.}})
152+
153+
Update [the source]({{.}}) and send a PR.`))

tools/docs2wiki/main_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"os"
1010
"path/filepath"
11+
"strings"
1112
"testing"
1213

1314
"github.com/google/go-cmp/cmp"
@@ -29,7 +30,8 @@ func TestRewriteLinks(t *testing.T) {
2930
// which we will read back for comparison against tt.want.
3031
// With overwrite=false, rewriteLinks just prints out the diff
3132
// which will be difficult to test.
32-
err := rewriteLinks(dir, true)
33+
genFooter := func(string) []byte { return nil }
34+
err := rewriteLinks(dir, genFooter, true)
3335
if err != nil {
3436
t.Fatal(err)
3537
}
@@ -82,3 +84,30 @@ var wikiStyle = `
8284
8385
[This doesn't change](https://go.dev/foo.md)
8486
[Untouchable.md](foo)`
87+
88+
func TestGenFooter(t *testing.T) {
89+
const editURLPrefix = "https://source.com/edit/docs/"
90+
91+
for _, tt := range []struct{ filename, wantURL string }{
92+
{filename: "doc.md", wantURL: editURLPrefix + "doc.md"},
93+
{filename: "sub/doc.md", wantURL: editURLPrefix + "sub/doc.md"},
94+
} {
95+
dir := prepareTestData(t, tt.filename, "")
96+
defer os.RemoveAll(dir)
97+
98+
genFooter := footerGenerator(editURLPrefix)
99+
err := rewriteLinks(dir, genFooter, true)
100+
if err != nil {
101+
t.Fatal(err)
102+
}
103+
// rewriteLinks overwrites the original file,
104+
// so reread the content for comparison.
105+
got, err := ioutil.ReadFile(filepath.Join(dir, tt.filename))
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
if !strings.Contains(string(got), tt.wantURL) {
110+
t.Errorf("missing %s, got:\n%s", tt.wantURL, got)
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)