Skip to content

Commit 9d942d4

Browse files
patnikoCopilot
andauthored
feat(docs-validation): add hidden block directive and convert samples (#600)
directive pair that lets authors include a full compilable code block that is validated but not rendered in docs, while the visible snippet that follows is automatically skipped from validation. Convert 4 skipped samples in getting-started.md to use hidden blocks: - Python event subscription example - Go event subscription example - C# event subscription example - Go CLI server connection example This increases validated code blocks from 182 to 186 and demonstrates the hidden block pattern for future conversions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fc63890 commit 9d942d4

File tree

3 files changed

+157
-5
lines changed

3 files changed

+157
-5
lines changed

docs/getting-started.md

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,32 @@ unsubscribeIdle();
426426
<details>
427427
<summary><strong>Python</strong></summary>
428428

429-
<!-- docs-validate: skip -->
429+
<!-- docs-validate: hidden -->
430+
```python
431+
from copilot import CopilotClient
432+
from copilot.generated.session_events import SessionEvent, SessionEventType
433+
434+
client = CopilotClient()
435+
436+
session = client.create_session({"on_permission_request": lambda req, inv: {"kind": "approved"}})
437+
438+
# Subscribe to all events
439+
unsubscribe = session.on(lambda event: print(f"Event: {event.type}"))
440+
441+
# Filter by event type in your handler
442+
def handle_event(event: SessionEvent) -> None:
443+
if event.type == SessionEventType.SESSION_IDLE:
444+
print("Session is idle")
445+
elif event.type == SessionEventType.ASSISTANT_MESSAGE:
446+
print(f"Message: {event.data.content}")
447+
448+
unsubscribe = session.on(handle_event)
449+
450+
# Later, to unsubscribe:
451+
unsubscribe()
452+
```
453+
<!-- /docs-validate: hidden -->
454+
430455
```python
431456
# Subscribe to all events
432457
unsubscribe = session.on(lambda event: print(f"Event: {event.type}"))
@@ -449,7 +474,39 @@ unsubscribe()
449474
<details>
450475
<summary><strong>Go</strong></summary>
451476

452-
<!-- docs-validate: skip -->
477+
<!-- docs-validate: hidden -->
478+
```go
479+
package main
480+
481+
import (
482+
"fmt"
483+
484+
copilot "github.com/github/copilot-sdk/go"
485+
)
486+
487+
func main() {
488+
session := &copilot.Session{}
489+
490+
// Subscribe to all events
491+
unsubscribe := session.On(func(event copilot.SessionEvent) {
492+
fmt.Println("Event:", event.Type)
493+
})
494+
495+
// Filter by event type in your handler
496+
session.On(func(event copilot.SessionEvent) {
497+
if event.Type == "session.idle" {
498+
fmt.Println("Session is idle")
499+
} else if event.Type == "assistant.message" {
500+
fmt.Println("Message:", *event.Data.Content)
501+
}
502+
})
503+
504+
// Later, to unsubscribe:
505+
unsubscribe()
506+
}
507+
```
508+
<!-- /docs-validate: hidden -->
509+
453510
```go
454511
// Subscribe to all events
455512
unsubscribe := session.On(func(event copilot.SessionEvent) {
@@ -474,7 +531,38 @@ unsubscribe()
474531
<details>
475532
<summary><strong>.NET</strong></summary>
476533

477-
<!-- docs-validate: skip -->
534+
<!-- docs-validate: hidden -->
535+
```csharp
536+
using GitHub.Copilot.SDK;
537+
538+
public static class EventSubscriptionExample
539+
{
540+
public static void Example(CopilotSession session)
541+
{
542+
// Subscribe to all events
543+
var unsubscribe = session.On(ev => Console.WriteLine($"Event: {ev.Type}"));
544+
545+
// Filter by event type using pattern matching
546+
session.On(ev =>
547+
{
548+
switch (ev)
549+
{
550+
case SessionIdleEvent:
551+
Console.WriteLine("Session is idle");
552+
break;
553+
case AssistantMessageEvent msg:
554+
Console.WriteLine($"Message: {msg.Data.Content}");
555+
break;
556+
}
557+
});
558+
559+
// Later, to unsubscribe:
560+
unsubscribe.Dispose();
561+
}
562+
}
563+
```
564+
<!-- /docs-validate: hidden -->
565+
478566
```csharp
479567
// Subscribe to all events
480568
var unsubscribe = session.On(ev => Console.WriteLine($"Event: {ev.Type}"));
@@ -1227,7 +1315,37 @@ session = await client.create_session({"on_permission_request": PermissionHandle
12271315
<details>
12281316
<summary><strong>Go</strong></summary>
12291317

1230-
<!-- docs-validate: skip -->
1318+
<!-- docs-validate: hidden -->
1319+
```go
1320+
package main
1321+
1322+
import (
1323+
"context"
1324+
"log"
1325+
1326+
copilot "github.com/github/copilot-sdk/go"
1327+
)
1328+
1329+
func main() {
1330+
ctx := context.Background()
1331+
1332+
client := copilot.NewClient(&copilot.ClientOptions{
1333+
CLIUrl: "localhost:4321",
1334+
})
1335+
1336+
if err := client.Start(ctx); err != nil {
1337+
log.Fatal(err)
1338+
}
1339+
defer client.Stop()
1340+
1341+
// Use the client normally
1342+
_, _ = client.CreateSession(ctx, &copilot.SessionConfig{
1343+
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
1344+
})
1345+
}
1346+
```
1347+
<!-- /docs-validate: hidden -->
1348+
12311349
```go
12321350
import copilot "github.com/github/copilot-sdk/go"
12331351

scripts/docs-validation/extract.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface CodeBlock {
3131
file: string;
3232
line: number;
3333
skip: boolean;
34+
hidden: boolean;
3435
wrapAsync: boolean;
3536
}
3637

@@ -58,6 +59,7 @@ function parseMarkdownCodeBlocks(
5859
let blockStartLine = 0;
5960
let skipNext = false;
6061
let wrapAsync = false;
62+
let inHiddenBlock = false;
6163

6264
for (let i = 0; i < lines.length; i++) {
6365
const line = lines[i];
@@ -71,6 +73,16 @@ function parseMarkdownCodeBlocks(
7173
wrapAsync = true;
7274
continue;
7375
}
76+
if (line.includes("<!-- docs-validate: hidden -->")) {
77+
inHiddenBlock = true;
78+
continue;
79+
}
80+
if (line.includes("<!-- /docs-validate: hidden -->")) {
81+
inHiddenBlock = false;
82+
// Skip the next visible code block since the hidden one replaces it
83+
skipNext = true;
84+
continue;
85+
}
7486

7587
// Start of code block
7688
if (!inCodeBlock && line.startsWith("```")) {
@@ -92,12 +104,17 @@ function parseMarkdownCodeBlocks(
92104
file: filePath,
93105
line: blockStartLine,
94106
skip: skipNext,
107+
hidden: inHiddenBlock,
95108
wrapAsync: wrapAsync,
96109
});
97110
inCodeBlock = false;
98111
currentLang = "";
99112
currentCode = [];
100-
skipNext = false;
113+
// Only reset skipNext when NOT in a hidden block — hidden blocks
114+
// can contain multiple code fences that all get validated.
115+
if (!inHiddenBlock) {
116+
skipNext = false;
117+
}
101118
wrapAsync = false;
102119
continue;
103120
}
@@ -358,6 +375,7 @@ async function main() {
358375
const langCounts = new Map<string, number>();
359376
let totalBlocks = 0;
360377
let skippedBlocks = 0;
378+
let hiddenBlocks = 0;
361379

362380
for (const mdFile of mdFiles) {
363381
const fullPath = path.join(DOCS_DIR, mdFile);
@@ -370,6 +388,10 @@ async function main() {
370388
continue;
371389
}
372390

391+
if (block.hidden) {
392+
hiddenBlocks++;
393+
}
394+
373395
// Skip empty or trivial blocks
374396
if (block.code.trim().length < 10) {
375397
continue;
@@ -426,6 +448,9 @@ async function main() {
426448
if (skippedBlocks > 0) {
427449
console.log(` Skipped ${skippedBlocks}`);
428450
}
451+
if (hiddenBlocks > 0) {
452+
console.log(` Hidden ${hiddenBlocks}`);
453+
}
429454
console.log(`\nOutput: ${OUTPUT_DIR}`);
430455
}
431456

scripts/docs-validation/validate.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,15 @@ async function main() {
465465
console.log(" 3. Re-run: npm run validate");
466466
console.log("\nTo skip a code block, add before it:");
467467
console.log(" <!-- docs-validate: skip -->");
468+
console.log("\nTo validate a complete version while showing a snippet:");
469+
console.log(" <!-- docs-validate: hidden -->");
470+
console.log(" ```lang");
471+
console.log(" // full compilable code");
472+
console.log(" ```");
473+
console.log(" <!-- /docs-validate: hidden -->");
474+
console.log(" ```lang");
475+
console.log(" // visible snippet (auto-skipped)");
476+
console.log(" ```");
468477
process.exit(1);
469478
}
470479

0 commit comments

Comments
 (0)