Skip to content

Commit 1cb7594

Browse files
Add an Overview subheader to logically separate Installation and general provider info (#2419)
This pull request is the result of discovering #2420. This fix: - Avoids the rendering issues for unheadered text as outlined in #2420 by adding a `##` header - Visually and logically separates the provider summary text from the installation section. - Strips the H1 header earlier in the doc transformation so we can verify remaining content - Strips the "schema generated by tfplugindocs" HTML comment TL;DR: while this is on its face a quick fix, we _do_ want to separate the installation from the general text, so adding the Overview header is something we'd want to do anyway. Screenshots: Before: <img width="550" alt="Screenshot 2024-09-17 at 11 16 15 AM" src="https://github.com/user-attachments/assets/0b07e7a3-2f74-4049-9382-e5739d13577d"> After: <img width="578" alt="Screenshot 2024-09-17 at 11 16 34 AM" src="https://github.com/user-attachments/assets/2f648067-56a2-4c0c-96c5-d8297afe7f8c">
1 parent 2b4cd9b commit 1cb7594

File tree

6 files changed

+104
-9
lines changed

6 files changed

+104
-9
lines changed

pkg/tfgen/installation_docs.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,25 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) {
2828
// Generate pulumi-specific front matter
2929
frontMatter := writeFrontMatter(g.info.Name)
3030

31+
// Remove the title. A title gets populated from Hugo frontmatter; we do not want two.
32+
content, err := removeTitle(content)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
// Strip the tfplugindocs generation HTML comment
38+
content = stripSchemaGeneratedByTFPluginDocs(content)
39+
3140
// Generate pulumi-specific installation instructions
3241
installationInstructions := writeInstallationInstructions(g.info.Golang.ImportBasePath, g.info.Name)
3342

43+
overviewHeader := getOverviewHeader(content)
44+
3445
// Add instructions to top of file
35-
contentStr := frontMatter + installationInstructions + string(content)
46+
contentStr := frontMatter + installationInstructions + overviewHeader + string(content)
3647

3748
//Translate code blocks to Pulumi
38-
contentStr, err := translateCodeBlocks(contentStr, g)
49+
contentStr, err = translateCodeBlocks(contentStr, g)
3950
if err != nil {
4051
return nil, err
4152
}
@@ -45,13 +56,6 @@ func plainDocsParser(docFile *DocFile, g *Generator) ([]byte, error) {
4556
if err != nil {
4657
return nil, err
4758
}
48-
49-
// Remove the title. A title gets populated from Hugo frontmatter; we do not want two.
50-
contentBytes, err = removeTitle(contentBytes)
51-
if err != nil {
52-
return nil, err
53-
}
54-
5559
//Reformat field names. Configuration fields are camelCased like nodejs.
5660
contentStr, _ = reformatText(infoContext{
5761
language: "nodejs",
@@ -107,6 +111,23 @@ func writeInstallationInstructions(goImportBasePath, providerName string) string
107111
)
108112
}
109113

114+
func getOverviewHeader(content []byte) string {
115+
const overviewHeader = "## Overview\n\n"
116+
// If content starts with an H2, or is otherwise empty then we don't want to add this header
117+
content = bytes.TrimSpace(content)
118+
if bytes.HasPrefix(content, []byte("## ")) || len(content) == 0 {
119+
return ""
120+
}
121+
return overviewHeader
122+
}
123+
124+
var tfplugindocsComment = regexp.MustCompile("<!-- schema generated by tfplugindocs -->")
125+
126+
func stripSchemaGeneratedByTFPluginDocs(content []byte) []byte {
127+
content = tfplugindocsComment.ReplaceAll(content, nil)
128+
return content
129+
}
130+
110131
func applyEditRules(contentBytes []byte, docFile string, g *Generator) ([]byte, error) {
111132
// Obtain edit rules passed by the provider
112133
edits := g.editRules

pkg/tfgen/installation_docs_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,57 @@ func TestWriteInstallationInstructions(t *testing.T) {
157157
})
158158
}
159159

160+
func TestWriteOverviewHeader(t *testing.T) {
161+
t.Parallel()
162+
type testCase struct {
163+
// The name of the test case.
164+
name string
165+
input string
166+
expected string
167+
}
168+
169+
testCases := []testCase{
170+
{
171+
name: "Writes When Content Exists",
172+
input: readTestFile(t, "write-overview-header/with-overview-text.md"),
173+
expected: "## Overview\n\n",
174+
},
175+
{
176+
name: "Does Not Write For Empty Overview",
177+
input: readTestFile(t, "write-overview-header/empty-overview.md"),
178+
expected: "",
179+
},
180+
{
181+
name: "Does Not Write For Empty Content",
182+
input: "",
183+
expected: "",
184+
},
185+
}
186+
for _, tt := range testCases {
187+
tt := tt
188+
t.Run(tt.name, func(t *testing.T) {
189+
t.Parallel()
190+
actual := getOverviewHeader([]byte(tt.input))
191+
assert.Equal(t, tt.expected, actual)
192+
})
193+
}
194+
195+
// The following mirrors the way that the result of `writeOverviewHeader` gets applied to our installation doc.
196+
contextTest := testCase{
197+
name: "Does Not Write For Empty Overview With Context",
198+
input: readTestFile(t, "write-overview-header/empty-overview-with-context-input.md"),
199+
expected: readTestFile(t, "write-overview-header/empty-overview-with-context-expected.md"),
200+
}
201+
t.Run(contextTest.name, func(t *testing.T) {
202+
t.Parallel()
203+
text, err := removeTitle([]byte(contextTest.input))
204+
require.NoError(t, err)
205+
header := getOverviewHeader(text)
206+
actual := header + string(text)
207+
assertEqualHTML(t, contextTest.expected, actual)
208+
})
209+
}
210+
160211
func TestWriteFrontMatter(t *testing.T) {
161212
t.Parallel()
162213

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Schema
2+
Find information about the test provider schema here.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Test Provider
2+
3+
## Schema
4+
Find information about the test provider schema here.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
4+
5+
6+
## Schema
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# test Provider
3+
4+
This provider has some functionality that we're introducing here.
5+
6+
More text
7+
8+
Lorem ipsum
9+
10+
11+
## Schema

0 commit comments

Comments
 (0)