Skip to content

Commit

Permalink
docs convert file name with special chars to hash
Browse files Browse the repository at this point in the history
  • Loading branch information
bughou committed May 6, 2021
1 parent a7360d5 commit 507cdac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
24 changes: 22 additions & 2 deletions docs/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package docs

import (
"bytes"
"crypto/md5"
"encoding/base64"
"io/ioutil"
"log"
"net/url"
Expand All @@ -21,7 +23,7 @@ type Group struct {
const readme = "README.md"

func (g *Group) Child(path, fullPath string, descs []string) Group {
path = pathPkg.Clean(path)
path = cleanPath(path)
child := Group{Dir: filepath.Join(g.Dir, filepath.FromSlash(path))}
switch path {
case ".", "/":
Expand Down Expand Up @@ -51,7 +53,7 @@ func (g *Group) Route(method, path, fullPath string, handler interface{}) {
return
}

path = pathPkg.Clean(path)
path = cleanPath(path)
switch path {
case ".", "/":
path = method + ".md"
Expand Down Expand Up @@ -136,3 +138,21 @@ func mkdir(dir string) {
}
}
}

func cleanPath(path string) string {
const chars = `\|:*?"<>` // characters invalid for windows file name
if strings.ContainsAny(path, chars) {
components := strings.Split(path, "/")
for i, v := range components {
if strings.ContainsAny(v, chars) {
var bytesSlice []byte
for _, b := range md5.Sum([]byte(v)) {
bytesSlice = append(bytesSlice, b)
}
components[i] = base64.RawURLEncoding.EncodeToString(bytesSlice)
}
}
path = pathPkg.Join(components...)
}
return pathPkg.Clean(path)
}
3 changes: 0 additions & 3 deletions docs/testdata/go.mod

This file was deleted.

2 changes: 1 addition & 1 deletion docs/testdata/users/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 用户 (/users)
### [用户列表 : GET /users](GET.md)
### [用户详情 : GET /users/(?P&lt;type&gt;\w+)/(?P&lt;id&gt;\d+)](%28%3FP%3Ctype%3E%5Cw+%29/GET_%28%3FP%3Cid%3E%5Cd+%29.md)
### [用户详情 : GET /users/(?P&lt;type&gt;\w+)/(?P&lt;id&gt;\d+)](grUHMHMwKlkYAavz-Oif3w/GET_P5wb8cYuHWKc8sbkz-m1vQ.md)

0 comments on commit 507cdac

Please sign in to comment.