Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add linter to check truncate tag in blog posts #4139

Merged
merged 7 commits into from Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions tools/checkdocs/checkdocs.go
Expand Up @@ -68,13 +68,27 @@ func checkFiles(files []string, logf, fatalf func(string, ...any)) {
logf("%q: %s", file, err)
failed = true
}

// Check for the truncate string
if err = verifyTruncateString(b); err != nil {
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
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")
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
}

// extractFrontMatter returns the front matter of a blog post.
func extractFrontMatter(fm []byte) ([]byte, error) {
var in bool
Expand Down
12 changes: 12 additions & 0 deletions tools/checkdocs/checkdocs_test.go
Expand Up @@ -62,3 +62,15 @@ func TestVerifyTags(t *testing.T) {
err := verifyTags(fm)
assert.EqualError(t, err, `tag "documents databases" is not in the allowed list`)
}

func TestVerifyTruncateString(t *testing.T) {
// Test case where truncate string is present
b := []byte("This is a blog post. <!--truncate--> The rest of the post...")
err := verifyTruncateString(b)
assert.NoError(t, err)

// Test case where truncate string is not present
b = []byte("This is a blog post without a truncate string.")
err = verifyTruncateString(b)
assert.EqualError(t, err, "truncate string not found")
}