forked from FerretDB/FerretDB
/
checkdocs.go
221 lines (179 loc) · 4.98 KB
/
checkdocs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main contains linter for blog posts.
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
files, err := filepath.Glob(filepath.Join("website", "blog", "*.md"))
if err != nil {
log.Fatal(err)
}
checkFiles(files, log.Printf, log.Fatalf)
}
// checkFiles verifies that blog posts are correctly formatted,
// using logf for progress reporting and fatalf for errors.
func checkFiles(files []string, logf, fatalf func(string, ...any)) {
if len(files) == 0 {
fatalf("No blog posts found")
}
var failed bool
for _, file := range files {
b, err := os.ReadFile(file)
if err != nil {
fatalf("Couldn't read file %s: %s", file, err)
}
if b, err = extractFrontMatter(b); err != nil {
fatalf("Couldn't extract front matter from %s: %s", file, err)
}
if err = verifySlug(filepath.Base(file), b); err != nil {
logf("%q: %s", file, err)
failed = true
}
if err = verifyDateNotPresent(b); err != nil {
logf("%q: %s", file, err)
failed = true
}
if err = verifyTags(b); err != nil {
logf("%q: %s", file, err)
failed = true
}
// Check for the truncate string
if err = verifyTruncateString(b); err != nil {
logf("%q: %s", file, err)
failed = true
}
}
if failed {
fatalf("One or more blog posts are not correctly formatted")
}
}
// verifyTruncateString checks that the truncate string is present.
func verifyTruncateString(b []byte) error {
if !bytes.Contains(b, []byte("<!--truncate-->")) {
return fmt.Errorf("truncate string not found")
}
return nil
}
// extractFrontMatter returns the front matter of a blog post.
func extractFrontMatter(fm []byte) ([]byte, error) {
var in bool
var res []byte
s := bufio.NewScanner(bytes.NewReader(fm))
for s.Scan() {
if !in {
if s.Text() != "---" {
return nil, fmt.Errorf("expected front matter start on the first line, got %q", s.Text())
}
in = true
continue
}
if s.Text() == "---" {
return res, nil
}
res = append(res, s.Bytes()...)
res = append(res, '\n')
}
return nil, fmt.Errorf("front matter end not found")
}
// verifySlug checks that file name and slug match each other.
func verifySlug(fn string, fm []byte) error {
fnRe := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}-(.*)\.md$`)
sm := fnRe.FindStringSubmatch(fn)
if len(sm) != 2 {
return fmt.Errorf("file name doesn't match the expected pattern")
}
fnSlug := sm[1]
re := regexp.MustCompile("^slug: (.*)")
var slug string
s := bufio.NewScanner(bytes.NewReader(fm))
for s.Scan() {
if sm = re.FindStringSubmatch(s.Text()); len(sm) > 1 {
slug = sm[1]
break
}
}
if err := s.Err(); err != nil {
return err
}
if slug == "" {
return fmt.Errorf("slug field should be present in the front matter")
}
if slug != fnSlug {
return fmt.Errorf("slug %q doesn't match the file name", slug)
}
return nil
}
// verifyDateNotPresent checks date field is not present.
func verifyDateNotPresent(fm []byte) error {
re := regexp.MustCompile("^date:")
var found bool
s := bufio.NewScanner(bytes.NewReader(fm))
for s.Scan() {
if re.MatchString(s.Text()) {
found = true
break
}
}
if err := s.Err(); err != nil {
return err
}
if found {
return fmt.Errorf("date field should not be present in the front matter")
}
return nil
}
// verifyTags checks that tags are in the allowed list.
func verifyTags(fm []byte) error {
// tags may span multiple lines after formatting
re := regexp.MustCompile(`(?ms)^tags:\s+\[(.+)\]`)
sm := re.FindStringSubmatch(string(fm))
if len(sm) != 2 {
return fmt.Errorf("tags field should be present in the front matter")
}
// keep in sync with writing-guide.md
expectedTags := map[string]struct{}{
"cloud": {},
"community": {},
"compatible applications": {},
"demo": {},
"document databases": {},
"events": {},
"hacktoberfest": {},
"javascript frameworks": {},
"mongodb compatible": {},
"mongodb gui": {},
"open source": {},
"postgresql tools": {},
"product": {},
"release": {},
"sspl": {},
"tutorial": {},
}
for _, tag := range strings.Split(sm[1], ",") {
tag = strings.TrimSpace(tag)
if _, ok := expectedTags[tag]; !ok {
return fmt.Errorf("tag %q is not in the allowed list", tag)
}
}
return nil
}