Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions printingpress/agent_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,10 @@ func renderOperationMD(ctx llmRenderContext, op *OperationPage) string {
b.WriteString(curl)
}

if codeSamples := renderCodeSamplesMD(op.CodeSamples); codeSamples != "" {
b.WriteString(codeSamples)
}

if idGuidance := renderOperationIDGuidanceMD(ctx, op); idGuidance != "" {
b.WriteString(idGuidance)
}
Expand Down Expand Up @@ -1898,6 +1902,61 @@ func renderCurlMD(op *OperationPage) string {
return b.String()
}

func renderCodeSamplesMD(samples []*CodeSample) string {
if len(samples) == 0 {
return ""
}
var b strings.Builder
for i, sample := range samples {
if sample == nil || strings.TrimSpace(sample.Source) == "" {
continue
}
if b.Len() == 0 {
b.WriteString("#### Code Samples\n\n")
}
b.WriteString("**" + codeSampleMarkdownLabel(sample, i) + "**\n\n")
fence := markdownCodeFence(sample.Source)
language := markdownFenceLanguage(sample.Lang)
b.WriteString(fence + language + "\n")
b.WriteString(strings.TrimRight(sample.Source, "\n"))
b.WriteString("\n" + fence + "\n\n")
}
return b.String()
}

func codeSampleMarkdownLabel(sample *CodeSample, index int) string {
// Markdown has no separate tab label and panel heading, so preserve both names when they differ.
if sample.Lang != "" && sample.Label != "" && sample.Label != sample.Lang {
return singleLine(sample.Lang + " - " + sample.Label)
}
return sample.DisplayLabel(index)
}

func markdownCodeFence(source string) string {
fence := "```"
for strings.Contains(source, fence) {
fence += "`"
}
return fence
}

func markdownFenceLanguage(language string) string {
language = strings.TrimSpace(language)
if language == "" {
return ""
}
var b strings.Builder
for _, r := range language {
if r >= 'a' && r <= 'z' ||
r >= 'A' && r <= 'Z' ||
r >= '0' && r <= '9' ||
r == '-' || r == '_' || r == '+' || r == '#' || r == '.' {
b.WriteRune(r)
}
}
return b.String()
}

func uniqueCurlVariants(variants []*CurlVariant) []*CurlVariant {
seen := make(map[string]bool)
out := make([]*CurlVariant, 0, len(variants))
Expand Down
36 changes: 36 additions & 0 deletions printingpress/agent_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,42 @@ func TestRenderOperationMD_CurlRelatedOpsGuidanceAndScopeDedup(t *testing.T) {
assert.Equal(t, 1, strings.Count(md, "sp:config-object-mapping:manage"))
}

func TestRenderOperationMD_CodeSamples(t *testing.T) {
curlVariants := []*CurlVariant{
{Label: "Default", Command: "curl https://api.example.com/bookings"},
}
curlJSON, err := json.Marshal(curlVariants)
require.NoError(t, err)

op := &OperationPage{
Method: "POST",
Path: "/bookings",
Summary: "Create booking",
CurlJSON: string(curlJSON),
CodeSamples: []*CodeSample{
{
Lang: "typescript",
Label: "create-booking_json",
Source: "const client = new TrainTravelSDK();\nawait client.bookings.create({ tripId: \"abc\" });\n",
},
{
Label: "python-sdk",
Source: "client.bookings.create({\"tripId\": \"abc\"})\n",
},
},
}

md := renderOperationMD(rootLLMRenderContext(&Site{}), op)

assert.Contains(t, md, "#### cURL")
assert.Contains(t, md, "#### Code Samples")
assert.Contains(t, md, "**typescript - create-booking_json**")
assert.Contains(t, md, "```typescript\nconst client = new TrainTravelSDK();")
assert.Contains(t, md, "**python-sdk**")
assert.Contains(t, md, "```\nclient.bookings.create")
assert.Less(t, strings.Index(md, "#### cURL"), strings.Index(md, "#### Code Samples"))
}

func TestRenderOperationMD_CurlSuppressesDuplicateVariants(t *testing.T) {
curlVariants := []*CurlVariant{
{Label: "Default", Command: "curl https://api.example.com/items"},
Expand Down
Loading
Loading