MCPcopy Create free account
hub / github.com/algolia/cli / ExamplesList

Method ExamplesList

internal/docs/docs.go:177–243  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

175}
176
177func (cmd Command) ExamplesList() []Example {
178 type exampleBuilder struct {
179 desc string
180 codeLines []string
181 }
182
183 var examples []Example
184 current := exampleBuilder{}
185
186 flush := func() {
187 code := strings.TrimSpace(strings.Join(current.codeLines, "\n"))
188 if code == "" {
189 current = exampleBuilder{}
190 return
191 }
192
193 formattedExample := Example{
194 Desc: current.desc,
195 Code: code,
196 }
197 if cmd.RunInWebCLI &&
198 !strings.Contains(code, ">") &&
199 !strings.Contains(code, "<") &&
200 !strings.Contains(code, "output") &&
201 !strings.Contains(code, "-F") &&
202 !strings.Contains(code, "|") {
203 formattedExample.WebCLICommand = strings.ReplaceAll(code, `"`, `\"`)
204 }
205
206 examples = append(examples, formattedExample)
207 current = exampleBuilder{}
208 }
209
210 for _, rawLine := range strings.Split(cmd.Examples, "\n") {
211 line := strings.TrimSpace(rawLine)
212 if line == "" {
213 if len(current.codeLines) > 0 {
214 flush()
215 }
216 continue
217 }
218
219 switch {
220 case strings.HasPrefix(line, "#"):
221 flush()
222 current.desc = strings.TrimSpace(strings.TrimPrefix(line, "#"))
223 case strings.HasPrefix(line, "$"):
224 if current.desc == "" && len(current.codeLines) > 0 {
225 flush()
226 }
227 current.codeLines = append(
228 current.codeLines,
229 strings.TrimSpace(strings.TrimPrefix(line, "$")),
230 )
231 case len(current.codeLines) > 0:
232 current.codeLines = append(current.codeLines, line)
233 case current.desc == "":
234 current.desc = line

Callers 2

GenMdxTreeFunction · 0.80

Calls

no outgoing calls