|
| 1 | +package guides |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/MakeNowJust/heredoc" |
| 13 | + "github.com/algolia/docli/pkg/cmd/generate/utils" |
| 14 | + "github.com/algolia/docli/pkg/dictionary" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +type Options struct { |
| 19 | + GuidesFile string |
| 20 | + OutputDirectory string |
| 21 | +} |
| 22 | + |
| 23 | +// GuidesMap represents the data from a guide file. |
| 24 | +type GuidesMap map[string]map[string]string |
| 25 | + |
| 26 | +func NewGuidesCommand() *cobra.Command { |
| 27 | + opts := &Options{} |
| 28 | + |
| 29 | + cmd := &cobra.Command{ |
| 30 | + Use: "guides <guides>", |
| 31 | + Short: "Generate guide snippets from a JSON file", |
| 32 | + Long: heredoc.Doc(` |
| 33 | + This command reads a data file with guide snippets. |
| 34 | + It generates an MDX file for each guide. |
| 35 | + `), |
| 36 | + Example: heredoc.Doc(` |
| 37 | + # Run from root of algolia/docs-new |
| 38 | + docli gen guides guides.json -o snippets/guides |
| 39 | + `), |
| 40 | + Args: cobra.ExactArgs(1), |
| 41 | + Run: func(cmd *cobra.Command, args []string) { |
| 42 | + opts.GuidesFile = args[0] |
| 43 | + runCommand(opts) |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + cmd.Flags(). |
| 48 | + StringVarP(&opts.OutputDirectory, "output", "o", "out", "Output directory for generated MDX files") |
| 49 | + |
| 50 | + return cmd |
| 51 | +} |
| 52 | + |
| 53 | +func runCommand(opts *Options) { |
| 54 | + bytes, err := os.ReadFile(opts.GuidesFile) |
| 55 | + if err != nil { |
| 56 | + log.Fatalf("Error: %e", err) |
| 57 | + } |
| 58 | + |
| 59 | + var data GuidesMap |
| 60 | + if err := json.Unmarshal(bytes, &data); err != nil { |
| 61 | + log.Fatalf("Error: %e", err) |
| 62 | + } |
| 63 | + |
| 64 | + fmt.Printf("Generating guide snippet files for: %s\n", opts.GuidesFile) |
| 65 | + fmt.Printf("Writing output in: %s\n", opts.OutputDirectory) |
| 66 | + |
| 67 | + guideNames := make([]string, 0, len(data)) |
| 68 | + for guide := range data { |
| 69 | + guideNames = append(guideNames, guide) |
| 70 | + } |
| 71 | + |
| 72 | + sort.Strings(guideNames) |
| 73 | + |
| 74 | + for _, guide := range guideNames { |
| 75 | + err := writeGuide( |
| 76 | + opts.OutputDirectory, |
| 77 | + fmt.Sprintf("%s.mdx", utils.ToKebabCase(guide)), |
| 78 | + generateMarkdownSnippet(data[guide]), |
| 79 | + ) |
| 80 | + if err != nil { |
| 81 | + log.Fatalf("Error: %e", err) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// generateMarkdownSnippet generates a CodeGroup block. |
| 87 | +func generateMarkdownSnippet(snippet map[string]string) string { |
| 88 | + result := "<CodeGroup>\n" |
| 89 | + languages := sortLanguages(snippet) |
| 90 | + |
| 91 | + for _, lang := range languages { |
| 92 | + result += fmt.Sprintf( |
| 93 | + "\n```%s %s\n", |
| 94 | + dictionary.NormalizeLang(lang), |
| 95 | + utils.GetLanguageName(lang), |
| 96 | + ) |
| 97 | + replaced := strings.ReplaceAll(snippet[lang], "<YOUR_INDEX_NAME>", "ALGOLIA_INDEX_NAME") |
| 98 | + replaced = strings.ReplaceAll( |
| 99 | + replaced, |
| 100 | + "cts_e2e_deleteObjects_javascript", |
| 101 | + "ALGOLIA_INDEX_NAME", |
| 102 | + ) |
| 103 | + result += replaced |
| 104 | + result += "\n```\n" |
| 105 | + } |
| 106 | + |
| 107 | + result += "\n</CodeGroup>" |
| 108 | + |
| 109 | + return result |
| 110 | +} |
| 111 | + |
| 112 | +// sortLanguages returns a list of sorted languages. |
| 113 | +func sortLanguages(snippet map[string]string) []string { |
| 114 | + sorted := make([]string, 0, len(snippet)) |
| 115 | + |
| 116 | + for lang := range snippet { |
| 117 | + sorted = append(sorted, lang) |
| 118 | + } |
| 119 | + |
| 120 | + sort.Strings(sorted) |
| 121 | + |
| 122 | + return sorted |
| 123 | +} |
| 124 | + |
| 125 | +// writeGuide writes the guide snippets into MDX files. |
| 126 | +func writeGuide(path string, filename string, snippet string) error { |
| 127 | + err := os.MkdirAll(path, 0o700) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + fullPath := filepath.Join(path, filename) |
| 133 | + |
| 134 | + out, err := os.Create(fullPath) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + defer out.Close() |
| 139 | + |
| 140 | + if _, err := out.WriteString(snippet); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
0 commit comments