Feat: add ci check the link (#324)

This commit is contained in:
yangsoon 2021-10-10 22:59:08 +08:00 committed by GitHub
parent c68358d548
commit 02893974d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 3 deletions

View File

@ -7,6 +7,16 @@ on:
branches: main
jobs:
check-link:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-go@v1
with:
go-version: "1.16.6"
- name: Check Link
run: make check-link
checks:
if: github.event_name != 'push'
runs-on: ubuntu-latest
@ -14,7 +24,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: '14.x'
node-version: "14.x"
- name: Test Build
run: |
if [ -e yarn.lock ]; then
@ -32,7 +42,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
with:
node-version: '14.x'
node-version: "14.x"
- uses: webfactory/ssh-agent@v0.5.0
with:
ssh-private-key: ${{ secrets.GH_PAGES_DEPLOY }}

View File

@ -1,2 +1,5 @@
update-version:
sh ./hack/version.sh $(version)
sh ./hack/version.sh $(version)
check-link:
go run ./hack/linkcheck.go

74
hack/linkcheck.go Normal file
View File

@ -0,0 +1,74 @@
package main
import (
"bytes"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
func main() {
var cnt int
filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if !strings.HasSuffix(path, ".md") || strings.Contains(path, "node_modules/") || strings.Contains(path, "version-v1.0") || strings.HasSuffix(path, "introduction.md") || strings.HasSuffix(path, "README.md") {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
fmt.Printf("readfile %s err %v\n", path, err)
return nil
}
if !strings.Contains(string(data), "](../") {
return nil
}
targetDir := filepath.Dir(path)
buff := bytes.NewBuffer(data)
for {
line, err := buff.ReadString('\n')
if err != nil {
break
}
sections := strings.Split(line, "](")
if len(sections) <= 1 {
continue
}
for idx, s := range sections {
if idx < 1 {
continue
}
if strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://") || strings.HasPrefix(s, "#") {
continue
}
d := strings.Index(s, ")")
subStr := s[:d]
targetFilePath := filepath.Clean(targetDir + "/" + subStr)
//fmt.Println(path, subStr, "=>", targetFilePath)
if _, err1 := os.Stat(targetFilePath); err1 != nil {
if _, err2 := os.Stat(targetFilePath + ".md"); err2 != nil {
ss := strings.LastIndex(targetFilePath, "#")
if ss != -1 {
targetFilePath = targetFilePath[:ss]
}
ww := strings.LastIndex(targetFilePath, "?")
if ww != -1 {
targetFilePath = targetFilePath[:ww]
}
if _, err3 := os.Stat(targetFilePath + ".md"); err3 != nil {
if _, err4 := os.Stat(targetFilePath + ".mdx"); err4 != nil {
fmt.Println("file:", path, "refer to a non-existent doc:", subStr, "search path:", targetFilePath)
os.Exit(1)
}
}
}
}
}
}
cnt++
return nil
})
fmt.Printf("%d total files effected\n", cnt)
}