Skip to content

Commit 10acaa0

Browse files
committed
Improve formatting of newlines in markdown files
1 parent cdcef5d commit 10acaa0

5 files changed

Lines changed: 31 additions & 10 deletions

plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Source Plugin: test
2+
23
## Tables
4+
35
- [test_table](test_table.md)
46
- [relation_table](relation_table.md)
57
- [relation_relation_table](relation_relation_table.md)

plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-relation_relation_table.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ Description for relational table's relation
55
The primary key for this table is **_cq_id**.
66

77
## Relations
8-
This table depends on [relation_table](relation_table.md).
98

9+
This table depends on [relation_table](relation_table.md).
1010

1111
## Columns
12+
1213
| Name | Type |
1314
| ------------- | ------------- |
1415
|_cq_source_name|String|

plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-relation_table.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ Description for relational table
55
The primary key for this table is **_cq_id**.
66

77
## Relations
8+
89
This table depends on [test_table](test_table.md).
910

1011
The following tables depend on relation_table:
1112
- [relation_relation_table](relation_relation_table.md)
1213

1314
## Columns
15+
1416
| Name | Type |
1517
| ------------- | ------------- |
1618
|_cq_source_name|String|

plugins/.snapshots/TestGenerateSourcePluginDocs-Markdown-test_table.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The following tables depend on test_table:
1111
- [relation_table2](relation_table2.md)
1212

1313
## Columns
14+
1415
| Name | Type |
1516
| ------------- | ------------- |
1617
|_cq_source_name|String|

plugins/source_docs.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package plugins
22

33
import (
4+
"bytes"
45
"embed"
56
"encoding/json"
67
"fmt"
78
"os"
89
"path/filepath"
10+
"regexp"
911
"strings"
1012
"text/template"
1113

@@ -15,6 +17,9 @@ import (
1517
//go:embed templates/*.go.tpl
1618
var templatesFS embed.FS
1719

20+
var reMatchNewlines = regexp.MustCompile(`\n{3,}`)
21+
var reMatchHeaders = regexp.MustCompile(`(#{1,6}.+)\n+`)
22+
1823
// GenerateSourcePluginDocs creates table documentation for the source plugin based on its list of tables
1924
func (p *SourcePlugin) GenerateSourcePluginDocs(dir, format string) error {
2025
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
@@ -90,15 +95,17 @@ func (p *SourcePlugin) renderTablesAsMarkdown(dir string) error {
9095
return fmt.Errorf("failed to parse template for README.md: %v", err)
9196
}
9297

98+
var b bytes.Buffer
99+
if err := t.Execute(&b, p); err != nil {
100+
return fmt.Errorf("failed to execute template: %v", err)
101+
}
102+
content := formatMarkdown(b.String())
93103
outputPath := filepath.Join(dir, "README.md")
94104
f, err := os.Create(outputPath)
95105
if err != nil {
96106
return fmt.Errorf("failed to create file %v: %v", outputPath, err)
97107
}
98-
defer f.Close()
99-
if err := t.Execute(f, p); err != nil {
100-
return fmt.Errorf("failed to execute template: %v", err)
101-
}
108+
f.WriteString(content)
102109
return nil
103110
}
104111

@@ -124,15 +131,23 @@ func renderTable(table *schema.Table, dir string) error {
124131
}
125132

126133
outputPath := filepath.Join(dir, fmt.Sprintf("%s.md", table.Name))
134+
135+
var b bytes.Buffer
136+
if err := t.Execute(&b, table); err != nil {
137+
return fmt.Errorf("failed to execute template: %v", err)
138+
}
139+
content := formatMarkdown(b.String())
127140
f, err := os.Create(outputPath)
128141
if err != nil {
129142
return fmt.Errorf("failed to create file %v: %v", outputPath, err)
130143
}
131-
defer f.Close()
132-
if err := t.Execute(f, table); err != nil {
133-
return fmt.Errorf("failed to execute template: %v", err)
134-
}
135-
return nil
144+
f.WriteString(content)
145+
return f.Close()
146+
}
147+
148+
func formatMarkdown(s string) string {
149+
s = reMatchNewlines.ReplaceAllString(s, "\n\n")
150+
return reMatchHeaders.ReplaceAllString(s, `$1`+"\n\n")
136151
}
137152

138153
func formatType(v schema.ValueType) string {

0 commit comments

Comments
 (0)