Skip to content

Commit 177f309

Browse files
authored
docs: update template and fix help (#218)
* chore: clean up after myself * fix: root path to CLI reference * fix: flags and docs generation * fix: misplaced empty line
1 parent b65799a commit 177f309

7 files changed

Lines changed: 300 additions & 270 deletions

File tree

internal/docs/docs.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ type Flag struct {
3434

3535
func newFlag(flag *pflag.Flag) Flag {
3636
return Flag{
37-
Name: flag.Name,
38-
Shorthand: flag.Shorthand,
39-
// Add two spaces before each newline to force a newline in markdown
40-
Description: strings.ReplaceAll(flag.Usage, "\n", " \n"),
37+
Name: flag.Name,
38+
Shorthand: flag.Shorthand,
39+
Description: strings.TrimSpace(flag.Usage),
4140
Default: flag.DefValue,
4241
}
4342
}

internal/docs/mdx.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
_ "embed"
55
"os"
66
"path/filepath"
7+
"regexp"
78
"strings"
89
"text/template"
910

@@ -12,6 +13,8 @@ import (
1213

1314
const mdxDocsRootSlug = "doc/tools/cli/commands"
1415

16+
var algoliaAPIReferenceLinkRE = regexp.MustCompile(`See: (/doc/api-reference/api-parameters/([^\s)]+))`)
17+
1518
//go:embed mdx.tpl
1619
var mdxTemplate string
1720

@@ -26,6 +29,8 @@ func GenMdxTree(cmd *cobra.Command, dir string) error {
2629
"getExamples": func(cmd Command) []Example {
2730
return cmd.ExamplesList()
2831
},
32+
"formatAlgoliaDocLinks": formatAlgoliaDocLinks,
33+
"trimTrailingNewlines": trimTrailingNewlines,
2934
}).Parse(mdxTemplate)
3035
if err != nil {
3136
return err
@@ -48,7 +53,7 @@ func newMdxPage(cmd Command) mdxPage {
4853
}
4954

5055
func writeMdxPageTree(tpl *template.Template, dir string, page mdxPage) error {
51-
filename := filepath.Join(dir, commandOutputDir(page.Command), "index.mdx")
56+
filename := filepath.Join(dir, commandOutputFilename(page.Command))
5257
if err := os.MkdirAll(filepath.Dir(filename), 0o755); err != nil {
5358
return err
5459
}
@@ -81,8 +86,16 @@ func commandPathParts(cmd Command) []string {
8186
return parts[1:]
8287
}
8388

84-
func commandOutputDir(cmd Command) string {
85-
return filepath.Join(commandPathParts(cmd)...)
89+
func commandOutputFilename(cmd Command) string {
90+
parts := commandPathParts(cmd)
91+
if len(parts) == 0 {
92+
return "index.mdx"
93+
}
94+
95+
last := len(parts) - 1
96+
parts[last] += ".mdx"
97+
98+
return filepath.Join(parts...)
8699
}
87100

88101
func buildMdxSlug(parts []string) string {
@@ -92,3 +105,12 @@ func buildMdxSlug(parts []string) string {
92105

93106
return mdxDocsRootSlug + "/" + strings.Join(parts, "/")
94107
}
108+
109+
func trimTrailingNewlines(s string) string {
110+
return strings.TrimRight(s, "\n")
111+
}
112+
113+
func formatAlgoliaDocLinks(s string) string {
114+
s = strings.ReplaceAll(s, "https://www.algolia.com/doc", "/doc")
115+
return algoliaAPIReferenceLinkRE.ReplaceAllString(s, "See: [`$2`]($1)")
116+
}

internal/docs/mdx.tpl

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,45 @@
11
---
2-
navigation: "cli"
3-
title: |-
4-
{{ .Name }}
5-
description: |-
6-
{{ .Description }}
7-
slug: {{ .Slug }}
2+
title: {{ .Name }}
3+
description: {{ .Description }}
4+
public: true
85
---
9-
{{ if .Description }}
10-
{{ .Description }}
11-
{{ end }}
12-
13-
## Usage
14-
15-
`{{ .Usage }}`
166

17-
{{ if .Aliases }}
18-
## Aliases
19-
20-
{{ range $alias := .Aliases -}}
21-
- `{{ $alias }}`
22-
{{ end }}
23-
{{ end }}
7+
```txt Usage
8+
{{ .Usage }}
9+
```
10+
{{- if .SubPages }}
2411

25-
{{ if .SubPages }}
26-
## Subcommands
12+
## Commands
2713

2814
{{ range $subPage := .SubPages -}}
29-
- [`{{ $subPage.Name }}`](/{{ $subPage.Slug }}): {{ $subPage.Description }}
30-
{{ end }}
31-
{{ end }}
15+
- [`{{ $subPage.Name }}`](/{{ $subPage.Slug }})
16+
{{ end -}}
17+
{{ end -}}
18+
{{ $examples := getExamples .Command -}}
19+
{{ if $examples }}
3220

33-
{{ $examples := getExamples .Command }}{{ if $examples }}
3421
## Examples
35-
{{ range $example := $examples }}
36-
{{ $example.Desc }}
3722

38-
```sh{{ if $example.WebCLICommand }} command="{{$example.WebCLICommand}}"{{ end }}
23+
{{ range $example := $examples -}}
24+
{{ if $example.Desc }}{{ $example.Desc }}:
25+
26+
{{ end -}}
27+
```sh icon=square-terminal
3928
{{ $example.Code }}
4029
```
41-
{{ end }}
42-
{{ end }}
43-
44-
{{ range $flagKey, $flagSlice := .Flags -}}
30+
{{ end -}}
31+
{{ end -}}
32+
{{ range $flagKey, $flagSlice := .Flags }}
4533
{{ if $flagSlice }}
4634
## {{ $flagKey }}
4735

4836
{{ range $flag := $flagSlice -}}
49-
- {{ if $flag.Shorthand }}`-{{ $flag.Shorthand }}`, {{ end }}`--{{ $flag.Name }}`: {{ $flag.Description }}
50-
{{ end }}
37+
<ParamField body="{{ if $flag.Shorthand }}-{{ $flag.Shorthand }}, {{ end }}--{{ $flag.Name }}">
38+
39+
{{ formatAlgoliaDocLinks (trimTrailingNewlines $flag.Description) }}
40+
41+
</ParamField>
42+
43+
{{ end -}}
5144
{{ end }}
52-
{{ end }}
45+
{{- end -}}

internal/docs/mdx_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestGenMdxTreeSupportsCurrentCommandTree(t *testing.T) {
2222
rootContent := readTestFile(t, filepath.Join(dir, "index.mdx"))
2323
require.Contains(t, rootContent, "algolia search MOVIES --query \"toy story\"")
2424

25-
logoutContent := readTestFile(t, filepath.Join(dir, "auth", "logout", "index.mdx"))
25+
logoutContent := readTestFile(t, filepath.Join(dir, "auth", "logout.mdx"))
2626
require.Contains(t, logoutContent, "algolia auth logout")
2727
}
2828

internal/docs/mdx_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,16 @@ func TestGenMdxTreeWritesNestedCommandPages(t *testing.T) {
6161
require.NoError(t, GenMdxTree(root, dir))
6262

6363
rootContent := readTestFile(t, filepath.Join(dir, "index.mdx"))
64-
require.Contains(t, rootContent, "slug: doc/tools/cli/commands")
64+
require.Contains(t, rootContent, "public: true")
6565
require.Contains(t, rootContent, "[`algolia events`](/doc/tools/cli/commands/events)")
6666

67-
eventsContent := readTestFile(t, filepath.Join(dir, "events", "index.mdx"))
68-
require.Contains(t, eventsContent, "slug: doc/tools/cli/commands/events")
67+
eventsContent := readTestFile(t, filepath.Join(dir, "events.mdx"))
68+
require.Contains(t, eventsContent, "public: true")
6969
require.Contains(t, eventsContent, "[`algolia events sources`](/doc/tools/cli/commands/events/sources)")
7070

71-
listContent := readTestFile(t, filepath.Join(dir, "events", "sources", "list", "index.mdx"))
72-
require.Contains(t, listContent, "slug: doc/tools/cli/commands/events/sources/list")
73-
require.Contains(t, listContent, "`algolia events sources list [flags]`")
74-
require.Contains(t, listContent, "`-F`, `--format`")
71+
listContent := readTestFile(t, filepath.Join(dir, "events", "sources", "list.mdx"))
72+
require.Contains(t, listContent, "algolia events sources list [flags]")
73+
require.Contains(t, listContent, `<ParamField body="-F, --format">`)
7574
}
7675

7776
func TestGenMdxTreeSupportsCodeOnlyExamples(t *testing.T) {

0 commit comments

Comments
 (0)