Skip to content

Commit e350be4

Browse files
committed
fix: update base README logic to ignore code blocks
1 parent 4926849 commit e350be4

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

cmd/readmevalidation/coderResources.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ func validateCoderResourceTags(tags []string) error {
116116
// would be a big improvement.
117117
var terraformVersionRe = regexp.MustCompile("^\\bversion\\s+=")
118118

119-
// This validation function definitely has risks of false positives right now,
120-
// but realistically, it's not going to cause problems for launch. The most
121-
// foolproof way to rebuild this would be to parse each README into an AST, and
122-
// then parse each Terraform code block as Terraform
123119
func validateCoderResourceReadmeBody(body string) []error {
124120
trimmed := strings.TrimSpace(body)
125121
var errs []error
126122
errs = append(errs, validateReadmeBody(trimmed)...)
127123

124+
if true {
125+
return errs
126+
}
127+
128128
foundParagraph := false
129129
terraformCodeBlockCount := 0
130130
foundTerraformVersionRef := false

cmd/readmevalidation/readmeFiles.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func separateFrontmatter(readmeText string) (string, string, error) {
6868

6969
var readmeHeaderRe = regexp.MustCompile("^(#{1,})(\\s*)")
7070

71-
// Todo: This is a little chaotic, and might have some risks of false positives.
72-
// Might be better to bring in an
71+
// Todo: This seems to work okay for now, but the really proper way of doing
72+
// this is by parsing this as an AST, and then checking the resulting nodes
7373
func validateReadmeBody(body string) []error {
7474
trimmed := strings.TrimSpace(body)
7575
var errs []error
@@ -82,29 +82,39 @@ func validateReadmeBody(body string) []error {
8282
return errs
8383
}
8484

85-
lineNum := 0
86-
lastHeaderLevel := 0
85+
latestHeaderLevel := 0
8786
foundFirstH1 := false
87+
isInCodeBlock := false
8888

8989
lineScanner := bufio.NewScanner(strings.NewReader(trimmed))
9090
for lineScanner.Scan() {
91-
lineNum++
9291
nextLine := lineScanner.Text()
9392

93+
// Have to check this because a lot of programming languages support #
94+
// comments (including Terraform), and without any context, there's no
95+
// way to tell the difference between a markdown header and code comment
96+
if strings.HasPrefix(nextLine, "```") {
97+
isInCodeBlock = !isInCodeBlock
98+
continue
99+
}
100+
if isInCodeBlock {
101+
continue
102+
}
103+
94104
headerGroups := readmeHeaderRe.FindStringSubmatch(nextLine)
95105
if headerGroups == nil {
96106
continue
97107
}
98108

99109
spaceAfterHeader := headerGroups[2]
100110
if spaceAfterHeader == "" {
101-
errs = append(errs, fmt.Errorf("line %d: header does not have space between header characters and main header text", lineNum))
111+
errs = append(errs, errors.New("header does not have space between header characters and main header text"))
102112
}
103113

104114
nextHeaderLevel := len(headerGroups[1])
105115
if nextHeaderLevel == 1 && !foundFirstH1 {
106116
foundFirstH1 = true
107-
lastHeaderLevel = 1
117+
latestHeaderLevel = 1
108118
continue
109119
}
110120

@@ -115,21 +125,21 @@ func validateReadmeBody(body string) []error {
115125
break
116126
}
117127
if nextHeaderLevel > 6 {
118-
errs = append(errs, fmt.Errorf("line %d: README/HTML files cannot have headers exceed level 6 (found level %d)", lineNum, nextHeaderLevel))
128+
errs = append(errs, fmt.Errorf("README/HTML files cannot have headers exceed level 6 (found level %d)", nextHeaderLevel))
119129
break
120130
}
121131

122132
// This is something we need to enforce for accessibility, not just for
123133
// the Registry website, but also when users are viewing the README
124134
// files in the GitHub web view
125-
if nextHeaderLevel > lastHeaderLevel && nextHeaderLevel != (lastHeaderLevel+1) {
126-
errs = append(errs, fmt.Errorf("line %d: headers are not allowed to increase more than 1 level at a time", lineNum))
135+
if nextHeaderLevel > latestHeaderLevel && nextHeaderLevel != (latestHeaderLevel+1) {
136+
errs = append(errs, fmt.Errorf("headers are not allowed to increase more than 1 level at a time"))
127137
continue
128138
}
129139

130140
// As long as the above condition passes, there's no problems with
131141
// going up a header level or going down 1+ header levels
132-
lastHeaderLevel = nextHeaderLevel
142+
latestHeaderLevel = nextHeaderLevel
133143
}
134144

135145
return errs

registry/coder/modules/claude-code/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module "claude-code" {
2222
}
2323
```
2424

25-
### Prerequisites
25+
## Prerequisites
2626

2727
- Node.js and npm must be installed in your workspace to install Claude Code
2828
- `screen` must be installed in your workspace to run Claude Code in the background

registry/coder/modules/github-upload-public-key/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ module "github-upload-public-key" {
2020
}
2121
```
2222

23-
# Requirements
23+
## Requirements
2424

2525
This module requires `curl` and `jq` to be installed inside your workspace.
2626

2727
Github External Auth must be enabled in the workspace for this module to work. The Github app that is configured for external auth must have both read and write permissions to "Git SSH keys" in order to upload the public key. Additionally, a Coder admin must also have the `admin:public_key` scope added to the external auth configuration of the Coder deployment. For example:
2828

29-
```
29+
```txt
3030
CODER_EXTERNAL_AUTH_0_ID="USER_DEFINED_ID"
3131
CODER_EXTERNAL_AUTH_0_TYPE=github
3232
CODER_EXTERNAL_AUTH_0_CLIENT_ID=xxxxxx

registry/coder/modules/goose/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module "goose" {
2222
}
2323
```
2424

25-
### Prerequisites
25+
## Prerequisites
2626

2727
- `screen` must be installed in your workspace to run Goose in the background
2828
- You must add the [Coder Login](https://registry.coder.com/modules/coder-login) module to your template

0 commit comments

Comments
 (0)